JoltPhysics3D: Allow raycast to return hit characters (and retrieve their entities)
This commit is contained in:
parent
f2ab31cc4b
commit
6cbfb01243
|
|
@ -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
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include <NazaraUtils/Prerequisites.hpp>
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
#include <Nazara/JoltPhysics3D/Config.hpp>
|
#include <Nazara/JoltPhysics3D/Config.hpp>
|
||||||
|
#include <Nazara/JoltPhysics3D/JoltAbstractBody.hpp>
|
||||||
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.hpp>
|
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.hpp>
|
||||||
#include <Nazara/Math/Quaternion.hpp>
|
#include <Nazara/Math/Quaternion.hpp>
|
||||||
#include <Nazara/Math/Vector3.hpp>
|
#include <Nazara/Math/Vector3.hpp>
|
||||||
|
|
@ -27,7 +28,7 @@ namespace Nz
|
||||||
class JoltCollider3D;
|
class JoltCollider3D;
|
||||||
class JoltPhysWorld3D;
|
class JoltPhysWorld3D;
|
||||||
|
|
||||||
class NAZARA_JOLTPHYSICS3D_API JoltCharacter : public JoltPhysicsStepListener
|
class NAZARA_JOLTPHYSICS3D_API JoltCharacter : public JoltAbstractBody, public JoltPhysicsStepListener
|
||||||
{
|
{
|
||||||
friend JoltPhysWorld3D;
|
friend JoltPhysWorld3D;
|
||||||
|
|
||||||
|
|
@ -42,11 +43,14 @@ namespace Nz
|
||||||
inline void DisableSleeping();
|
inline void DisableSleeping();
|
||||||
void EnableSleeping(bool enable);
|
void EnableSleeping(bool enable);
|
||||||
|
|
||||||
inline UInt32 GetBodyIndex() const;
|
UInt32 GetBodyIndex() const override;
|
||||||
|
inline const std::shared_ptr<JoltCollider3D>& GetCollider() const;
|
||||||
Vector3f GetLinearVelocity() const;
|
Vector3f GetLinearVelocity() const;
|
||||||
Quaternionf GetRotation() const;
|
inline JoltPhysWorld3D& GetPhysWorld();
|
||||||
|
inline const JoltPhysWorld3D& GetPhysWorld() const;
|
||||||
Vector3f GetPosition() const;
|
Vector3f GetPosition() const;
|
||||||
std::pair<Vector3f, Quaternionf> GetPositionAndRotation() const;
|
std::pair<Vector3f, Quaternionf> GetPositionAndRotation() const;
|
||||||
|
Quaternionf GetRotation() const;
|
||||||
Vector3f GetUp() const;
|
Vector3f GetUp() const;
|
||||||
|
|
||||||
bool IsOnGround() const;
|
bool IsOnGround() const;
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,19 @@ namespace Nz
|
||||||
return EnableSleeping(false);
|
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)
|
inline void JoltCharacter::SetImpl(std::shared_ptr<JoltCharacterImpl> characterImpl)
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,10 @@ namespace JPH
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
class JoltAbstractBody;
|
||||||
class JoltCharacter;
|
class JoltCharacter;
|
||||||
class JoltCharacterImpl;
|
class JoltCharacterImpl;
|
||||||
|
class JoltCollider3D;
|
||||||
class JoltPhysicsStepListener;
|
class JoltPhysicsStepListener;
|
||||||
class JoltRigidBody3D;
|
class JoltRigidBody3D;
|
||||||
|
|
||||||
|
|
@ -75,7 +77,7 @@ namespace Nz
|
||||||
struct RaycastHit
|
struct RaycastHit
|
||||||
{
|
{
|
||||||
float fraction;
|
float fraction;
|
||||||
JoltRigidBody3D* hitBody = nullptr;
|
JoltAbstractBody* hitBody = nullptr;
|
||||||
Vector3f hitNormal;
|
Vector3f hitNormal;
|
||||||
Vector3f hitPosition;
|
Vector3f hitPosition;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
#include <NazaraUtils/Prerequisites.hpp>
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
#include <Nazara/Core/Enums.hpp>
|
#include <Nazara/Core/Enums.hpp>
|
||||||
#include <Nazara/JoltPhysics3D/Config.hpp>
|
#include <Nazara/JoltPhysics3D/Config.hpp>
|
||||||
|
#include <Nazara/JoltPhysics3D/JoltAbstractBody.hpp>
|
||||||
#include <Nazara/JoltPhysics3D/JoltCollider3D.hpp>
|
#include <Nazara/JoltPhysics3D/JoltCollider3D.hpp>
|
||||||
#include <Nazara/Math/Matrix4.hpp>
|
#include <Nazara/Math/Matrix4.hpp>
|
||||||
#include <Nazara/Math/Quaternion.hpp>
|
#include <Nazara/Math/Quaternion.hpp>
|
||||||
|
|
@ -26,7 +27,7 @@ namespace Nz
|
||||||
{
|
{
|
||||||
class JoltPhysWorld3D;
|
class JoltPhysWorld3D;
|
||||||
|
|
||||||
class NAZARA_JOLTPHYSICS3D_API JoltRigidBody3D
|
class NAZARA_JOLTPHYSICS3D_API JoltRigidBody3D : public JoltAbstractBody
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
struct DynamicSettings;
|
struct DynamicSettings;
|
||||||
|
|
@ -54,7 +55,7 @@ namespace Nz
|
||||||
Vector3f GetAngularVelocity() const;
|
Vector3f GetAngularVelocity() const;
|
||||||
inline JPH::Body* GetBody();
|
inline JPH::Body* GetBody();
|
||||||
inline const JPH::Body* GetBody() const;
|
inline const JPH::Body* GetBody() const;
|
||||||
inline UInt32 GetBodyIndex() const;
|
UInt32 GetBodyIndex() const override;
|
||||||
inline const std::shared_ptr<JoltCollider3D>& GetGeom() const;
|
inline const std::shared_ptr<JoltCollider3D>& GetGeom() const;
|
||||||
float GetLinearDamping() const;
|
float GetLinearDamping() const;
|
||||||
Vector3f GetLinearVelocity() const;
|
Vector3f GetLinearVelocity() const;
|
||||||
|
|
|
||||||
|
|
@ -36,11 +36,6 @@ namespace Nz
|
||||||
return m_body;
|
return m_body;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline UInt32 JoltRigidBody3D::GetBodyIndex() const
|
|
||||||
{
|
|
||||||
return m_bodyIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const std::shared_ptr<JoltCollider3D>& JoltRigidBody3D::GetGeom() const
|
inline const std::shared_ptr<JoltCollider3D>& JoltRigidBody3D::GetGeom() const
|
||||||
{
|
{
|
||||||
return m_geom;
|
return m_geom;
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,9 @@ namespace Nz
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void OnBodyConstruct(entt::registry& registry, entt::entity entity);
|
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 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::size_t m_stepCount;
|
||||||
std::vector<entt::entity> m_bodyIndicesToEntity;
|
std::vector<entt::entity> m_bodyIndicesToEntity;
|
||||||
|
|
@ -60,8 +61,9 @@ namespace Nz
|
||||||
entt::observer m_characterConstructObserver;
|
entt::observer m_characterConstructObserver;
|
||||||
entt::observer m_rigidBodyConstructObserver;
|
entt::observer m_rigidBodyConstructObserver;
|
||||||
entt::scoped_connection m_bodyConstructConnection;
|
entt::scoped_connection m_bodyConstructConnection;
|
||||||
entt::scoped_connection m_characterConstructConnection;
|
|
||||||
entt::scoped_connection m_bodyDestructConnection;
|
entt::scoped_connection m_bodyDestructConnection;
|
||||||
|
entt::scoped_connection m_characterConstructConnection;
|
||||||
|
entt::scoped_connection m_characterDestructConnection;
|
||||||
JoltPhysWorld3D m_physWorld;
|
JoltPhysWorld3D m_physWorld;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
m_bodyIndex(character.m_bodyIndex)
|
||||||
{
|
{
|
||||||
character.m_bodyIndex = std::numeric_limits<UInt32>::max();
|
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)
|
if (m_world)
|
||||||
{
|
{
|
||||||
m_world->UnregisterStepListener(&character);
|
m_world->UnregisterStepListener(&character);
|
||||||
|
|
@ -52,6 +58,11 @@ namespace Nz
|
||||||
bodyLock.GetBody().SetAllowSleeping(enable);
|
bodyLock.GetBody().SetAllowSleeping(enable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UInt32 JoltCharacter::GetBodyIndex() const
|
||||||
|
{
|
||||||
|
return m_bodyIndex;
|
||||||
|
}
|
||||||
|
|
||||||
Vector3f JoltCharacter::GetLinearVelocity() const
|
Vector3f JoltCharacter::GetLinearVelocity() const
|
||||||
{
|
{
|
||||||
return FromJolt(m_character->GetLinearVelocity(false));
|
return FromJolt(m_character->GetLinearVelocity(false));
|
||||||
|
|
@ -135,6 +146,12 @@ namespace Nz
|
||||||
|
|
||||||
character.m_bodyIndex = std::numeric_limits<UInt32>::max();
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,6 +174,9 @@ namespace Nz
|
||||||
|
|
||||||
m_bodyIndex = m_character->GetBodyID().GetIndex();
|
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);
|
m_world->RegisterStepListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,7 +186,7 @@ namespace Nz
|
||||||
|
|
||||||
JPH::Body& body = lock.GetBody();
|
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)));
|
hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(result.mSubShapeID2, ToJolt(hitInfo.hitPosition)));
|
||||||
|
|
||||||
if (auto fractionOpt = m_callback(hitInfo))
|
if (auto fractionOpt = m_callback(hitInfo))
|
||||||
|
|
@ -387,7 +387,7 @@ namespace Nz
|
||||||
RaycastHit hitInfo;
|
RaycastHit hitInfo;
|
||||||
hitInfo.fraction = collector.mHit.GetEarlyOutFraction();
|
hitInfo.fraction = collector.mHit.GetEarlyOutFraction();
|
||||||
hitInfo.hitPosition = Lerp(from, to, hitInfo.fraction);
|
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())));
|
hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(collector.mHit.mSubShapeID2, rayCast.GetPointOnRay(collector.mHit.GetEarlyOutFraction())));
|
||||||
|
|
||||||
callback(hitInfo);
|
callback(hitInfo);
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,11 @@ namespace Nz
|
||||||
return FromJolt(m_body->GetAngularVelocity());
|
return FromJolt(m_body->GetAngularVelocity());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UInt32 JoltRigidBody3D::GetBodyIndex() const
|
||||||
|
{
|
||||||
|
return m_bodyIndex;
|
||||||
|
}
|
||||||
|
|
||||||
float JoltRigidBody3D::GetLinearDamping() const
|
float JoltRigidBody3D::GetLinearDamping() const
|
||||||
{
|
{
|
||||||
if NAZARA_UNLIKELY(IsStatic())
|
if NAZARA_UNLIKELY(IsStatic())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue