Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
EngineCore::PhysicsBodyComponent Class Reference

Component that manages a physics rigid body for an entity. More...

#include <PhysicsBodyComponent.h>

Inheritance diagram for EngineCore::PhysicsBodyComponent:
Collaboration diagram for EngineCore::PhysicsBodyComponent:

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.
EntitygetOwningEntity () 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 ()
PhysicsEnginegetPhysicsEngine () const

Private Attributes

entt::entity entity_
std::weak_ptr< SceneNodesceneNode_
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
ScenegetScene () const
 Gets the scene this component belongs to.
SceneManagergetSceneManager () const
 Helper to get the SceneManager from the owning scene.

Detailed Description

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.

Author
Konstantin Passig
Date
2026-01-16

Definition at line 31 of file PhysicsBodyComponent.h.

Constructor & Destructor Documentation

◆ PhysicsBodyComponent()

EngineCore::PhysicsBodyComponent::PhysicsBodyComponent ( Scene * scene,
entt::entity entity,
std::shared_ptr< SceneNode > sceneNode )

Constructs a PhysicsBodyComponent.

Parameters
sceneThe owning scene.
entityThe ECS entity associated with this component.
sceneNodeThe 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~PhysicsBodyComponent()

EngineCore::PhysicsBodyComponent::~PhysicsBodyComponent ( )
override

Definition at line 26 of file PhysicsBodyComponent.cpp.

References compoundShape_, destroyRigidBody(), and rigidBody_.

Here is the call graph for this function:

Member Function Documentation

◆ beginPlay()

void EngineCore::PhysicsBodyComponent::beginPlay ( )
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().

Here is the call graph for this function:

◆ createRigidBody()

void EngineCore::PhysicsBodyComponent::createRigidBody ( )
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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroyRigidBody()

void EngineCore::PhysicsBodyComponent::destroyRigidBody ( )
private

Definition at line 475 of file PhysicsBodyComponent.cpp.

References getPhysicsEngine(), EngineCore::PhysicsEngine::removeRigidBody(), and rigidBody_.

Referenced by endPlay(), unregisterCollision(), and ~PhysicsBodyComponent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endPlay()

void EngineCore::PhysicsBodyComponent::endPlay ( )
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_.

Here is the call graph for this function:

◆ getAngularVelocity()

glm::vec3 EngineCore::PhysicsBodyComponent::getAngularVelocity ( ) const
nodiscard

Gets the angular velocity.

Returns
Angular velocity in rad/s.

Definition at line 224 of file PhysicsBodyComponent.cpp.

References glm::getVec3(), and rigidBody_.

Here is the call graph for this function:

◆ getFriction()

float EngineCore::PhysicsBodyComponent::getFriction ( ) const
inlinenodiscard

Gets the friction coefficient.

Returns
Friction value.

Definition at line 75 of file PhysicsBodyComponent.h.

References friction_.

◆ getLinearVelocity()

glm::vec3 EngineCore::PhysicsBodyComponent::getLinearVelocity ( ) const
nodiscard

Gets the linear velocity.

Returns
Velocity in m/s.

Definition at line 210 of file PhysicsBodyComponent.cpp.

References glm::getVec3(), and rigidBody_.

Here is the call graph for this function:

◆ getMass()

float EngineCore::PhysicsBodyComponent::getMass ( ) const
inlinenodiscard

Gets the mass of the rigid body.

Returns
Mass in kilograms.

Definition at line 63 of file PhysicsBodyComponent.h.

References mass_.

◆ getPhysicsEngine()

PhysicsEngine * EngineCore::PhysicsBodyComponent::getPhysicsEngine ( ) const
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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getRestitution()

float EngineCore::PhysicsBodyComponent::getRestitution ( ) const
inlinenodiscard

Gets the restitution.

Returns
Restitution value.

Definition at line 87 of file PhysicsBodyComponent.h.

References restitution_.

◆ getRigidBody()

btRigidBody * EngineCore::PhysicsBodyComponent::getRigidBody ( ) const
inlinenodiscard

Gets the Bullet rigid body.

Returns
Pointer to the rigid body, or nullptr if not created.

Definition at line 188 of file PhysicsBodyComponent.h.

References rigidBody_.

◆ isKinematic()

bool EngineCore::PhysicsBodyComponent::isKinematic ( ) const
inlinenodiscard

Checks if the body is kinematic.

Returns
True if kinematic.

Definition at line 100 of file PhysicsBodyComponent.h.

References isKinematic_.

◆ isStatic()

bool EngineCore::PhysicsBodyComponent::isStatic ( ) const
inlinenodiscard

Checks if the body is static.

Returns
True if static.

Definition at line 112 of file PhysicsBodyComponent.h.

References isStatic_.

Referenced by setStatic().

Here is the caller graph for this function:

◆ markShapeDirty()

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_.

◆ processCollisions()

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_.

Here is the call graph for this function:

◆ rebuildCollisionShape()

void EngineCore::PhysicsBodyComponent::rebuildCollisionShape ( )
private

Definition at line 380 of file PhysicsBodyComponent.cpp.

References collisions_, compoundShape_, and shapeDirty_.

Referenced by beginPlay(), rebuildIfDirty(), registerCollision(), and unregisterCollision().

Here is the caller graph for this function:

◆ rebuildIfDirty()

void EngineCore::PhysicsBodyComponent::rebuildIfDirty ( )

Rebuilds the collision shape if dirty.

Definition at line 297 of file PhysicsBodyComponent.cpp.

References isInitialized_, rebuildCollisionShape(), and shapeDirty_.

Here is the call graph for this function:

◆ registerCollision()

void EngineCore::PhysicsBodyComponent::registerCollision ( CollisionComponent * collision)

Registers a collision component with this body. Called by CollisionComponent::beginPlay().

