JoltPhysics3D: Allow raycast to return hit characters (and retrieve their entities)
This commit is contained in:
11
src/Nazara/JoltPhysics3D/JoltAbstractBody.cpp
Normal file
11
src/Nazara/JoltPhysics3D/JoltAbstractBody.cpp
Normal 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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user