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 SceneNode;
19} // namespace Engine::Entities
20
21class btRigidBody;
22class btCompoundShape;
23class btCollisionShape;
24class btDiscreteDynamicsWorld;
25
26namespace Engine::Components {
27 class Collision;
28
36 class PhysicsBody : public Logic {
37 public:
38 static constexpr bool IsUnique = true;
39 static constexpr const char* ComponentName = "PhysicsBody";
40
47 PhysicsBody(Entities::Scene* scene, entt::entity entity,
48 std::shared_ptr<Entities::SceneNode> sceneNode);
49
50 ~PhysicsBody() override;
51
52 void beginPlay() override;
53 void endPlay() override;
54
55 // =========================================================================
56 // Physics Properties
57 // =========================================================================
58
63 void setMass(float mass);
64
69 [[nodiscard]] float getMass() const { return mass_; }
70
75 void setFriction(float friction);
76
81 [[nodiscard]] float getFriction() const { return friction_; }
82
87 void setRestitution(float restitution);
88
93 [[nodiscard]] float getRestitution() const { return restitution_; }
94
100 void setKinematic(bool kinematic);
101
106 [[nodiscard]] bool isKinematic() const { return isKinematic_; }
107
112 void setStatic(bool isStatic);
113
118 [[nodiscard]] bool isStatic() const { return isStatic_; }
119
120 // =========================================================================
121 // Transform Control
122 // =========================================================================
123
128 void teleport(const glm::vec3& position);
129
135 void teleport(const glm::vec3& position, const glm::quat& rotation);
136
141 void setLinearVelocity(const glm::vec3& velocity);
142
147 [[nodiscard]] glm::vec3 getLinearVelocity() const;
148
153 void setAngularVelocity(const glm::vec3& velocity);
154
159 [[nodiscard]] glm::vec3 getAngularVelocity() const;
160
161 // =========================================================================
162 // Collision Management (called by CollisionComponents)
163 // =========================================================================
164
171
178
184
189
194 [[nodiscard]] btRigidBody* getRigidBody() const { return rigidBody_; }
195
200 [[nodiscard]] btDiscreteDynamicsWorld* getDynamicsWorld() const;
201
202 // =========================================================================
203 // Collision Callbacks
204 // =========================================================================
205
211
212 // =========================================================================
213 // Debug Visualization
214 // =========================================================================
215
222 void debugDraw() const;
223
224 private:
229 [[nodiscard]] Core::PhysicsEngine * getPhysicsEngine() const;
230
231 entt::entity entity_;
232 std::weak_ptr<Entities::SceneNode> sceneNode_;
233
234 std::vector<Components::Collision*> collisions_;
235 btCompoundShape* compoundShape_ = nullptr;
236 btRigidBody* rigidBody_ = nullptr;
237
238 float mass_ = 1.0f;
239 float friction_ = 0.5f;
240 float restitution_ = 0.0f;
241 bool isKinematic_ = false;
242 bool isStatic_ = false;
243 bool shapeDirty_ = true;
244 bool isInitialized_ = false;
245
246 // For collision callbacks - track previous frame's collisions
247 std::set<Components::Collision*> previousCollisions_;
248 };
249
250} // 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
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.
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.
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:56
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9