JoltPhysics3D: Allow raycast to return hit characters (and retrieve their entities)

This commit is contained in:
SirLynix
2023-12-07 16:45:14 +01:00
parent f2ab31cc4b
commit 6cbfb01243
12 changed files with 112 additions and 18 deletions

View File

@@ -0,0 +1,11 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - JoltPhysics3D module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/JoltPhysics3D/JoltAbstractBody.hpp>
#include <Nazara/JoltPhysics3D/Debug.hpp>
namespace Nz
{
JoltAbstractBody::~JoltAbstractBody() = default;
}

View File

@@ -29,7 +29,13 @@ namespace Nz
m_bodyIndex(character.m_bodyIndex)
{
character.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_character)
{
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this)));
}
if (m_world)
{
m_world->UnregisterStepListener(&character);
@@ -52,6 +58,11 @@ namespace Nz
bodyLock.GetBody().SetAllowSleeping(enable);
}
UInt32 JoltCharacter::GetBodyIndex() const
{
return m_bodyIndex;
}
Vector3f JoltCharacter::GetLinearVelocity() const
{
return FromJolt(m_character->GetLinearVelocity(false));
@@ -135,6 +146,12 @@ namespace Nz
character.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_character)
{
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this)));
}
return *this;
}
@@ -157,6 +174,9 @@ namespace Nz
m_bodyIndex = m_character->GetBodyID().GetIndex();
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this)));
m_world->RegisterStepListener(this);
}

View File

@@ -186,7 +186,7 @@ namespace Nz
JPH::Body& body = lock.GetBody();
hitInfo.hitBody = reinterpret_cast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(body.GetUserData()));
hitInfo.hitBody = reinterpret_cast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData()));
hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(result.mSubShapeID2, ToJolt(hitInfo.hitPosition)));
if (auto fractionOpt = m_callback(hitInfo))
@@ -387,7 +387,7 @@ namespace Nz
RaycastHit hitInfo;
hitInfo.fraction = collector.mHit.GetEarlyOutFraction();
hitInfo.hitPosition = Lerp(from, to, hitInfo.fraction);
hitInfo.hitBody = reinterpret_cast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(body.GetUserData()));
hitInfo.hitBody = reinterpret_cast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData()));
hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(collector.mHit.mSubShapeID2, rayCast.GetPointOnRay(collector.mHit.GetEarlyOutFraction())));
callback(hitInfo);

View File

@@ -120,6 +120,11 @@ namespace Nz
return FromJolt(m_body->GetAngularVelocity());
}
UInt32 JoltRigidBody3D::GetBodyIndex() const
{
return m_bodyIndex;
}
float JoltRigidBody3D::GetLinearDamping() const
{
if NAZARA_UNLIKELY(IsStatic())