diff --git a/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp b/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp index 549ecac47..04641bf0c 100644 --- a/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp +++ b/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace JPH @@ -63,6 +64,7 @@ namespace Nz { float fraction; JoltRigidBody3D* hitBody = nullptr; + Vector3f hitNormal; Vector3f hitPosition; }; diff --git a/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp b/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp index cd34d0b36..d82536e0b 100644 --- a/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp +++ b/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp @@ -163,7 +163,7 @@ namespace Nz { namespace NAZARA_ANONYMOUS_NAMESPACE { - class CallbackHitResult : public JPH::RayCastBodyCollector + class CallbackHitResult : public JPH::CastRayCollector { public: CallbackHitResult(const JPH::BodyLockInterface& bodyLockInterface, const Vector3f& from, const Vector3f& to, const FunctionRef(const JoltPhysWorld3D::RaycastHit& hitInfo)>& callback) : @@ -175,7 +175,7 @@ namespace Nz { } - void AddHit(const JPH::BroadPhaseCastResult& result) override + void AddHit(const JPH::RayCastResult& result) override { JoltPhysWorld3D::RaycastHit hitInfo; hitInfo.fraction = result.mFraction; @@ -185,7 +185,10 @@ namespace Nz if (!lock.Succeeded()) return; //< body was destroyed - hitInfo.hitBody = reinterpret_cast(static_cast(lock.GetBody().GetUserData())); + JPH::Body& body = lock.GetBody(); + + hitInfo.hitBody = reinterpret_cast(static_cast(body.GetUserData())); + hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(result.mSubShapeID2, ToJolt(hitInfo.hitPosition))); if (auto fractionOpt = m_callback(hitInfo)) { @@ -336,24 +339,28 @@ namespace Nz bool JoltPhysWorld3D::RaycastQuery(const Vector3f& from, const Vector3f& to, const FunctionRef(const RaycastHit& hitInfo)>& callback) { - JPH::RayCast rayCast; + JPH::RRayCast rayCast; rayCast.mDirection = ToJolt(to - from); rayCast.mOrigin = ToJolt(from); + JPH::RayCastSettings rayCastSettings; + CallbackHitResult collector(m_world->physicsSystem.GetBodyLockInterface(), from, to, callback); - m_world->physicsSystem.GetBroadPhaseQuery().CastRay(rayCast, collector); + m_world->physicsSystem.GetNarrowPhaseQuery().CastRay(rayCast, rayCastSettings, collector); return collector.DidHit(); } bool JoltPhysWorld3D::RaycastQueryFirst(const Vector3f& from, const Vector3f& to, const FunctionRef& callback) { - JPH::RayCast rayCast; + JPH::RRayCast rayCast; rayCast.mDirection = ToJolt(to - from); rayCast.mOrigin = ToJolt(from); - JPH::ClosestHitCollisionCollector collector; - m_world->physicsSystem.GetBroadPhaseQuery().CastRay(rayCast, collector); + JPH::RayCastSettings rayCastSettings; + + JPH::ClosestHitCollisionCollector collector; + m_world->physicsSystem.GetNarrowPhaseQuery().CastRay(rayCast, rayCastSettings, collector); if (!collector.HadHit()) return false; @@ -362,10 +369,13 @@ namespace Nz if (!lock.Succeeded()) return false; //< body was destroyed before lock + JPH::Body& body = lock.GetBody(); + RaycastHit hitInfo; hitInfo.fraction = collector.mHit.GetEarlyOutFraction(); hitInfo.hitPosition = Lerp(from, to, hitInfo.fraction); - hitInfo.hitBody = reinterpret_cast(static_cast(lock.GetBody().GetUserData())); + hitInfo.hitBody = reinterpret_cast(static_cast(body.GetUserData())); + hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(collector.mHit.mSubShapeID2, rayCast.GetPointOnRay(collector.mHit.GetEarlyOutFraction()))); return true; }