Parameters
collisionThe 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setAngularVelocity()

void EngineCore::PhysicsBodyComponent::setAngularVelocity ( const glm::vec3 & velocity)

Sets the angular velocity.

Parameters
velocityAngular velocity in rad/s.

Definition at line 217 of file PhysicsBodyComponent.cpp.

References bt::getVec3(), and rigidBody_.

Here is the call graph for this function:

◆ setFriction()

void EngineCore::PhysicsBodyComponent::setFriction ( float friction)

Sets the friction coefficient.

Parameters
frictionFriction value (0.0 = ice, 0.5 = wood, 1.0 = high friction).

Definition at line 111 of file PhysicsBodyComponent.cpp.

References friction_, and rigidBody_.

◆ setKinematic()

void EngineCore::PhysicsBodyComponent::setKinematic ( bool kinematic)

Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics.

Parameters
kinematicTrue for kinematic, false for dynamic.

Definition at line 125 of file PhysicsBodyComponent.cpp.

References isKinematic_, rigidBody_, and updateEcsComponent().

Here is the call graph for this function:

◆ setLinearVelocity()

void EngineCore::PhysicsBodyComponent::setLinearVelocity ( const glm::vec3 & velocity)

Sets the linear velocity.

Parameters
velocityVelocity in m/s.

Definition at line 203 of file PhysicsBodyComponent.cpp.

References bt::getVec3(), and rigidBody_.

Here is the call graph for this function:

◆ setMass()

void EngineCore::PhysicsBodyComponent::setMass ( float mass)

Sets the mass of the rigid body.

Parameters
massMass in kilograms. 0 = static object.

Definition at line 99 of file PhysicsBodyComponent.cpp.

References compoundShape_, isStatic_, mass_, and rigidBody_.

◆ setRestitution()

void EngineCore::PhysicsBodyComponent::setRestitution ( float restitution)

Sets the restitution (bounciness).

Parameters
restitution0.0 = no bounce, 1.0 = full energy preserved.

Definition at line 118 of file PhysicsBodyComponent.cpp.

References restitution_, and rigidBody_.

◆ setStatic()

void EngineCore::PhysicsBodyComponent::setStatic ( bool isStatic)

Sets whether the body is static (immovable).

Parameters
isStaticTrue for static, false for dynamic.

Definition at line 142 of file PhysicsBodyComponent.cpp.

References isStatic(), isStatic_, rigidBody_, and updateEcsComponent().

Here is the call graph for this function:

◆ teleport() [1/2]

void EngineCore::PhysicsBodyComponent::teleport ( const glm::vec3 & position)

Teleports the body to a new position.

Parameters
positionNew world position.

Definition at line 160 of file PhysicsBodyComponent.cpp.

References sceneNode_, and teleport().

Referenced by teleport().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ teleport() [2/2]

void EngineCore::PhysicsBodyComponent::teleport ( const glm::vec3 & position,
const glm::quat & rotation )

Teleports the body to a new position and rotation.

Parameters
positionNew world position.
rotationNew world rotation.

Definition at line 170 of file PhysicsBodyComponent.cpp.

References Ecs::RegistryManager::get(), bt::getQuat(), bt::getVec3(), rigidBody_, and sceneNode_.

Here is the call graph for this function:

◆ unregisterCollision()

void EngineCore::PhysicsBodyComponent::unregisterCollision ( CollisionComponent * collision)

Unregisters a collision component from this body. Called by CollisionComponent::endPlay().

Parameters
collisionThe 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_.

Here is the call graph for this function:

◆ updateEcsComponent()

void EngineCore::PhysicsBodyComponent::updateEcsComponent ( )
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().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ collisions_

std::vector<CollisionComponent*> EngineCore::PhysicsBodyComponent::collisions_
private

◆ compoundShape_

btCompoundShape* EngineCore::PhysicsBodyComponent::compoundShape_ = nullptr
private

◆ entity_

entt::entity EngineCore::PhysicsBodyComponent::entity_
private

Definition at line 207 of file PhysicsBodyComponent.h.

Referenced by PhysicsBodyComponent().

◆ friction_

float EngineCore::PhysicsBodyComponent::friction_ = 0.5f
private

Definition at line 215 of file PhysicsBodyComponent.h.

Referenced by createRigidBody(), getFriction(), and setFriction().

◆ isInitialized_

bool EngineCore::PhysicsBodyComponent::isInitialized_ = false
private

◆ isKinematic_

bool EngineCore::PhysicsBodyComponent::isKinematic_ = false
private

◆ isStatic_

bool EngineCore::PhysicsBodyComponent::isStatic_ = false
private

◆ IsUnique

bool EngineCore::PhysicsBodyComponent::IsUnique = true
staticconstexpr

Definition at line 33 of file PhysicsBodyComponent.h.

◆ mass_

float EngineCore::PhysicsBodyComponent::mass_ = 1.0f
private

Definition at line 214 of file PhysicsBodyComponent.h.

Referenced by createRigidBody(), getMass(), and setMass().

◆ previousCollisions_

std::set<CollisionComponent*> EngineCore::PhysicsBodyComponent::previousCollisions_
private

Definition at line 223 of file PhysicsBodyComponent.h.

Referenced by processCollisions().

◆ restitution_

float EngineCore::PhysicsBodyComponent::restitution_ = 0.0f
private

Definition at line 216 of file PhysicsBodyComponent.h.

Referenced by createRigidBody(), getRestitution(), and setRestitution().

◆ rigidBody_

◆ sceneNode_

std::weak_ptr<SceneNode> EngineCore::PhysicsBodyComponent::sceneNode_
private

◆ shapeDirty_

bool EngineCore::PhysicsBodyComponent::shapeDirty_ = true
private

The documentation for this class was generated from the following files: