|
Vulkan Schnee 0.0.1
High-performance rendering engine
|
Component that manages a physics rigid body for an entity. More...
#include <PhysicsBodyComponent.h>
Public Member Functions | |
| PhysicsBodyComponent (Scene *scene, entt::entity entity, std::shared_ptr< SceneNode > sceneNode) | |
| Constructs a PhysicsBodyComponent. | |
| ~PhysicsBodyComponent () override | |
| 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 | setMass (float mass) |
| Sets the mass of the rigid body. | |
| float | getMass () const |
| Gets the mass of the rigid body. | |
| void | setFriction (float friction) |
| Sets the friction coefficient. | |
| float | getFriction () const |
| Gets the friction coefficient. | |
| void | setRestitution (float restitution) |
| Sets the restitution (bounciness). | |
| float | getRestitution () const |
| Gets the restitution. | |
| void | setKinematic (bool kinematic) |
| Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics. | |
| bool | isKinematic () const |
| Checks if the body is kinematic. | |
| void | setStatic (bool isStatic) |
| Sets whether the body is static (immovable). | |
| bool | isStatic () const |
| Checks if the body is static. | |
| void | teleport (const glm::vec3 &position) |
| Teleports the body to a new position. | |
| void | teleport (const glm::vec3 &position, const glm::quat &rotation) |
| Teleports the body to a new position and rotation. | |
| void | setLinearVelocity (const glm::vec3 &velocity) |
| Sets the linear velocity. | |
| glm::vec3 | getLinearVelocity () const |
| Gets the linear velocity. | |
| void | setAngularVelocity (const glm::vec3 &velocity) |
| Sets the angular velocity. | |
| glm::vec3 | getAngularVelocity () const |
| Gets the angular velocity. | |
| void | registerCollision (CollisionComponent *collision) |
| Registers a collision component with this body. Called by CollisionComponent::beginPlay(). | |
| void | unregisterCollision (CollisionComponent *collision) |
| Unregisters a collision component from this body. Called by CollisionComponent::endPlay(). | |
| void | markShapeDirty () |
| Marks the collision shape as needing rebuild. Called when a collision component's shape changes. | |
| void | rebuildIfDirty () |
| Rebuilds the collision shape if dirty. | |
| btRigidBody * | getRigidBody () const |
| Gets the Bullet rigid body. | |
| void | processCollisions () |
| Processes collision events and dispatches callbacks. Called after physics step. | |
| Public Member Functions inherited from EngineCore::LogicComponent | |
| LogicComponent (Scene *owningScene) | |
| virtual | ~LogicComponent ()=default |
| virtual void | tick (double deltaTime) |
| Called every frame if ticking is enabled. | |
| bool | canTick () const |
| Checks if the component is currently set to tick. | |
| void | setCanTick (bool enable) |
| Enables or disables ticking for this component. | |
| Entity * | getOwningEntity () const |
| Gets the entity this component belongs to. | |
Static Public Attributes | |
| static constexpr bool | IsUnique = true |
| Static Public Attributes inherited from EngineCore::LogicComponent | |
| static constexpr bool | IsUnique = false |
| Defines whether multiple instances of this component can exist on the same entity. Defaults to false (multiple allowed). Override in derived classes with static constexpr bool IsUnique = true; if needed. | |
Private Member Functions | |
| void | rebuildCollisionShape () |
| void | createRigidBody () |
| void | destroyRigidBody () |
| void | updateEcsComponent () |
| PhysicsEngine * | getPhysicsEngine () const |
Private Attributes | |
| entt::entity | entity_ |
| std::weak_ptr< SceneNode > | sceneNode_ |
| std::vector< CollisionComponent * > | collisions_ |
| btCompoundShape * | compoundShape_ = nullptr |
| btRigidBody * | rigidBody_ = nullptr |
| float | mass_ = 1.0f |
| float | friction_ = 0.5f |
| float | restitution_ = 0.0f |
| bool | isKinematic_ = false |
| bool | isStatic_ = false |
| bool | shapeDirty_ = true |
| bool | isInitialized_ = false |
| std::set< CollisionComponent * > | previousCollisions_ |
Additional Inherited Members | |
| Protected Member Functions inherited from EngineCore::LogicComponent | |
| Scene * | getScene () const |
| Gets the scene this component belongs to. | |
| SceneManager * | getSceneManager () const |
| Helper to get the SceneManager from the owning scene. | |
Component that manages a physics rigid body for an entity.
PhysicsBodyComponent is unique per entity and manages the Bullet rigid body. CollisionComponents register with this component to define collision shapes. When multiple collision shapes exist, they are combined into a compound shape.
Definition at line 31 of file PhysicsBodyComponent.h.
| EngineCore::PhysicsBodyComponent::PhysicsBodyComponent | ( | Scene * | scene, |
| entt::entity | entity, | ||
| std::shared_ptr< SceneNode > | sceneNode ) |
Constructs a PhysicsBodyComponent.
| scene | The owning scene. |
| entity | The ECS entity associated with this component. |
| sceneNode | The scene node for transform access. |
Definition at line 18 of file PhysicsBodyComponent.cpp.
References entity_, EngineCore::LogicComponent::LogicComponent(), EngineCore::LogicComponent::scene, and sceneNode_.
Referenced by processCollisions().
|
override |
Definition at line 26 of file PhysicsBodyComponent.cpp.
References compoundShape_, destroyRigidBody(), and rigidBody_.
|
overridevirtual |
Called when the component is added to the scene or the game starts.
Reimplemented from EngineCore::LogicComponent.
Definition at line 37 of file PhysicsBodyComponent.cpp.
References EngineCore::LogicComponent::beginPlay(), collisions_, createRigidBody(), EngineCore::LogicComponent::Entity, EngineCore::Entity::getComponents(), EngineCore::LogicComponent::getOwningEntity(), isInitialized_, rebuildCollisionShape(), and registerCollision().
|
private |
Definition at line 413 of file PhysicsBodyComponent.cpp.
References collisions_, compoundShape_, EngineCore::PhysicsEngine::createRigidBody(), EngineCore::RigidBodyCreateInfo::friction, friction_, getPhysicsEngine(), isKinematic_, isStatic_, EngineCore::RigidBodyCreateInfo::mass, mass_, EngineCore::RigidBodyCreateInfo::restitution, restitution_, rigidBody_, sceneNode_, EngineCore::RigidBodyCreateInfo::shape, EngineCore::RigidBodyCreateInfo::transform, and updateEcsComponent().
Referenced by beginPlay(), and registerCollision().
|
private |
Definition at line 475 of file PhysicsBodyComponent.cpp.
References getPhysicsEngine(), EngineCore::PhysicsEngine::removeRigidBody(), and rigidBody_.
Referenced by endPlay(), unregisterCollision(), and ~PhysicsBodyComponent().
|
overridevirtual |
Called when the component is removed or the game ends.
Reimplemented from EngineCore::LogicComponent.
Definition at line 60 of file PhysicsBodyComponent.cpp.
References collisions_, compoundShape_, destroyRigidBody(), EngineCore::LogicComponent::endPlay(), Ecs::RegistryManager::get(), isInitialized_, Ecs::SimulatesPhysics::rigidBody, and sceneNode_.
|
nodiscard |
Gets the angular velocity.
Definition at line 224 of file PhysicsBodyComponent.cpp.
References glm::getVec3(), and rigidBody_.
|
inlinenodiscard |
Gets the friction coefficient.
Definition at line 75 of file PhysicsBodyComponent.h.
References friction_.
|
nodiscard |
Gets the linear velocity.
Definition at line 210 of file PhysicsBodyComponent.cpp.
References glm::getVec3(), and rigidBody_.
|
inlinenodiscard |
Gets the mass of the rigid body.
Definition at line 63 of file PhysicsBodyComponent.h.
References mass_.
|
private |
Definition at line 509 of file PhysicsBodyComponent.cpp.
References EngineCore::EngineManager::getEngineModule(), EngineCore::EngineManager::getInstance(), and EngineCore::Engine::getPhysicsEngine().
Referenced by createRigidBody(), destroyRigidBody(), processCollisions(), registerCollision(), and unregisterCollision().
|
inlinenodiscard |
Gets the restitution.
Definition at line 87 of file PhysicsBodyComponent.h.
References restitution_.
|
inlinenodiscard |
Gets the Bullet rigid body.
Definition at line 188 of file PhysicsBodyComponent.h.
References rigidBody_.
|
inlinenodiscard |
Checks if the body is kinematic.
Definition at line 100 of file PhysicsBodyComponent.h.
References isKinematic_.
|
inlinenodiscard |
Checks if the body is static.
Definition at line 112 of file PhysicsBodyComponent.h.
References isStatic_.
Referenced by setStatic().
| void EngineCore::PhysicsBodyComponent::markShapeDirty | ( | ) |
Marks the collision shape as needing rebuild. Called when a collision component's shape changes.
Definition at line 293 of file PhysicsBodyComponent.cpp.
References shapeDirty_.
| void EngineCore::PhysicsBodyComponent::processCollisions | ( | ) |
Processes collision events and dispatches callbacks. Called after physics step.
Definition at line 308 of file PhysicsBodyComponent.cpp.
References collisions_, EngineCore::PhysicsEngine::getDynamicsWorld(), getPhysicsEngine(), PhysicsBodyComponent(), previousCollisions_, and rigidBody_.
|
private |
Definition at line 380 of file PhysicsBodyComponent.cpp.
References collisions_, compoundShape_, and shapeDirty_.
Referenced by beginPlay(), rebuildIfDirty(), registerCollision(), and unregisterCollision().
| void EngineCore::PhysicsBodyComponent::rebuildIfDirty | ( | ) |
Rebuilds the collision shape if dirty.
Definition at line 297 of file PhysicsBodyComponent.cpp.
References isInitialized_, rebuildCollisionShape(), and shapeDirty_.
| void EngineCore::PhysicsBodyComponent::registerCollision | ( | CollisionComponent * | collision | ) |
Registers a collision component with this body. Called by CollisionComponent::beginPlay().
| collision | The collision component to register. |
Definition at line 235 of file PhysicsBodyComponent.cpp.
References EngineCore::CollisionComponent::bodyComponent_, collisions_, compoundShape_, createRigidBody(), EngineCore::PhysicsEngine::getDynamicsWorld(), getPhysicsEngine(), isInitialized_, rebuildCollisionShape(), rigidBody_, and shapeDirty_.
Referenced by beginPlay().
| void EngineCore::PhysicsBodyComponent::setAngularVelocity | ( | const glm::vec3 & | velocity | ) |
Sets the angular velocity.
| velocity | Angular velocity in rad/s. |
Definition at line 217 of file PhysicsBodyComponent.cpp.
References bt::getVec3(), and rigidBody_.
| void EngineCore::PhysicsBodyComponent::setFriction | ( | float | friction | ) |
Sets the friction coefficient.
| friction | Friction value (0.0 = ice, 0.5 = wood, 1.0 = high friction). |
Definition at line 111 of file PhysicsBodyComponent.cpp.
References friction_, and rigidBody_.
| void EngineCore::PhysicsBodyComponent::setKinematic | ( | bool | kinematic | ) |
Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics.
| kinematic | True for kinematic, false for dynamic. |
Definition at line 125 of file PhysicsBodyComponent.cpp.
References isKinematic_, rigidBody_, and updateEcsComponent().
| void EngineCore::PhysicsBodyComponent::setLinearVelocity | ( | const glm::vec3 & | velocity | ) |
Sets the linear velocity.
| velocity | Velocity in m/s. |
Definition at line 203 of file PhysicsBodyComponent.cpp.
References bt::getVec3(), and rigidBody_.
| void EngineCore::PhysicsBodyComponent::setMass | ( | float | mass | ) |
Sets the mass of the rigid body.
| mass | Mass in kilograms. 0 = static object. |
Definition at line 99 of file PhysicsBodyComponent.cpp.
References compoundShape_, isStatic_, mass_, and rigidBody_.
| void EngineCore::PhysicsBodyComponent::setRestitution | ( | float | restitution | ) |
Sets the restitution (bounciness).
| restitution | 0.0 = no bounce, 1.0 = full energy preserved. |
Definition at line 118 of file PhysicsBodyComponent.cpp.
References restitution_, and rigidBody_.
| void EngineCore::PhysicsBodyComponent::setStatic | ( | bool | isStatic | ) |
Sets whether the body is static (immovable).
| isStatic | True for static, false for dynamic. |
Definition at line 142 of file PhysicsBodyComponent.cpp.
References isStatic(), isStatic_, rigidBody_, and updateEcsComponent().
| void EngineCore::PhysicsBodyComponent::teleport | ( | const glm::vec3 & | position | ) |
Teleports the body to a new position.
| position | New world position. |
Definition at line 160 of file PhysicsBodyComponent.cpp.
References sceneNode_, and teleport().
Referenced by teleport().
| void EngineCore::PhysicsBodyComponent::teleport | ( | const glm::vec3 & | position, |
| const glm::quat & | rotation ) |
Teleports the body to a new position and rotation.
| position | New world position. |
| rotation | New world rotation. |
Definition at line 170 of file PhysicsBodyComponent.cpp.
References Ecs::RegistryManager::get(), bt::getQuat(), bt::getVec3(), rigidBody_, and sceneNode_.
| void EngineCore::PhysicsBodyComponent::unregisterCollision | ( | CollisionComponent * | collision | ) |
Unregisters a collision component from this body. Called by CollisionComponent::endPlay().
| collision | The collision component to unregister. |
Definition at line 264 of file PhysicsBodyComponent.cpp.
References EngineCore::CollisionComponent::bodyComponent_, collisions_, compoundShape_, destroyRigidBody(), EngineCore::PhysicsEngine::getDynamicsWorld(), getPhysicsEngine(), isInitialized_, rebuildCollisionShape(), rigidBody_, and shapeDirty_.
|
private |
Definition at line 489 of file PhysicsBodyComponent.cpp.
References compoundShape_, Ecs::RegistryManager::get(), isKinematic_, isStatic_, Ecs::SimulatesPhysics::rigidBody, rigidBody_, and sceneNode_.
Referenced by createRigidBody(), setKinematic(), and setStatic().
|
private |
Definition at line 210 of file PhysicsBodyComponent.h.
Referenced by beginPlay(), createRigidBody(), endPlay(), processCollisions(), rebuildCollisionShape(), registerCollision(), and unregisterCollision().
|
private |
Definition at line 211 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), endPlay(), rebuildCollisionShape(), registerCollision(), setMass(), unregisterCollision(), updateEcsComponent(), and ~PhysicsBodyComponent().
|
private |
Definition at line 207 of file PhysicsBodyComponent.h.
Referenced by PhysicsBodyComponent().
|
private |
Definition at line 215 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), getFriction(), and setFriction().
|
private |
Definition at line 220 of file PhysicsBodyComponent.h.
Referenced by beginPlay(), endPlay(), rebuildIfDirty(), registerCollision(), and unregisterCollision().
|
private |
Definition at line 217 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), isKinematic(), setKinematic(), and updateEcsComponent().
|
private |
Definition at line 218 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), isStatic(), setMass(), setStatic(), and updateEcsComponent().
|
staticconstexpr |
Definition at line 33 of file PhysicsBodyComponent.h.
|
private |
Definition at line 214 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), getMass(), and setMass().
|
private |
Definition at line 223 of file PhysicsBodyComponent.h.
Referenced by processCollisions().
|
private |
Definition at line 216 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), getRestitution(), and setRestitution().
|
private |
Definition at line 212 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), destroyRigidBody(), getAngularVelocity(), getLinearVelocity(), getRigidBody(), processCollisions(), registerCollision(), setAngularVelocity(), setFriction(), setKinematic(), setLinearVelocity(), setMass(), setRestitution(), setStatic(), teleport(), unregisterCollision(), updateEcsComponent(), and ~PhysicsBodyComponent().
|
private |
Definition at line 208 of file PhysicsBodyComponent.h.
Referenced by createRigidBody(), endPlay(), PhysicsBodyComponent(), teleport(), teleport(), and updateEcsComponent().
|
private |
Definition at line 219 of file PhysicsBodyComponent.h.
Referenced by markShapeDirty(), rebuildCollisionShape(), rebuildIfDirty(), registerCollision(), and unregisterCollision().