Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Engine::Components::PhysicsBody Class Reference

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

#include <PhysicsBodyComponent.h>

Inheritance diagram for Engine::Components::PhysicsBody:
Collaboration diagram for Engine::Components::PhysicsBody:

Public Member Functions

 PhysicsBody (Entities::Scene *scene, entt::entity entity, std::shared_ptr< Entities::SceneNode > sceneNode)
 Constructs a PhysicsBodyComponent.
 ~PhysicsBody () 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 (Components::Collision *collision)
 Registers a collision component with this body. Called by CollisionComponent::beginPlay().
void unregisterCollision (Components::Collision *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.
btDiscreteDynamicsWorld * getDynamicsWorld () const
 Gets the Bullet dynamics world.
void processCollisions ()
 Processes collision events and dispatches callbacks. Called after physics step.
void debugDraw () const
 Draws debug visualization of all collision shapes.
Public Member Functions inherited from Engine::Components::Logic
 Logic (Entities::Scene *owningScene)
virtual ~Logic ()=default
virtual std::string getComponentName () const
 Gets the component type name for debugging/UI.
virtual void tick (double deltaTime)
 Called every frame if ticking is enabled.
bool hasBegunPlay () const
bool canTick () const
 Checks if the component is currently set to tick.
void setCanTick (bool enable)
 Enables or disables ticking for this component.
Entities::EntitygetOwningEntity () const
 Gets the entity this component belongs to.

Static Public Attributes

static constexpr bool IsUnique = true
static constexpr const char * ComponentName = "PhysicsBody"
Static Public Attributes inherited from Engine::Components::Logic
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.
static constexpr const char * ComponentName = "Logic"
 Fallback component type name. Derived classes may override getComponentName() for custom labels.

Private Member Functions

void rebuildCollisionShape ()
void createRigidBody ()
void destroyRigidBody ()
void updateEcsComponent ()
Core::PhysicsEnginegetPhysicsEngine () const

Private Attributes

entt::entity entity_
std::weak_ptr< Entities::SceneNodesceneNode_
std::vector< Components::Collision * > 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< Components::Collision * > previousCollisions_

Additional Inherited Members

Protected Member Functions inherited from Engine::Components::Logic
Entities::ScenegetScene () const
 Gets the scene this component belongs to.
Core::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.

Definition at line 36 of file PhysicsBodyComponent.h.

Constructor & Destructor Documentation

◆ PhysicsBody()

Engine::Components::PhysicsBody::PhysicsBody ( Entities::Scene * scene,
entt::entity entity,
std::shared_ptr< Entities::SceneNode > sceneNode )

Constructs a PhysicsBodyComponent.

Parameters
sceneThe owning scene.
entityThe ECS entity associated with this component.
sceneNodeThe scene node for transform access.

◆ ~PhysicsBody()

Engine::Components::PhysicsBody::~PhysicsBody ( )
override

Member Function Documentation

◆ beginPlay()

void Engine::Components::PhysicsBody::beginPlay ( )
overridevirtual

Called when the component is added to the scene or the game starts.

Reimplemented from Engine::Components::Logic.

◆ createRigidBody()

void Engine::Components::PhysicsBody::createRigidBody ( )
private

◆ debugDraw()

void Engine::Components::PhysicsBody::debugDraw ( ) const

Draws debug visualization of all collision shapes.

Iterates through registered CollisionComponents and calls their debugDraw(). Uses the current world transform from the SceneNode.

◆ destroyRigidBody()

void Engine::Components::PhysicsBody::destroyRigidBody ( )
private

◆ endPlay()

void Engine::Components::PhysicsBody::endPlay ( )
overridevirtual

Called when the component is removed or the game ends.

Reimplemented from Engine::Components::Logic.

◆ getAngularVelocity()

glm::vec3 Engine::Components::PhysicsBody::getAngularVelocity ( ) const
nodiscard

Gets the angular velocity.

Returns
Angular velocity in rad/s.

◆ getDynamicsWorld()

btDiscreteDynamicsWorld * Engine::Components::PhysicsBody::getDynamicsWorld ( ) const
nodiscard

Gets the Bullet dynamics world.

Returns
Pointer to the dynamics world, or nullptr if physics engine unavailable.

◆ getFriction()

float Engine::Components::PhysicsBody::getFriction ( ) const
inlinenodiscard

Gets the friction coefficient.

Returns
Friction value.

Definition at line 81 of file PhysicsBodyComponent.h.

References friction_.

◆ getLinearVelocity()

glm::vec3 Engine::Components::PhysicsBody::getLinearVelocity ( ) const
nodiscard

Gets the linear velocity.

Returns
Velocity in m/s.

◆ getMass()

float Engine::Components::PhysicsBody::getMass ( ) const
inlinenodiscard

Gets the mass of the rigid body.

Returns
Mass in kilograms.

Definition at line 69 of file PhysicsBodyComponent.h.

References mass_.

◆ getPhysicsEngine()

Core::PhysicsEngine * Engine::Components::PhysicsBody::getPhysicsEngine ( ) const
nodiscardprivate

◆ getRestitution()

float Engine::Components::PhysicsBody::getRestitution ( ) const
inlinenodiscard

Gets the restitution.

Returns
Restitution value.

Definition at line 93 of file PhysicsBodyComponent.h.

References restitution_.

◆ getRigidBody()

btRigidBody * Engine::Components::PhysicsBody::getRigidBody ( ) const
inlinenodiscard

Gets the Bullet rigid body.

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

Definition at line 194 of file PhysicsBodyComponent.h.

References rigidBody_.

◆ isKinematic()

bool Engine::Components::PhysicsBody::isKinematic ( ) const
inlinenodiscard

Checks if the body is kinematic.

Returns
True if kinematic.

Definition at line 106 of file PhysicsBodyComponent.h.

References isKinematic_.

◆ isStatic()

bool Engine::Components::PhysicsBody::isStatic ( ) const
inlinenodiscard

Checks if the body is static.

Returns
True if static.

Definition at line 118 of file PhysicsBodyComponent.h.

References isStatic_.

Referenced by setStatic().

Here is the caller graph for this function:

◆ markShapeDirty()

void Engine::Components::PhysicsBody::markShapeDirty ( )

Marks the collision shape as needing rebuild. Called when a collision component's shape changes.

◆ processCollisions()

void Engine::Components::PhysicsBody::processCollisions ( )

Processes collision events and dispatches callbacks. Called after physics step.

◆ rebuildCollisionShape()

void Engine::Components::PhysicsBody::rebuildCollisionShape ( )
private

◆ rebuildIfDirty()

void Engine::Components::PhysicsBody::rebuildIfDirty ( )

Rebuilds the collision shape if dirty.

◆ registerCollision()

void Engine::Components::PhysicsBody::registerCollision ( Components::Collision * collision)

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

Parameters
collisionThe collision component to register.

◆ setAngularVelocity()

void Engine::Components::PhysicsBody::setAngularVelocity ( const glm::vec3 & velocity)

Sets the angular velocity.

Parameters
velocityAngular velocity in rad/s.

◆ setFriction()

void Engine::Components::PhysicsBody::setFriction ( float friction)

Sets the friction coefficient.

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

◆ setKinematic()

void Engine::Components::PhysicsBody::setKinematic ( bool kinematic)

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

Parameters
kinematicTrue for kinematic, false for dynamic.

◆ setLinearVelocity()

void Engine::Components::PhysicsBody::setLinearVelocity ( const glm::vec3 & velocity)

Sets the linear velocity.

Parameters
velocityVelocity in m/s.

◆ setMass()

void Engine::Components::PhysicsBody::setMass ( float mass)

Sets the mass of the rigid body.

Parameters
massMass in kilograms. 0 = static object.

◆ setRestitution()

void Engine::Components::PhysicsBody::setRestitution ( float restitution)

Sets the restitution (bounciness).

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

◆ setStatic()

void Engine::Components::PhysicsBody::setStatic ( bool isStatic)

Sets whether the body is static (immovable).

Parameters
isStaticTrue for static, false for dynamic.

References isStatic().

Here is the call graph for this function:

◆ teleport() [1/2]

void Engine::Components::PhysicsBody::teleport ( const glm::vec3 & position)

Teleports the body to a new position.

Parameters
positionNew world position.

◆ teleport() [2/2]

void Engine::Components::PhysicsBody::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.

◆ unregisterCollision()

void Engine::Components::PhysicsBody::unregisterCollision ( Components::Collision * collision)

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

Parameters
collisionThe collision component to unregister.

◆ updateEcsComponent()

void Engine::Components::PhysicsBody::updateEcsComponent ( )
private

Member Data Documentation

◆ collisions_

std::vector<Components::Collision*> Engine::Components::PhysicsBody::collisions_
private

Definition at line 234 of file PhysicsBodyComponent.h.

◆ ComponentName

const char* Engine::Components::PhysicsBody::ComponentName = "PhysicsBody"
staticconstexpr

Definition at line 39 of file PhysicsBodyComponent.h.

◆ compoundShape_

btCompoundShape* Engine::Components::PhysicsBody::compoundShape_ = nullptr
private

Definition at line 235 of file PhysicsBodyComponent.h.

◆ entity_

entt::entity Engine::Components::PhysicsBody::entity_
private

Definition at line 231 of file PhysicsBodyComponent.h.

◆ friction_

float Engine::Components::PhysicsBody::friction_ = 0.5f
private

Definition at line 239 of file PhysicsBodyComponent.h.

Referenced by getFriction().

◆ isInitialized_

bool Engine::Components::PhysicsBody::isInitialized_ = false
private

Definition at line 244 of file PhysicsBodyComponent.h.

◆ isKinematic_

bool Engine::Components::PhysicsBody::isKinematic_ = false
private

Definition at line 241 of file PhysicsBodyComponent.h.

Referenced by isKinematic().

◆ isStatic_

bool Engine::Components::PhysicsBody::isStatic_ = false
private

Definition at line 242 of file PhysicsBodyComponent.h.

Referenced by isStatic().

◆ IsUnique

bool Engine::Components::PhysicsBody::IsUnique = true
staticconstexpr

Definition at line 38 of file PhysicsBodyComponent.h.

◆ mass_

float Engine::Components::PhysicsBody::mass_ = 1.0f
private

Definition at line 238 of file PhysicsBodyComponent.h.

Referenced by getMass().

◆ previousCollisions_

std::set<Components::Collision*> Engine::Components::PhysicsBody::previousCollisions_
private

Definition at line 247 of file PhysicsBodyComponent.h.

◆ restitution_

float Engine::Components::PhysicsBody::restitution_ = 0.0f
private

Definition at line 240 of file PhysicsBodyComponent.h.

Referenced by getRestitution().

◆ rigidBody_

btRigidBody* Engine::Components::PhysicsBody::rigidBody_ = nullptr
private

Definition at line 236 of file PhysicsBodyComponent.h.

Referenced by getRigidBody().

◆ sceneNode_

std::weak_ptr<Entities::SceneNode> Engine::Components::PhysicsBody::sceneNode_
private

Definition at line 232 of file PhysicsBodyComponent.h.

◆ shapeDirty_

bool Engine::Components::PhysicsBody::shapeDirty_ = true
private

Definition at line 243 of file PhysicsBodyComponent.h.


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