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
11class btRigidBody;
12class btCompoundShape;
13class btCollisionShape;
14
15namespace EngineCore {
16 class Scene;
17 class SceneNode;
19 class PhysicsEngine;
20
32 public:
33 static constexpr bool IsUnique = true;
34
41 PhysicsBodyComponent(Scene* scene, entt::entity entity,
42 std::shared_ptr<SceneNode> sceneNode);
43
44 ~PhysicsBodyComponent() override;
45
46 void beginPlay() override;
47 void endPlay() override;
48
49 // =========================================================================
50 // Physics Properties
51 // =========================================================================
52
57 void setMass(float mass);
58
63 [[nodiscard]] float getMass() const { return mass_; }
64
69 void setFriction(float friction);
70
75 [[nodiscard]] float getFriction() const { return friction_; }
76
81 void setRestitution(float restitution);
82
87 [[nodiscard]] float getRestitution() const { return restitution_; }
88
94 void setKinematic(bool kinematic);
95
100 [[nodiscard]] bool isKinematic() const { return isKinematic_; }
101
106 void setStatic(bool isStatic);
107
112 [[nodiscard]] bool isStatic() const { return isStatic_; }
113
114 // =========================================================================
115 // Transform Control
116 // =========================================================================
117
122 void teleport(const glm::vec3& position);
123
129 void teleport(const glm::vec3& position, const glm::quat& rotation);
130
135 void setLinearVelocity(const glm::vec3& velocity);
136
141 [[nodiscard]] glm::vec3 getLinearVelocity() const;
142
147 void setAngularVelocity(const glm::vec3& velocity);
148
153 [[nodiscard]] glm::vec3 getAngularVelocity() const;
154
155 // =========================================================================
156 // Collision Management (called by CollisionComponents)
157 // =========================================================================
158
164 void registerCollision(CollisionComponent* collision);
165
172
177 void markShapeDirty();
178
182 void rebuildIfDirty();
183
188 [[nodiscard]] btRigidBody* getRigidBody() const { return rigidBody_; }
189
190 // =========================================================================
191 // Collision Callbacks
192 // =========================================================================
193
198 void processCollisions();
199
200 private:
202 void createRigidBody();
203 void destroyRigidBody();
204 void updateEcsComponent();
206
207 entt::entity entity_;
208 std::weak_ptr<SceneNode> sceneNode_;
209
210 std::vector<CollisionComponent*> collisions_;
211 btCompoundShape* compoundShape_ = nullptr;
212 btRigidBody* rigidBody_ = nullptr;
213
214 float mass_ = 1.0f;
215 float friction_ = 0.5f;
216 float restitution_ = 0.0f;
217 bool isKinematic_ = false;
218 bool isStatic_ = false;
219 bool shapeDirty_ = true;
220 bool isInitialized_ = false;
221
222 // For collision callbacks - track previous frame's collisions
223 std::set<CollisionComponent*> previousCollisions_;
224 };
225
226} // namespace EngineCore
Base class for collision shape components.
LogicComponent(Scene *owningScene)
void unregisterCollision(CollisionComponent *collision)
Unregisters a collision component from this body. Called by CollisionComponent::endPlay().
void registerCollision(CollisionComponent *collision)
Registers a collision component with this body. Called by CollisionComponent::beginPlay().
void beginPlay() override
Called when the component is added to the scene or the game starts.
void endPlay() override
Called when the component is removed or the game ends.
void setRestitution(float restitution)
Sets the restitution (bounciness).
float getRestitution() const
Gets the restitution.
bool isKinematic() const
Checks if the body is kinematic.
void setFriction(float friction)
Sets the friction coefficient.
void setStatic(bool isStatic)
Sets whether the body is static (immovable).
glm::vec3 getAngularVelocity() const
Gets the angular velocity.
void markShapeDirty()
Marks the collision shape as needing rebuild. Called when a collision component's shape changes.
std::weak_ptr< SceneNode > sceneNode_
std::set< CollisionComponent * > previousCollisions_
bool isStatic() const
Checks if the body is static.
float getFriction() const
Gets the friction coefficient.
void setKinematic(bool kinematic)
Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics.
void setAngularVelocity(const glm::vec3 &velocity)
Sets the angular velocity.
glm::vec3 getLinearVelocity() const
Gets the linear velocity.
void setMass(float mass)
Sets the mass of the rigid body.
void processCollisions()
Processes collision events and dispatches callbacks. Called after physics step.
btRigidBody * getRigidBody() const
Gets the Bullet rigid body.
void setLinearVelocity(const glm::vec3 &velocity)
Sets the linear velocity.
void rebuildIfDirty()
Rebuilds the collision shape if dirty.
void teleport(const glm::vec3 &position)
Teleports the body to a new position.
std::vector< CollisionComponent * > collisions_
PhysicsBodyComponent(Scene *scene, entt::entity entity, std::shared_ptr< SceneNode > sceneNode)
Constructs a PhysicsBodyComponent.
float getMass() const
Gets the mass of the rigid body.
The physics engine manages creating and destroying physics objects for the physics simulation.
Represents a node in the scene graph, containing information about its position, rotation,...
Definition SceneGraph.h:24
A scene is the overarching structure which can spawn actors.
Definition Scene.h:17
Log category system implementation.