Physics3D: Add raycast support
This commit is contained in:
committed by
Jérôme Leclercq
parent
5ee25e9621
commit
522315dbca
@@ -70,6 +70,27 @@ namespace Nz
|
||||
return m_stepSize;
|
||||
}
|
||||
|
||||
bool PhysWorld3D::RaycastQueryFirst(const Vector3f& from, const Vector3f& to, RaycastHit* hitInfo)
|
||||
{
|
||||
btCollisionWorld::ClosestRayResultCallback callback(ToBullet(from), ToBullet(to));
|
||||
m_world->dynamicWorld.rayTest(ToBullet(from), ToBullet(to), callback);
|
||||
|
||||
if (!callback.hasHit())
|
||||
return false;
|
||||
|
||||
if (hitInfo)
|
||||
{
|
||||
hitInfo->fraction = callback.m_closestHitFraction;
|
||||
hitInfo->hitNormal = FromBullet(callback.m_hitNormalWorld);
|
||||
hitInfo->hitPosition = FromBullet(callback.m_hitPointWorld);
|
||||
|
||||
if (const btRigidBody* body = btRigidBody::upcast(callback.m_collisionObject))
|
||||
hitInfo->hitBody = static_cast<RigidBody3D*>(body->getUserPointer());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PhysWorld3D::SetGravity(const Vector3f& gravity)
|
||||
{
|
||||
m_world->dynamicWorld.setGravity(ToBullet(gravity));
|
||||
|
||||
@@ -9,24 +9,55 @@
|
||||
namespace Nz
|
||||
{
|
||||
Physics3DSystem::Physics3DSystem(entt::registry& registry) :
|
||||
m_registry(registry)
|
||||
m_registry(registry),
|
||||
m_physicsConstructObserver(m_registry, entt::collector.group<RigidBody3DComponent, NodeComponent>())
|
||||
{
|
||||
m_constructConnection = registry.on_construct<RigidBody3DComponent>().connect<OnConstruct>();
|
||||
m_constructConnection = registry.on_construct<RigidBody3DComponent>().connect<&Physics3DSystem::OnConstruct>(this);
|
||||
m_destructConnection = registry.on_destroy<RigidBody3DComponent>().connect<&Physics3DSystem::OnDestruct>(this);
|
||||
}
|
||||
|
||||
Physics3DSystem::~Physics3DSystem()
|
||||
{
|
||||
m_physicsConstructObserver.disconnect();
|
||||
|
||||
// Ensure every RigidBody3D is destroyed before world is
|
||||
auto rigidBodyView = m_registry.view<RigidBody3DComponent>();
|
||||
for (auto [entity, rigidBodyComponent] : rigidBodyView.each())
|
||||
rigidBodyComponent.Destroy();
|
||||
}
|
||||
|
||||
bool Physics3DSystem::RaycastQueryFirst(const Vector3f& from, const Vector3f& to, RaycastHit* hitInfo)
|
||||
{
|
||||
if (!m_physWorld.RaycastQueryFirst(from, to, hitInfo))
|
||||
return false;
|
||||
|
||||
if (hitInfo->hitBody)
|
||||
{
|
||||
std::size_t uniqueIndex = hitInfo->hitBody->GetUniqueIndex();
|
||||
if (uniqueIndex < m_physicsEntities.size())
|
||||
hitInfo->hitEntity = entt::handle(m_registry, m_physicsEntities[uniqueIndex]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Physics3DSystem::Update(Time elapsedTime)
|
||||
{
|
||||
// Move newly-created physics entities to their node position/rotation
|
||||
m_physicsConstructObserver.each([&](entt::entity entity)
|
||||
{
|
||||
RigidBody3DComponent& entityPhysics = m_registry.get<RigidBody3DComponent>(entity);
|
||||
NodeComponent& entityNode = m_registry.get<NodeComponent>(entity);
|
||||
|
||||
entityPhysics.SetPosition(entityNode.GetPosition(CoordSys::Global));
|
||||
entityPhysics.SetRotation(entityNode.GetRotation(CoordSys::Global));
|
||||
});
|
||||
|
||||
// Update the physics world
|
||||
m_physWorld.Step(elapsedTime);
|
||||
|
||||
// Replicate rigid body position to their node components
|
||||
// TODO: Only replicate active entities
|
||||
auto view = m_registry.view<NodeComponent, const RigidBody3DComponent>();
|
||||
for (auto [entity, nodeComponent, rigidBodyComponent] : view.each())
|
||||
{
|
||||
@@ -40,13 +71,22 @@ namespace Nz
|
||||
|
||||
void Physics3DSystem::OnConstruct(entt::registry& registry, entt::entity entity)
|
||||
{
|
||||
// If our entity already has a node component when adding a rigid body, initialize it with its position/rotation
|
||||
NodeComponent* node = registry.try_get<NodeComponent>(entity);
|
||||
if (node)
|
||||
{
|
||||
RigidBody3DComponent& rigidBody = registry.get<RigidBody3DComponent>(entity);
|
||||
rigidBody.SetPosition(node->GetPosition(CoordSys::Global));
|
||||
rigidBody.SetRotation(node->GetRotation(CoordSys::Global));
|
||||
}
|
||||
// Register rigid body owning entity
|
||||
RigidBody3DComponent& rigidBody = registry.get<RigidBody3DComponent>(entity);
|
||||
std::size_t uniqueIndex = rigidBody.GetUniqueIndex();
|
||||
if (uniqueIndex >= m_physicsEntities.size())
|
||||
m_physicsEntities.resize(uniqueIndex + 1);
|
||||
|
||||
m_physicsEntities[uniqueIndex] = entity;
|
||||
}
|
||||
|
||||
void Physics3DSystem::OnDestruct(entt::registry& registry, entt::entity entity)
|
||||
{
|
||||
// Unregister owning entity
|
||||
RigidBody3DComponent& rigidBody = registry.get<RigidBody3DComponent>(entity);
|
||||
std::size_t uniqueIndex = rigidBody.GetUniqueIndex();
|
||||
assert(uniqueIndex <= m_physicsEntities.size());
|
||||
|
||||
m_physicsEntities[uniqueIndex] = entt::null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user