3#include <btBulletDynamicsCommon.h>
4#include "Engine/Physics/CollisionProfile.h"
5#include <glm/mat4x4.hpp>
9#include <unordered_map>
65 btCollisionShape*
shape =
nullptr;
204 const glm::vec3& origin,
205 const glm::vec3& direction,
225 const glm::vec3& origin,
226 const glm::vec3& direction,
245 const glm::vec3& from,
249 const btCollisionObject* ignoredObject =
nullptr
268 const glm::vec3& from,
273 const btCollisionObject* ignoredObject =
nullptr
289 const glm::vec3& position,
292 const btCollisionObject* ignoredObject =
nullptr
310 const glm::vec3& position,
314 const btCollisionObject* ignoredObject =
nullptr
345 static btCollisionShape *
createPlaneShape(
const glm::vec3& surfaceNormal,
float distanceFromOrigin);
366 const std::vector<glm::vec3>& vertices,
367 const std::vector<uint32_t>& indices,
368 btTriangleMesh*& triangleMeshOut);
402 std::uint16_t collisionGroup,
403 std::uint16_t collisionMask
Base class for collision shape components.
Component that manages a physics rigid body for an entity.
PhysicsEngine(Physics::CollisionProfileRegistry *collisionProfileRegistry)
Creates a physics engine with an explicit collision profile registry.
Components::PhysicsBody * resolve(Physics::PhysicsBodyHandle handle) const
Resolves a runtime body handle to a physics body component.
bool raycastClosest(const glm::vec3 &origin, const glm::vec3 &direction, float maxDistance, Physics::RaycastHit &outHit) const
Casts a ray through the physics world and returns the closest hit, if any.
PhysicsEngine()
Creates a physics engine using the active collision profile registry.
btCollisionDispatcher * dispatcher
void unregisterCollision(Physics::CollisionHandle handle)
Unregisters a collision handle and queues End events for active pairs that reference it.
void dispatchQueuedCollisionEvents()
Dispatches collision events queued by the last PhysicsEngine::update() call.
Physics::CollisionHandle registerCollision(Components::Collision *collision)
Registers a collision component for handle-based lookup.
std::vector< RuntimeCollisionSlot > collisionSlots_
std::vector< PendingActorEvent > pendingActorEvents_
void debugDrawAll() const
Draws debug visualization of all collision shapes in the world.
btConstraintSolver * solver
btRigidBody * createRigidBody(const Physics::RigidBodyCreateInfo &info) const
Creates and adds a rigid body to the physics world with default Bullet collision filters.
static btCollisionShape * createConvexHullShape(const std::vector< glm::vec3 > &vertices)
Creates a convex hull collision shape from vertex positions.
btDiscreteDynamicsWorld * getDynamicsWorld() const
Gets the Bullet dynamics world used by this physics engine.
void queueDirectedEvents(const ComponentPairKey &pair, Physics::CollisionEventType eventType)
bool sphereClosest(const glm::vec3 &position, float radius, Physics::RaycastHit &outHit, const btCollisionObject *ignoredObject=nullptr) const
Tests a sphere at one position and returns the closest overlapping collision object,...
static btCollisionShape * createCapsuleShape(float radius, float height)
Creates a capsule collision shape aligned to Bullet's Y axis.
static btCollisionShape * createPlaneShape(const glm::vec3 &surfaceNormal, float distanceFromOrigin)
Creates an infinite static plane collision shape.
btCollisionConfiguration * collisionConfiguration
Physics::CollisionProfileRegistry * getCollisionProfileRegistry() const
Gets the collision profile registry assigned to this physics engine.
std::vector< RuntimeBodySlot > bodySlots_
bool raycastClosest(const glm::vec3 &origin, const glm::vec3 &direction, float maxDistance, const Physics::PhysicsQueryFilter &filter, Physics::RaycastHit &outHit) const
Casts a filtered ray through the physics world and returns the closest hit, if any.
std::vector< PendingComponentEvent > pendingLifetimeComponentEvents_
bool sphereSweepClosest(const glm::vec3 &from, const glm::vec3 &to, float radius, const Physics::PhysicsQueryFilter &filter, Physics::RaycastHit &outHit, const btCollisionObject *ignoredObject=nullptr) const
Sweeps a filtered sphere through the physics world and returns the closest hit, if any.
btBroadphaseInterface * broadphase
bool sphereClosest(const glm::vec3 &position, float radius, const Physics::PhysicsQueryFilter &filter, Physics::RaycastHit &outHit, const btCollisionObject *ignoredObject=nullptr) const
Tests a filtered sphere and returns the closest overlapping collision object, if any.
void update(double deltaTimeSeconds)
Steps the physics simulation and queues collision events.
void removeRigidBody(btRigidBody *body) const
Removes a rigid body from the physics world and deletes it.
Physics::PhysicsBodyHandle registerBody(Components::PhysicsBody *body)
Registers a physics body component for handle-based lookup.
Physics::CollisionProfileRegistry * collisionProfileRegistry_
void unregisterBody(Physics::PhysicsBodyHandle handle)
Unregisters a physics body handle and queues End events for active pairs that reference it.
static btCollisionShape * createBoxShape(const glm::vec3 &halfExtents)
Creates a box collision shape.
void onPhysicsComponentDestroyed(entt::registry ®istry, entt::entity entity)
Cleans up legacy ECS physics data when a SimulatesPhysics component is destroyed.
std::uint32_t nextCollisionId_
btRigidBody * createRigidBody(const Physics::RigidBodyCreateInfo &info, std::uint16_t collisionGroup, std::uint16_t collisionMask) const
Creates and adds a rigid body to the physics world with explicit Bullet collision filters.
static btCompoundShape * createCompoundShape()
Creates an empty compound collision shape.
std::set< ComponentPairKey > previousComponentPairs_
~PhysicsEngine()
Removes remaining Bullet objects and destroys the Bullet world infrastructure.
std::vector< PendingComponentEvent > pendingComponentEvents_
std::uint32_t nextBodyId_
void setCollisionProfileRegistry(Physics::CollisionProfileRegistry *collisionProfileRegistry)
Sets the collision profile registry used by collision events and filtered queries.
bool bodyMatchesQueryFilter(const Components::PhysicsBody *body, const Physics::PhysicsQueryFilter &filter) const
bool sphereSweepClosest(const glm::vec3 &from, const glm::vec3 &to, float radius, Physics::RaycastHit &outHit, const btCollisionObject *ignoredObject=nullptr) const
Sweeps a sphere through the physics world and returns the closest overlap hit, if any.
void queueInvalidationEndEvents(Physics::PhysicsBodyHandle bodyHandle, Physics::CollisionHandle collisionHandle)
static btCollisionShape * createSphereShape(float radius)
Creates a sphere collision shape.
static btCollisionShape * createTriangleMeshShape(const std::vector< glm::vec3 > &vertices, const std::vector< uint32_t > &indices, btTriangleMesh *&triangleMeshOut)
Creates a static triangle mesh collision shape from vertices and triangle indices.
btDiscreteDynamicsWorld * dynamicsWorld
void collectCollisionEvents()
Components::Collision * resolve(Physics::CollisionHandle handle) const
Resolves a runtime collision handle to a collision component.
Registers collision profile names, enum mappings, and pair responses.
CollisionEventType
Lifetime state for a collision or overlap pair.
@ Begin
Pair was absent on the previous physics step and is present now.
CollisionResponse
Collision response selected for a pair of collision profiles.
@ Ignore
No contact callback and no physical collision response.
Physics::PhysicsBodyHandle bodyA
friend bool operator==(const ComponentPairKey &, const ComponentPairKey &)=default
friend auto operator<=>(const ComponentPairKey &, const ComponentPairKey &)=default
Physics::CollisionResponse response
Physics::CollisionHandle componentA
Physics::CollisionHandle componentB
Physics::PhysicsBodyHandle bodyB
Physics::CollisionResponse response
std::vector< PendingComponentEvent > componentEvents
Physics::CollisionEventType eventType
Physics::PhysicsBodyHandle otherBody
Physics::PhysicsBodyHandle selfBody
Physics::CollisionHandle otherComponent
Physics::CollisionResponse response
Physics::CollisionEventType eventType
Physics::PhysicsBodyHandle selfBody
Physics::CollisionHandle selfComponent
Physics::PhysicsBodyHandle otherBody
Components::PhysicsBody * body
Components::Collision * collision
Actor-level collision or overlap event generated by PhysicsEngine.
Runtime handle for a registered collision component.
Runtime handle for a registered physics body component.
Collision profile filter used by PhysicsEngine scene queries.
Result data for physics raycasts and sweep tests.
glm::vec3 normal
World-space surface normal at the impact point.
float distance
Distance from query origin to impact point in meters.
Components::PhysicsBody * getPhysicsBody() const
Gets the engine physics body attached to collisionObject.
const btCollisionObject * collisionObject
Bullet collision object hit by the query.
float fraction
Normalized hit fraction along the query segment.
glm::vec3 point
World-space impact point.
Parameters used by Engine::Core::PhysicsEngine::createRigidBody().
btCollisionShape * shape
Collision shape used by the body. Must not be nullptr.
float friction
Surface friction. 0.0 is low friction, 1.0 is high friction.
float restitution
Restitution. 0.0 absorbs impact energy, 1.0 preserves impact energy.
glm::mat4 transform
Initial world transform for the rigid body.
float mass
Mass in kilograms. A value of 0 creates an immovable static body.