|
Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
|
Component that manages a physics rigid body for an entity. More...
#include <PhysicsBodyComponent.h>
Classes | |
| struct | BodyCallbackSubscription |
Public Types | |
| using | PhysicsBodyCallbackHandle = std::uint64_t |
| Opaque handle for registered collision callbacks. | |
Public Member Functions | |
| PhysicsBody (Entities::Scene *scene, entt::entity entity, std::shared_ptr< Entities::SceneNode > sceneNode) | |
| Constructs a PhysicsBodyComponent. | |
| ~PhysicsBody () override | |
| Destroys the physics body and releases Bullet resources. | |
| 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. | |
| bool | drawImGui () override |
| Draws component-specific ImGui inspector controls. | |
| 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 | markCollisionFiltersDirty () |
| Marks collision filters as needing update on the rigid body. | |
| void | rebuildIfDirty () |
| Rebuilds the collision shape if dirty. | |
| btRigidBody * | getRigidBody () const |
| Gets the Bullet rigid body. | |
| Entities::Actor * | getHitActor () const |
| Gets the actor owning this physics body when the owning entity is an actor. | |
| Physics::PhysicsBodyHandle | getPhysicsBodyHandle () const |
| Gets the physics-engine registration handle for this body. | |
| void | setPhysicsBodyHandle (Physics::PhysicsBodyHandle handle) |
| Sets the physics-engine registration handle for this body. | |
| const std::vector< Components::Collision * > & | getCollisions () const |
| Gets collision components registered with this body. | |
| Components::Collision * | findCollisionForShape (const btCollisionShape *shape) const |
| Finds the collision component that owns a Bullet collision shape. | |
| btDiscreteDynamicsWorld * | getDynamicsWorld () const |
| Gets the Bullet dynamics world. | |
| void | processCollisions () |
| Processes collision events and dispatches callbacks. Called after physics step. | |
| template<typename ActorType, typename Callback> | |
| PhysicsBodyCallbackHandle | onOverlapBegin (Callback &&callback) |
| Registers a callback for overlap begin events with actors of ActorType. | |
| template<typename ActorType, typename Callback> | |
| PhysicsBodyCallbackHandle | onOverlapStay (Callback &&callback) |
| Registers a callback for overlap stay events with actors of ActorType. | |
| template<typename ActorType, typename Callback> | |
| PhysicsBodyCallbackHandle | onOverlapEnd (Callback &&callback) |
| Registers a callback for overlap end events with actors of ActorType. | |
| template<typename ActorType, typename Callback> | |
| PhysicsBodyCallbackHandle | onCollisionBegin (Callback &&callback) |
| Registers a callback for blocking collision begin events with actors of ActorType. | |
| template<typename ActorType, typename Callback> | |
| PhysicsBodyCallbackHandle | onCollisionStay (Callback &&callback) |
| Registers a callback for blocking collision stay events with actors of ActorType. | |
| template<typename ActorType, typename Callback> | |
| PhysicsBodyCallbackHandle | onCollisionEnd (Callback &&callback) |
| Registers a callback for blocking collision end events with actors of ActorType. | |
| void | removeCollisionCallback (PhysicsBodyCallbackHandle handle) |
| Removes a previously registered collision callback. | |
| void | dispatchCollisionEvent (const Physics::CollisionEvent &event) |
| Dispatches a collision event to callbacks matching its type and response. | |
| void | debugDraw () const |
| Draws debug visualization of all collision shapes. | |
| Public Member Functions inherited from Engine::Components::Logic | |
| Logic (Entities::Scene *owningScene) | |
| Constructs a logic component owned by a scene. | |
| Logic (const Entities::SpawnContext &context) | |
| Constructs a logic component from spawn context data. | |
| virtual | ~Logic ()=default |
| Destroys the logic component. | |
| virtual std::string | getComponentName () const |
| Gets the component type name for debugging/UI. | |
| virtual bool | drawImGuiName () |
| Used to alter the name of the entity in the outliner. | |
| virtual void | tick (double deltaTime) |
| Called every frame if ticking is enabled. | |
| bool | hasBegunPlay () const |
| Checks whether beginPlay() has been called without a matching endPlay(). | |
| bool | canTick () const |
| Checks if the component is currently set to tick. | |
| void | setCanTick (bool enable) |
| Enables or disables ticking for this component. | |
| Entities::Entity * | getOwningEntity () const |
| Gets the entity this component belongs to. | |
Static Public Attributes | |
| static constexpr bool | IsUnique = true |
| Only one physics body may be attached per entity. | |
| static constexpr const char * | ComponentName = "PhysicsBody" |
| Component type name. | |
| 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 () |
| void | applyCollisionFlags () |
| void | readdRigidBodyWithCurrentFilters () |
| std::uint16_t | getBulletCollisionGroup () const |
| std::uint16_t | getBulletCollisionMask () const |
| bool | shouldUseNoContactResponse () const |
| Core::PhysicsEngine * | getPhysicsEngine () const |
| PhysicsBodyCallbackHandle | addBodyCallback (std::vector< BodyCallbackSubscription > &callbacks, std::function< void(const Physics::CollisionEvent &)> callback) |
| void | clearCallbacks () |
Private Attributes | |
| entt::entity | entity_ |
| std::weak_ptr< Entities::SceneNode > | sceneNode_ |
| 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 |
| bool | collisionFiltersDirty_ = true |
| Physics::PhysicsBodyHandle | bodyHandle_ |
| PhysicsBodyCallbackHandle | nextCallbackHandle_ = 1 |
| std::vector< BodyCallbackSubscription > | overlapBeginCallbacks_ |
| std::vector< BodyCallbackSubscription > | overlapStayCallbacks_ |
| std::vector< BodyCallbackSubscription > | overlapEndCallbacks_ |
| std::vector< BodyCallbackSubscription > | collisionBeginCallbacks_ |
| std::vector< BodyCallbackSubscription > | collisionStayCallbacks_ |
| std::vector< BodyCallbackSubscription > | collisionEndCallbacks_ |
| std::set< Components::Collision * > | previousCollisions_ |
Additional Inherited Members | |
| Protected Member Functions inherited from Engine::Components::Logic | |
| Entities::Scene * | getScene () const |
| Gets the scene this component belongs to. | |
| Core::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 40 of file PhysicsBodyComponent.h.
| using Engine::Components::PhysicsBody::PhysicsBodyCallbackHandle = std::uint64_t |
Opaque handle for registered collision callbacks.
Definition at line 44 of file PhysicsBodyComponent.h.
| Engine::Components::PhysicsBody::PhysicsBody | ( | Entities::Scene * | scene, |
| entt::entity | entity, | ||
| std::shared_ptr< Entities::SceneNode > | sceneNode ) |
Constructs a PhysicsBodyComponent.
| scene | The owning scene. |
| entity | The ECS entity associated with this component. |
| sceneNode | The scene node for transform access. |
|
override |
Destroys the physics body and releases Bullet resources.
|
private |
Referenced by onCollisionBegin(), onCollisionEnd(), onCollisionStay(), onOverlapBegin(), onOverlapEnd(), and onOverlapStay().
|
private |
|
overridevirtual |
Called when the component is added to the scene or the game starts.
Reimplemented from Engine::Components::Logic.
|
private |
|
private |
| 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.
|
private |
| void Engine::Components::PhysicsBody::dispatchCollisionEvent | ( | const Physics::CollisionEvent & | event | ) |
Dispatches a collision event to callbacks matching its type and response.
| event | Collision event to dispatch. |
|
overridevirtual |
Draws component-specific ImGui inspector controls.
Reimplemented from Engine::Components::Logic.
|
overridevirtual |
Called when the component is removed or the game ends.
Reimplemented from Engine::Components::Logic.
|
nodiscard |
Finds the collision component that owns a Bullet collision shape.
| shape | Bullet collision shape to look up. |
|
nodiscard |
Gets the angular velocity.
|
nodiscardprivate |
|
nodiscardprivate |
|
inlinenodiscard |
Gets collision components registered with this body.
Definition at line 232 of file PhysicsBodyComponent.h.
References collisions_.
|
nodiscard |
Gets the Bullet dynamics world.
|
inlinenodiscard |
Gets the friction coefficient.
Definition at line 90 of file PhysicsBodyComponent.h.
References friction_.
|
nodiscard |
Gets the actor owning this physics body when the owning entity is an actor.
Referenced by Engine::Components::Collision::onOverlapBegin(), and Engine::Components::Collision::onOverlapEnd().
|
nodiscard |
Gets the linear velocity.
|
inlinenodiscard |
Gets the mass of the rigid body.
Definition at line 78 of file PhysicsBodyComponent.h.
References mass_.
|
inlinenodiscard |
Gets the physics-engine registration handle for this body.
Definition at line 220 of file PhysicsBodyComponent.h.
References bodyHandle_.
|
nodiscardprivate |
|
inlinenodiscard |
Gets the restitution.
Definition at line 102 of file PhysicsBodyComponent.h.
References restitution_.
|
inlinenodiscard |
Gets the Bullet rigid body.
Definition at line 208 of file PhysicsBodyComponent.h.
References rigidBody_.
|
inlinenodiscard |
Checks if the body is kinematic.
Definition at line 115 of file PhysicsBodyComponent.h.
References isKinematic_.
|
inlinenodiscard |
Checks if the body is static.
Definition at line 127 of file PhysicsBodyComponent.h.
References isStatic_.
Referenced by setStatic().
| void Engine::Components::PhysicsBody::markCollisionFiltersDirty | ( | ) |
Marks collision filters as needing update on the rigid body.
| void Engine::Components::PhysicsBody::markShapeDirty | ( | ) |
Marks the collision shape as needing rebuild. Called when a collision component's shape changes.
|
inline |
Registers a callback for blocking collision begin events with actors of ActorType.
| callback | Callable invoked with the collision event and matched actor. |
Definition at line 326 of file PhysicsBodyComponent.h.
References addBodyCallback(), and collisionBeginCallbacks_.
|
inline |
Registers a callback for blocking collision end events with actors of ActorType.
| callback | Callable invoked with the collision event and matched actor. |
Definition at line 368 of file PhysicsBodyComponent.h.
References addBodyCallback(), and collisionEndCallbacks_.
|
inline |
Registers a callback for blocking collision stay events with actors of ActorType.
| callback | Callable invoked with the collision event and matched actor. |
Definition at line 347 of file PhysicsBodyComponent.h.
References addBodyCallback(), and collisionStayCallbacks_.
|
inline |
Registers a callback for overlap begin events with actors of ActorType.
| callback | Callable invoked with the collision event and matched actor. |
Definition at line 263 of file PhysicsBodyComponent.h.
References addBodyCallback(), and overlapBeginCallbacks_.
|
inline |
Registers a callback for overlap end events with actors of ActorType.
| callback | Callable invoked with the collision event and matched actor. |
Definition at line 305 of file PhysicsBodyComponent.h.
References addBodyCallback(), and overlapEndCallbacks_.
|
inline |
Registers a callback for overlap stay events with actors of ActorType.
| callback | Callable invoked with the collision event and matched actor. |
Definition at line 284 of file PhysicsBodyComponent.h.
References addBodyCallback(), and overlapStayCallbacks_.
| void Engine::Components::PhysicsBody::processCollisions | ( | ) |
Processes collision events and dispatches callbacks. Called after physics step.
|
private |
|
private |
| void Engine::Components::PhysicsBody::rebuildIfDirty | ( | ) |
Rebuilds the collision shape if dirty.
| void Engine::Components::PhysicsBody::registerCollision | ( | Components::Collision * | collision | ) |
Registers a collision component with this body. Called by CollisionComponent::beginPlay().
| collision | The collision component to register. |
| void Engine::Components::PhysicsBody::removeCollisionCallback | ( | PhysicsBodyCallbackHandle | handle | ) |
Removes a previously registered collision callback.
| handle | Callback handle returned from a registration method. |
| void Engine::Components::PhysicsBody::setAngularVelocity | ( | const glm::vec3 & | velocity | ) |
Sets the angular velocity.
| velocity | Angular velocity in rad/s. |
| void Engine::Components::PhysicsBody::setFriction | ( | float | friction | ) |
Sets the friction coefficient.
| friction | Friction value (0.0 = ice, 0.5 = wood, 1.0 = high friction). |
| void Engine::Components::PhysicsBody::setKinematic | ( | bool | kinematic | ) |
Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics.
| kinematic | True for kinematic, false for dynamic. |
| void Engine::Components::PhysicsBody::setLinearVelocity | ( | const glm::vec3 & | velocity | ) |
Sets the linear velocity.
| velocity | Velocity in m/s. |
| void Engine::Components::PhysicsBody::setMass | ( | float | mass | ) |
Sets the mass of the rigid body.
| mass | Mass in kilograms. 0 = static object. |
|
inline |
Sets the physics-engine registration handle for this body.
| handle | New physics body handle. |
Definition at line 226 of file PhysicsBodyComponent.h.
References bodyHandle_.
| void Engine::Components::PhysicsBody::setRestitution | ( | float | restitution | ) |
Sets the restitution (bounciness).
| restitution | 0.0 = no bounce, 1.0 = full energy preserved. |
| void Engine::Components::PhysicsBody::setStatic | ( | bool | isStatic | ) |
Sets whether the body is static (immovable).
| isStatic | True for static, false for dynamic. |
References isStatic().
|
nodiscardprivate |
| void Engine::Components::PhysicsBody::teleport | ( | const glm::vec3 & | position | ) |
Teleports the body to a new position.
| position | New world position. |
| void Engine::Components::PhysicsBody::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. |
| void Engine::Components::PhysicsBody::unregisterCollision | ( | Components::Collision * | collision | ) |
Unregisters a collision component from this body. Called by CollisionComponent::endPlay().
| collision | The collision component to unregister. |
|
private |
|
private |
Definition at line 446 of file PhysicsBodyComponent.h.
Referenced by getPhysicsBodyHandle(), and setPhysicsBodyHandle().
|
private |
Definition at line 451 of file PhysicsBodyComponent.h.
Referenced by onCollisionBegin().
|
private |
Definition at line 453 of file PhysicsBodyComponent.h.
Referenced by onCollisionEnd().
|
private |
Definition at line 445 of file PhysicsBodyComponent.h.
|
private |
Definition at line 434 of file PhysicsBodyComponent.h.
Referenced by getCollisions().
|
private |
Definition at line 452 of file PhysicsBodyComponent.h.
Referenced by onCollisionStay().
|
staticconstexpr |
Component type name.
Definition at line 43 of file PhysicsBodyComponent.h.
|
private |
Definition at line 435 of file PhysicsBodyComponent.h.
|
private |
Definition at line 431 of file PhysicsBodyComponent.h.
|
private |
Definition at line 439 of file PhysicsBodyComponent.h.
Referenced by getFriction().
|
private |
Definition at line 444 of file PhysicsBodyComponent.h.
|
private |
Definition at line 441 of file PhysicsBodyComponent.h.
Referenced by isKinematic().
|
private |
Definition at line 442 of file PhysicsBodyComponent.h.
Referenced by isStatic().
|
staticconstexpr |
Only one physics body may be attached per entity.
Definition at line 42 of file PhysicsBodyComponent.h.
|
private |
Definition at line 438 of file PhysicsBodyComponent.h.
Referenced by getMass().
|
private |
Definition at line 447 of file PhysicsBodyComponent.h.
|
private |
Definition at line 448 of file PhysicsBodyComponent.h.
Referenced by onOverlapBegin().
|
private |
Definition at line 450 of file PhysicsBodyComponent.h.
Referenced by onOverlapEnd().
|
private |
Definition at line 449 of file PhysicsBodyComponent.h.
Referenced by onOverlapStay().
|
private |
Definition at line 456 of file PhysicsBodyComponent.h.
|
private |
Definition at line 440 of file PhysicsBodyComponent.h.
Referenced by getRestitution().
|
private |
Definition at line 436 of file PhysicsBodyComponent.h.
Referenced by getRigidBody().
|
private |
Definition at line 432 of file PhysicsBodyComponent.h.
|
private |
Definition at line 443 of file PhysicsBodyComponent.h.