3#include "Engine/Component/LogicComponent.h"
4#include "Engine/Physics/CollisionProfile.h"
6#include <glm/gtc/quaternion.hpp>
7#include <entt/entt.hpp>
27class btCollisionShape;
28class btDiscreteDynamicsWorld;
53 std::shared_ptr<Entities::SceneNode> sceneNode);
144 void teleport(
const glm::vec3& position,
const glm::quat& rotation);
262 template<
typename ActorType,
typename Callback>
269 auto * actor =
dynamic_cast<ActorType *
>(
event.otherActor );
270 if ( actor !=
nullptr )
272 callback( event, *actor );
283 template<
typename ActorType,
typename Callback>
290 auto * actor =
dynamic_cast<ActorType *
>(
event.otherActor );
291 if ( actor !=
nullptr )
293 callback( event, *actor );
304 template<
typename ActorType,
typename Callback>
311 auto * actor =
dynamic_cast<ActorType *
>(
event.otherActor );
312 if ( actor !=
nullptr )
314 callback( event, *actor );
325 template<
typename ActorType,
typename Callback>
332 auto * actor =
dynamic_cast<ActorType *
>(
event.otherActor );
333 if ( actor !=
nullptr )
335 callback( event, *actor );
346 template<
typename ActorType,
typename Callback>
353 auto * actor =
dynamic_cast<ActorType *
>(
event.otherActor );
354 if ( actor !=
nullptr )
356 callback( event, *actor );
367 template<
typename ActorType,
typename Callback>
374 auto * actor =
dynamic_cast<ActorType *
>(
event.otherActor );
375 if ( actor !=
nullptr )
377 callback( event, *actor );
426 std::vector<BodyCallbackSubscription> & callbacks,
Base class for collision shape components.
Logic(Entities::Scene *owningScene)
Constructs a logic component owned by a scene.
float getRestitution() const
Gets the restitution.
std::vector< BodyCallbackSubscription > overlapStayCallbacks_
PhysicsBodyCallbackHandle addBodyCallback(std::vector< BodyCallbackSubscription > &callbacks, std::function< void(const Physics::CollisionEvent &)> callback)
PhysicsBodyCallbackHandle onCollisionBegin(Callback &&callback)
Registers a callback for blocking collision begin events with actors of ActorType.
PhysicsBodyCallbackHandle onOverlapEnd(Callback &&callback)
Registers a callback for overlap end events with actors of ActorType.
float getMass() const
Gets the mass of the rigid body.
void setRestitution(float restitution)
Sets the restitution (bounciness).
bool collisionFiltersDirty_
void setAngularVelocity(const glm::vec3 &velocity)
Sets the angular velocity.
std::vector< Components::Collision * > collisions_
static constexpr bool IsUnique
Only one physics body may be attached per entity.
float getFriction() const
Gets the friction coefficient.
glm::vec3 getAngularVelocity() const
Gets the angular velocity.
void applyCollisionFlags()
void debugDraw() const
Draws debug visualization of all collision shapes.
std::vector< BodyCallbackSubscription > overlapBeginCallbacks_
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.
void setPhysicsBodyHandle(Physics::PhysicsBodyHandle handle)
Sets the physics-engine registration handle for this body.
bool shouldUseNoContactResponse() const
std::weak_ptr< Entities::SceneNode > sceneNode_
void markCollisionFiltersDirty()
Marks collision filters as needing update on the rigid body.
btCompoundShape * compoundShape_
btDiscreteDynamicsWorld * getDynamicsWorld() const
Gets the Bullet dynamics world.
void setMass(float mass)
Sets the mass of the rigid body.
void rebuildCollisionShape()
std::uint64_t PhysicsBodyCallbackHandle
Opaque handle for registered collision callbacks.
std::vector< BodyCallbackSubscription > overlapEndCallbacks_
std::vector< BodyCallbackSubscription > collisionStayCallbacks_
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.
std::uint16_t getBulletCollisionMask() const
std::vector< BodyCallbackSubscription > collisionEndCallbacks_
Physics::PhysicsBodyHandle getPhysicsBodyHandle() const
Gets the physics-engine registration handle for this body.
glm::vec3 getLinearVelocity() const
Gets the linear velocity.
void rebuildIfDirty()
Rebuilds the collision shape if dirty.
Physics::PhysicsBodyHandle bodyHandle_
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 removeCollisionCallback(PhysicsBodyCallbackHandle handle)
Removes a previously registered collision callback.
~PhysicsBody() override
Destroys the physics body and releases Bullet resources.
void setFriction(float friction)
Sets the friction coefficient.
void dispatchCollisionEvent(const Physics::CollisionEvent &event)
Dispatches a collision event to callbacks matching its type and response.
Core::PhysicsEngine * getPhysicsEngine() const
void updateEcsComponent()
PhysicsBodyCallbackHandle onCollisionEnd(Callback &&callback)
Registers a callback for blocking collision end events with actors of ActorType.
PhysicsBodyCallbackHandle onOverlapBegin(Callback &&callback)
Registers a callback for overlap begin events with actors of ActorType.
const std::vector< Components::Collision * > & getCollisions() const
Gets collision components registered with this body.
void processCollisions()
Processes collision events and dispatches callbacks. Called after physics step.
std::vector< BodyCallbackSubscription > collisionBeginCallbacks_
void setStatic(bool isStatic)
Sets whether the body is static (immovable).
static constexpr const char * ComponentName
Component type name.
bool drawImGui() override
Draws component-specific ImGui inspector controls.
Components::Collision * findCollisionForShape(const btCollisionShape *shape) const
Finds the collision component that owns a Bullet collision shape.
void setKinematic(bool kinematic)
Sets whether the body is kinematic. Kinematic bodies are moved by code, not physics.
PhysicsBodyCallbackHandle nextCallbackHandle_
PhysicsBodyCallbackHandle onCollisionStay(Callback &&callback)
Registers a callback for blocking collision stay events with actors of ActorType.
bool isStatic() const
Checks if the body is static.
Entities::Actor * getHitActor() const
Gets the actor owning this physics body when the owning entity is an actor.
void readdRigidBodyWithCurrentFilters()
std::uint16_t getBulletCollisionGroup() const
btRigidBody * getRigidBody() const
Gets the Bullet rigid body.
std::set< Components::Collision * > previousCollisions_
PhysicsBodyCallbackHandle onOverlapStay(Callback &&callback)
Registers a callback for overlap stay events with actors of ActorType.
Owns the Bullet dynamics world and dispatches engine collision events.
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
Represents a node in the scene graph, containing information about its position, rotation,...
Owns scene entities, actor lifetime, and the scene graph root.
PhysicsBodyCallbackHandle handle
std::function< void(const Physics::CollisionEvent &)> callback
Actor-level collision or overlap event generated by PhysicsEngine.
Runtime handle for a registered physics body component.