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,32 @@
// 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
#pragma once
#ifndef NAZARA_JOLTPHYSICS3D_JOLTABSTRACTBODY_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTABSTRACTBODY_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
namespace Nz
{
class NAZARA_JOLTPHYSICS3D_API JoltAbstractBody
{
public:
JoltAbstractBody() = default;
JoltAbstractBody(const JoltAbstractBody&) = delete;
JoltAbstractBody(JoltAbstractBody&&) = delete;
virtual ~JoltAbstractBody();
virtual UInt32 GetBodyIndex() const = 0;
JoltAbstractBody& operator=(const JoltAbstractBody&) = delete;
JoltAbstractBody& operator=(JoltAbstractBody&&) = delete;
};
}
#include <Nazara/JoltPhysics3D/JoltAbstractBody.inl>
#endif // NAZARA_JOLTPHYSICS3D_JOLTABSTRACTBODY_HPP

View File

@ -0,0 +1,12 @@
// 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
{
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@ -9,6 +9,7 @@
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/JoltPhysics3D/JoltAbstractBody.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
@ -27,7 +28,7 @@ namespace Nz
class JoltCollider3D;
class JoltPhysWorld3D;
class NAZARA_JOLTPHYSICS3D_API JoltCharacter : public JoltPhysicsStepListener
class NAZARA_JOLTPHYSICS3D_API JoltCharacter : public JoltAbstractBody, public JoltPhysicsStepListener
{
friend JoltPhysWorld3D;
@ -42,11 +43,14 @@ namespace Nz
inline void DisableSleeping();
void EnableSleeping(bool enable);
inline UInt32 GetBodyIndex() const;
UInt32 GetBodyIndex() const override;
inline const std::shared_ptr<JoltCollider3D>& GetCollider() const;
Vector3f GetLinearVelocity() const;
Quaternionf GetRotation() const;
inline JoltPhysWorld3D& GetPhysWorld();
inline const JoltPhysWorld3D& GetPhysWorld() const;
Vector3f GetPosition() const;
std::pair<Vector3f, Quaternionf> GetPositionAndRotation() const;
Quaternionf GetRotation() const;
Vector3f GetUp() const;
bool IsOnGround() const;

View File

@ -11,9 +11,19 @@ namespace Nz
return EnableSleeping(false);
}
inline UInt32 JoltCharacter::GetBodyIndex() const
inline const std::shared_ptr<JoltCollider3D>& JoltCharacter::GetCollider() const
{
return m_bodyIndex;
return m_collider;
}
inline JoltPhysWorld3D& JoltCharacter::GetPhysWorld()
{
return *m_world;
}
inline const JoltPhysWorld3D& JoltCharacter::GetPhysWorld() const
{
return *m_world;
}
inline void JoltCharacter::SetImpl(std::shared_ptr<JoltCharacterImpl> characterImpl)

View File

@ -27,8 +27,10 @@ namespace JPH
namespace Nz
{
class JoltAbstractBody;
class JoltCharacter;
class JoltCharacterImpl;
class JoltCollider3D;
class JoltPhysicsStepListener;
class JoltRigidBody3D;
@ -75,7 +77,7 @@ namespace Nz
struct RaycastHit
{
float fraction;
JoltRigidBody3D* hitBody = nullptr;
JoltAbstractBody* hitBody = nullptr;
Vector3f hitNormal;
Vector3f hitPosition;
};

View File

@ -10,6 +10,7 @@
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/Enums.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/JoltPhysics3D/JoltAbstractBody.hpp>
#include <Nazara/JoltPhysics3D/JoltCollider3D.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Quaternion.hpp>
@ -26,7 +27,7 @@ namespace Nz
{
class JoltPhysWorld3D;
class NAZARA_JOLTPHYSICS3D_API JoltRigidBody3D
class NAZARA_JOLTPHYSICS3D_API JoltRigidBody3D : public JoltAbstractBody
{
public:
struct DynamicSettings;
@ -54,7 +55,7 @@ namespace Nz
Vector3f GetAngularVelocity() const;
inline JPH::Body* GetBody();
inline const JPH::Body* GetBody() const;
inline UInt32 GetBodyIndex() const;
UInt32 GetBodyIndex() const override;
inline const std::shared_ptr<JoltCollider3D>& GetGeom() const;
float GetLinearDamping() const;
Vector3f GetLinearVelocity() const;

View File

@ -36,11 +36,6 @@ namespace Nz
return m_body;
}
inline UInt32 JoltRigidBody3D::GetBodyIndex() const
{
return m_bodyIndex;
}
inline const std::shared_ptr<JoltCollider3D>& JoltRigidBody3D::GetGeom() const
{
return m_geom;

View File

@ -51,8 +51,9 @@ namespace Nz
private:
void OnBodyConstruct(entt::registry& registry, entt::entity entity);
void OnCharacterConstruct(entt::registry& registry, entt::entity entity);
void OnBodyDestruct(entt::registry& registry, entt::entity entity);
void OnCharacterConstruct(entt::registry& registry, entt::entity entity);
void OnCharacterDestruct(entt::registry& registry, entt::entity entity);
std::size_t m_stepCount;
std::vector<entt::entity> m_bodyIndicesToEntity;
@ -60,8 +61,9 @@ namespace Nz
entt::observer m_characterConstructObserver;
entt::observer m_rigidBodyConstructObserver;
entt::scoped_connection m_bodyConstructConnection;
entt::scoped_connection m_characterConstructConnection;
entt::scoped_connection m_bodyDestructConnection;
entt::scoped_connection m_characterConstructConnection;
entt::scoped_connection m_characterDestructConnection;
JoltPhysWorld3D m_physWorld;
};
}

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())