Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
PhysicsBodyComponent.h
Go to the documentation of this file.
1#pragma once
2
4#include <glm/glm.hpp>
5#include <glm/gtc/quaternion.hpp>
6#include <entt/entt.hpp>
7#include <memory>
8#include <vector>
9#include <set>
10
11namespace Engine::Core
12{
13 class PhysicsEngine;
14} // namespace Engine::Core
15
16namespace Engine::Entities
17{
18 class Actor;
19 class SceneNode;
20} // namespace Engine::Entities
21
22class btRigidBody;
23class btCompoundShape;
24class btCollisionShape;
25class btDiscreteDynamicsWorld;
26
27namespace Engine::Components {
28 class Collision;
29
37 class PhysicsBody : public Logic {
38 public:
39 static constexpr bool IsUnique = true;
40 static constexpr const char* ComponentName = "PhysicsBody";
41
48 PhysicsBody(Entities::Scene* scene, entt::entity entity,
49 std::shared_ptr<Entities::SceneNode> sceneNode);
50
51 ~PhysicsBody() override;
52
53 void beginPlay() override;
54 void endPlay() override;
55 bool drawImGui() override;
56
57 // =========================================================================
58 // Physics Properties
59 // =========================================================================
60
65 void setMass(float mass);
66
71 [[nodiscard]] float getMass() const { return mass_; }
72
77 void setFriction(float friction);
78
83 [[nodiscard]] float getFriction() const { return friction_; }
84
89 void setRestitution(float restitution);
90
95 [[nodiscard]] float getRestitution() const { return restitution_; }
96
102 void setKinematic(bool kinematic);
103
108 [[nodiscard]] bool isKinematic() const { return isKinematic_; }
109
114 void setStatic(bool isStatic);
115
120 [[nodiscard]] bool isStatic() const { return isStatic_; }
121
122 // =========================================================================
123 // Transform Control
124 // =========================================================================
125
130 void teleport(const glm::vec3& position);
131
137 void teleport(const glm::vec3& position, const glm::quat& rotation);
138
143 void setLinearVelocity(const glm::vec3& velocity);
144
149 [[nodiscard]] glm::vec3 getLinearVelocity() const;
150
155 void setAngularVelocity(const glm::vec3& velocity);
156
161 [[nodiscard]] glm::vec3 getAngularVelocity() const;
162
163 // =========================================================================
164 // Collision Management (called by CollisionComponents)
165 // =========================================================================
166
173
180
186
191
196 [[nodiscard]] btRigidBody* getRigidBody() const { return rigidBody_; }
197
202 [[nodiscard]] Entities::Actor* getHitActor() const;
203
208 [[nodiscard]] btDiscreteDynamicsWorld* getDynamicsWorld() const;
209
210 // =========================================================================
211 // Collision Callbacks
212 // =========================================================================
213
219
220 // =========================================================================
221 // Debug Visualization
222 // =========================================================================
223
230 void debugDraw() const;
231
232 private:
237 [[nodiscard]] Core::PhysicsEngine * getPhysicsEngine() const;
238
239 entt::entity entity_;
240 std::weak_ptr<Entities::SceneNode> sceneNode_;
241
242 std::vector<Components::Collision*> collisions_;
243 btCompoundShape* compoundShape_ = nullptr;
244 btRigidBody* rigidBody_ = nullptr;
245
246 float mass_ = 1.0f;
247 float friction_ = 0.5f;
248 float restitution_ = 0.0f;
249 bool isKinematic_ = false;
250 bool isStatic_ = false;
251 bool shapeDirty_ = true;
252 bool isInitialized_ = false;
253
254 // For collision callbacks - track previous frame's collisions
255 std::set<Components::Collision*> previousCollisions_;
256 };
257
258} // namespace EngineCore
Base class for collision shape components.
Logic(Entities::Scene *owningScene)
float getRestitution() const
Gets the restitution.
float getMass() const
Gets the mass of the rigid body.
void setRestitution(float restitution)
Sets the restitution (bounciness).
void setAngularVelocity(const glm::vec3 &velocity)
Sets the angular velocity.
std::vector< Components::Collision * > collisions_
float getFriction() const
Gets the friction coefficient.
glm::vec3 getAngularVelocity() const
Gets the angular velocity.
void debugDraw() const
Draws debug visualization of all collision shapes.
void endPlay() override
Called when the component is removed or the game ends.
void beginPlay() override
Called when the component is added to the scene or the game starts.
std::weak_ptr< Entities::SceneNode > sceneNode_
btDiscreteDynamicsWorld * getDynamicsWorld() const
Gets the Bullet dynamics world.
void setMass(float mass)
Sets the mass of the rigid body.
void unregisterCollision(Components::Collision *collision)
Unregisters a collision component from this body. Called by CollisionComponent::endPlay().
void teleport(const glm::vec3 &position, const glm::quat &rotation)
Teleports the body to a new position and rotation.
void markShapeDirty()
Marks the collision shape as needing rebuild. Called when a collision component's shape changes.
void teleport(const glm::vec3 &position)
Teleports the body to a new position.
glm::vec3 getLinearVelocity() const
Gets the linear velocity.
void rebuildIfDirty()
Rebuilds the collision shape if dirty.
void registerCollision(Components::Collision *collision)
Registers a collision component with this body. Called by CollisionComponent::beginPlay().
bool isKinematic() const
Checks if the body is kinematic.
void setLinearVelocity(const glm::vec3 &velocity)
Sets the linear velocity.
PhysicsBody(Entities::Scene *scene, entt::entity entity, std::shared_ptr< Entities::SceneNode > sceneNode)
Constructs a PhysicsBodyComponent.
void setFriction(float friction)
Sets the friction coefficient.
Core::PhysicsEngine * getPhysicsEngine() const
void processCollisions()
Processes collision events and dispatches callbacks. Called after physics step.
void setStatic(bool isStatic)
Sets whether the body is static (immovable).
static constexpr const char * ComponentName
bool drawImGui() override
Draws component-specific ImGui inspector controls.
void setKinematic(bool kinematic)
Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics.
bool isStatic() const
Checks if the body is static.
Entities::Actor * getHitActor() const
Gets the actor owning this physics body when the owning entity is an actor.
btRigidBody * getRigidBody() const
Gets the Bullet rigid body.
std::set< Components::Collision * > previousCollisions_
The physics engine manages creating and destroying physics objects for the physics simulation.
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
Definition Actor.h:65
Represents a node in the scene graph, containing information about its position, rotation,...
Definition SceneGraph.h:18
A scene is the overarching structure which can spawn, contain and destroy actors or entities.
Definition Scene.h:62
Manages IBL (Image-Based Lighting) resources for specular reflections.