JoltPhysics3D: Rework Character class

This commit is contained in:
SirLynix 2023-05-15 19:03:28 +02:00
parent 14c9c7fffd
commit 6f15400d01
19 changed files with 339 additions and 70 deletions

View File

@ -35,6 +35,7 @@
#include <Nazara/JoltPhysics3D/JoltCollider3D.hpp> #include <Nazara/JoltPhysics3D/JoltCollider3D.hpp>
#include <Nazara/JoltPhysics3D/JoltConstraint3D.hpp> #include <Nazara/JoltPhysics3D/JoltConstraint3D.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysics3D.hpp> #include <Nazara/JoltPhysics3D/JoltPhysics3D.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp> #include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp>
#include <Nazara/JoltPhysics3D/JoltRigidBody3D.hpp> #include <Nazara/JoltPhysics3D/JoltRigidBody3D.hpp>

View File

@ -29,6 +29,7 @@
#ifndef NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP #ifndef NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP
#define NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP #define NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP
#include <Nazara/JoltPhysics3D/Components/JoltCharacterComponent.hpp>
#include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.hpp> #include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.hpp>
#endif // NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP #endif // NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP

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_COMPONENTS_JOLTCHARACTERCOMPONENT_HPP
#define NAZARA_JOLTPHYSICS3D_COMPONENTS_JOLTCHARACTERCOMPONENT_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/JoltCharacter.hpp>
namespace Nz
{
class NAZARA_JOLTPHYSICS3D_API JoltCharacterComponent : public JoltCharacter
{
friend class JoltPhysics3DSystem;
public:
using JoltCharacter::JoltCharacter;
JoltCharacterComponent(const JoltCharacterComponent&) = default;
JoltCharacterComponent(JoltCharacterComponent&&) noexcept = default;
~JoltCharacterComponent() = default;
JoltCharacterComponent& operator=(const JoltCharacterComponent&) = default;
JoltCharacterComponent& operator=(JoltCharacterComponent&&) noexcept = default;
};
}
#include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.inl>
#endif // NAZARA_JOLTPHYSICS3D_COMPONENTS_JOLTCHARACTERCOMPONENT_HPP

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/Debug.hpp>
namespace Nz
{
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@ -9,8 +9,10 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp> #include <Nazara/JoltPhysics3D/Config.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>
#include <NazaraUtils/MovablePtr.hpp>
#include <memory> #include <memory>
namespace JPH namespace JPH
@ -21,22 +23,24 @@ namespace JPH
namespace Nz namespace Nz
{ {
class JoltCharacterImpl;
class JoltCollider3D; class JoltCollider3D;
class JoltPhysWorld3D; class JoltPhysWorld3D;
class NAZARA_JOLTPHYSICS3D_API JoltCharacter class NAZARA_JOLTPHYSICS3D_API JoltCharacter : public JoltPhysicsStepListener
{ {
friend JoltPhysWorld3D; friend JoltPhysWorld3D;
public: public:
JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position, const Quaternionf& rotation = Quaternionf::Identity()); JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position = Vector3f::Zero(), const Quaternionf& rotation = Quaternionf::Identity());
JoltCharacter(const JoltCharacter&) = delete; JoltCharacter(const JoltCharacter&) = delete;
JoltCharacter(JoltCharacter&&) = delete; JoltCharacter(JoltCharacter&& character) noexcept;
~JoltCharacter(); ~JoltCharacter();
inline void DisableSleeping(); inline void DisableSleeping();
void EnableSleeping(bool enable); void EnableSleeping(bool enable);
inline UInt32 GetBodyIndex() const;
Vector3f GetLinearVelocity() const; Vector3f GetLinearVelocity() const;
Quaternionf GetRotation() const; Quaternionf GetRotation() const;
Vector3f GetPosition() const; Vector3f GetPosition() const;
@ -46,23 +50,45 @@ namespace Nz
bool IsOnGround() const; bool IsOnGround() const;
void SetFriction(float friction); void SetFriction(float friction);
inline void SetImpl(std::shared_ptr<JoltCharacterImpl> characterImpl);
void SetLinearVelocity(const Vector3f& linearVel); void SetLinearVelocity(const Vector3f& linearVel);
void SetRotation(const Quaternionf& rotation); void SetRotation(const Quaternionf& rotation);
void SetUp(const Vector3f& up); void SetUp(const Vector3f& up);
void TeleportTo(const Vector3f& position, const Quaternionf& rotation);
void WakeUp(); void WakeUp();
JoltCharacter& operator=(const JoltCharacter&) = delete; JoltCharacter& operator=(const JoltCharacter&) = delete;
JoltCharacter& operator=(JoltCharacter&&) = delete; JoltCharacter& operator=(JoltCharacter&& character) noexcept;
protected: protected:
virtual void PreSimulate(float elapsedTime); void Destroy();
virtual void PostSimulate();
private: private:
void PostSimulate() override;
void PreSimulate(float elapsedTime) override;
std::shared_ptr<JoltCharacterImpl> m_impl;
std::shared_ptr<JoltCollider3D> m_collider; std::shared_ptr<JoltCollider3D> m_collider;
std::unique_ptr<JPH::Character> m_character; std::unique_ptr<JPH::Character> m_character;
JoltPhysWorld3D& m_physicsWorld; MovablePtr<JoltPhysWorld3D> m_world;
UInt32 m_bodyIndex;
};
class NAZARA_JOLTPHYSICS3D_API JoltCharacterImpl
{
public:
JoltCharacterImpl() = default;
JoltCharacterImpl(const JoltCharacterImpl&) = delete;
JoltCharacterImpl(JoltCharacterImpl&&) = delete;
virtual ~JoltCharacterImpl();
virtual void PostSimulate(JoltCharacter& character);
virtual void PreSimulate(JoltCharacter& character, float elapsedTime);
JoltCharacterImpl& operator=(const JoltCharacterImpl&) = delete;
JoltCharacterImpl& operator=(JoltCharacterImpl&&) = delete;
}; };
} }

View File

@ -10,6 +10,16 @@ namespace Nz
{ {
return EnableSleeping(false); return EnableSleeping(false);
} }
inline UInt32 JoltCharacter::GetBodyIndex() const
{
return m_bodyIndex;
}
inline void JoltCharacter::SetImpl(std::shared_ptr<JoltCharacterImpl> characterImpl)
{
m_impl = std::move(characterImpl);
}
} }
#include <Nazara/JoltPhysics3D/DebugOff.hpp> #include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@ -28,6 +28,8 @@ namespace JPH
namespace Nz namespace Nz
{ {
class JoltCharacter; class JoltCharacter;
class JoltCharacterImpl;
class JoltPhysicsStepListener;
class JoltRigidBody3D; class JoltRigidBody3D;
class NAZARA_JOLTPHYSICS3D_API JoltPhysWorld3D class NAZARA_JOLTPHYSICS3D_API JoltPhysWorld3D
@ -57,12 +59,16 @@ namespace Nz
void RefreshBodies(); void RefreshBodies();
inline void RegisterStepListener(JoltPhysicsStepListener* character);
void SetGravity(const Vector3f& gravity); void SetGravity(const Vector3f& gravity);
void SetMaxStepCount(std::size_t maxStepCount); void SetMaxStepCount(std::size_t maxStepCount);
void SetStepSize(Time stepSize); void SetStepSize(Time stepSize);
void Step(Time timestep); void Step(Time timestep);
inline void UnregisterStepListener(JoltPhysicsStepListener* character);
JoltPhysWorld3D& operator=(const JoltPhysWorld3D&) = delete; JoltPhysWorld3D& operator=(const JoltPhysWorld3D&) = delete;
JoltPhysWorld3D& operator=(JoltPhysWorld3D&&) = delete; JoltPhysWorld3D& operator=(JoltPhysWorld3D&&) = delete;
@ -83,21 +89,21 @@ namespace Nz
struct JoltWorld; struct JoltWorld;
std::shared_ptr<JoltCharacterImpl> GetDefaultCharacterImpl();
const JPH::Shape* GetNullShape() const; const JPH::Shape* GetNullShape() const;
void OnPreStep(float deltatime); void OnPreStep(float deltatime);
void RegisterBody(const JPH::BodyID& bodyID, bool activate, bool removeFromDeactivationList); void RegisterBody(const JPH::BodyID& bodyID, bool activate, bool removeFromDeactivationList);
inline void RegisterCharacter(JoltCharacter* character);
void UnregisterBody(const JPH::BodyID& bodyID, bool destroy, bool removeFromRegisterList); void UnregisterBody(const JPH::BodyID& bodyID, bool destroy, bool removeFromRegisterList);
inline void UnregisterCharacter(JoltCharacter* character);
std::size_t m_maxStepCount; std::size_t m_maxStepCount;
std::shared_ptr<JoltCharacterImpl> m_defaultCharacterImpl;
std::unique_ptr<std::atomic_uint64_t[]> m_activeBodies; std::unique_ptr<std::atomic_uint64_t[]> m_activeBodies;
std::unique_ptr<std::uint64_t[]> m_registeredBodies; std::unique_ptr<std::uint64_t[]> m_registeredBodies;
std::unique_ptr<JoltWorld> m_world; std::unique_ptr<JoltWorld> m_world;
std::vector<JoltCharacter*> m_characters; std::vector<JoltPhysicsStepListener*> m_stepListeners;
Vector3f m_gravity; Vector3f m_gravity;
Time m_stepSize; Time m_stepSize;
Time m_timestepAccumulator; Time m_timestepAccumulator;

View File

@ -22,17 +22,17 @@ namespace Nz
return m_registeredBodies[blockIndex] & (UInt64(1u) << localIndex); return m_registeredBodies[blockIndex] & (UInt64(1u) << localIndex);
} }
inline void JoltPhysWorld3D::RegisterCharacter(JoltCharacter* character) inline void JoltPhysWorld3D::RegisterStepListener(JoltPhysicsStepListener* stepListener)
{ {
auto it = std::lower_bound(m_characters.begin(), m_characters.end(), character); auto it = std::lower_bound(m_stepListeners.begin(), m_stepListeners.end(), stepListener);
m_characters.insert(it, character); m_stepListeners.insert(it, stepListener);
} }
inline void JoltPhysWorld3D::UnregisterCharacter(JoltCharacter* character) inline void JoltPhysWorld3D::UnregisterStepListener(JoltPhysicsStepListener* stepListener)
{ {
auto it = std::lower_bound(m_characters.begin(), m_characters.end(), character); auto it = std::lower_bound(m_stepListeners.begin(), m_stepListeners.end(), stepListener);
assert(*it == character); assert(*it == stepListener);
m_characters.erase(it); m_stepListeners.erase(it);
} }
} }

View File

@ -0,0 +1,33 @@
// 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_JOLTPHYSICSSTEPLISTENER_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTPHYSICSSTEPLISTENER_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
namespace Nz
{
class NAZARA_JOLTPHYSICS3D_API JoltPhysicsStepListener
{
public:
JoltPhysicsStepListener() = default;
JoltPhysicsStepListener(const JoltPhysicsStepListener&) = delete;
JoltPhysicsStepListener(JoltPhysicsStepListener&&) = delete;
virtual ~JoltPhysicsStepListener();
virtual void PostSimulate();
virtual void PreSimulate(float elapsedTime);
JoltPhysicsStepListener& operator=(const JoltPhysicsStepListener&) = delete;
JoltPhysicsStepListener& operator=(JoltPhysicsStepListener&&) = delete;
};
}
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.inl>
#endif // NAZARA_JOLTPHYSICS3D_JOLTPHYSICSSTEPLISTENER_HPP

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/Debug.hpp>
namespace Nz
{
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@ -133,8 +133,8 @@ namespace Nz
bool ShouldActivate() const; bool ShouldActivate() const;
std::shared_ptr<JoltCollider3D> m_geom; std::shared_ptr<JoltCollider3D> m_geom;
JPH::Body* m_body; MovablePtr<JPH::Body> m_body;
JoltPhysWorld3D* m_world; MovablePtr<JoltPhysWorld3D> m_world;
UInt32 m_bodyIndex; UInt32 m_bodyIndex;
bool m_isSimulationEnabled; bool m_isSimulationEnabled;
bool m_isTrigger; bool m_isTrigger;

View File

@ -11,6 +11,7 @@
#include <Nazara/Core/Clock.hpp> #include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Time.hpp> #include <Nazara/Core/Time.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp> #include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp>
#include <Nazara/JoltPhysics3D/Components/JoltCharacterComponent.hpp>
#include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.hpp> #include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.hpp>
#include <NazaraUtils/TypeList.hpp> #include <NazaraUtils/TypeList.hpp>
#include <entt/entt.hpp> #include <entt/entt.hpp>
@ -22,7 +23,7 @@ namespace Nz
{ {
public: public:
static constexpr Int64 ExecutionOrder = 0; static constexpr Int64 ExecutionOrder = 0;
using Components = TypeList<JoltRigidBody3DComponent, class NodeComponent>; using Components = TypeList<JoltCharacterComponent, JoltRigidBody3DComponent, class NodeComponent>;
struct RaycastHit; struct RaycastHit;
@ -31,6 +32,7 @@ namespace Nz
JoltPhysics3DSystem(JoltPhysics3DSystem&&) = delete; JoltPhysics3DSystem(JoltPhysics3DSystem&&) = delete;
~JoltPhysics3DSystem(); ~JoltPhysics3DSystem();
template<typename... Args> JoltCharacterComponent CreateCharacter(Args&&... args);
template<typename... Args> JoltRigidBody3DComponent CreateRigidBody(Args&&... args); template<typename... Args> JoltRigidBody3DComponent CreateRigidBody(Args&&... args);
void Dump(); void Dump();
@ -52,13 +54,14 @@ namespace Nz
}; };
private: private:
void OnConstruct(entt::registry& registry, entt::entity entity); void OnBodyConstruct(entt::registry& registry, entt::entity entity);
void OnDestruct(entt::registry& registry, entt::entity entity); void OnBodyDestruct(entt::registry& registry, entt::entity entity);
std::size_t m_stepCount; std::size_t m_stepCount;
std::vector<entt::entity> m_physicsEntities; std::vector<entt::entity> m_physicsEntities;
entt::registry& m_registry; entt::registry& m_registry;
entt::observer m_physicsConstructObserver; entt::observer m_characterConstructObserver;
entt::observer m_rigidBodyConstructObserver;
entt::scoped_connection m_constructConnection; entt::scoped_connection m_constructConnection;
entt::scoped_connection m_destructConnection; entt::scoped_connection m_destructConnection;
JoltPhysWorld3D m_physWorld; JoltPhysWorld3D m_physWorld;

View File

@ -6,6 +6,12 @@
namespace Nz namespace Nz
{ {
template<typename... Args>
JoltCharacterComponent JoltPhysics3DSystem::CreateCharacter(Args&& ...args)
{
return JoltCharacterComponent(m_physWorld, std::forward<Args>(args)...);
}
template<typename... Args> template<typename... Args>
JoltRigidBody3DComponent JoltPhysics3DSystem::CreateRigidBody(Args&&... args) JoltRigidBody3DComponent JoltPhysics3DSystem::CreateRigidBody(Args&&... args)
{ {

View File

@ -5,7 +5,6 @@
#include <Nazara/BulletPhysics3D/Systems/BulletPhysics3DSystem.hpp> #include <Nazara/BulletPhysics3D/Systems/BulletPhysics3DSystem.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp> #include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp> #include <Nazara/Utility/Components/NodeComponent.hpp>
#include <iostream>
#include <Nazara/BulletPhysics3D/Debug.hpp> #include <Nazara/BulletPhysics3D/Debug.hpp>
namespace Nz namespace Nz
@ -37,11 +36,13 @@ namespace Nz
if (m_stepCount == 0) if (m_stepCount == 0)
m_stepCount = 1; m_stepCount = 1;
/*
std::cout << "Physics time: " << (m_physicsTime / Time::Nanoseconds(m_stepCount)) << std::endl; std::cout << "Physics time: " << (m_physicsTime / Time::Nanoseconds(m_stepCount)) << std::endl;
std::cout << "Update time: " << (m_updateTime / Time::Nanoseconds(m_stepCount)) << std::endl; std::cout << "Update time: " << (m_updateTime / Time::Nanoseconds(m_stepCount)) << std::endl;
std::cout << "Active body count: " << m_activeObjectCount << std::endl; std::cout << "Active body count: " << m_activeObjectCount << std::endl;
std::cout << "--" << std::endl; std::cout << "--" << std::endl;
*/
m_stepCount = 0; m_stepCount = 0;
m_physicsTime = Time::Zero(); m_physicsTime = Time::Zero();
m_updateTime = Time::Zero(); m_updateTime = Time::Zero();

View File

@ -16,7 +16,7 @@ namespace Nz
{ {
JoltCharacter::JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position, const Quaternionf& rotation) : JoltCharacter::JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position, const Quaternionf& rotation) :
m_collider(std::move(collider)), m_collider(std::move(collider)),
m_physicsWorld(physWorld) m_world(&physWorld)
{ {
auto shapeResult = m_collider->GetShapeSettings()->Create(); auto shapeResult = m_collider->GetShapeSettings()->Create();
if (!shapeResult.IsValid()) if (!shapeResult.IsValid())
@ -26,22 +26,35 @@ namespace Nz
settings.mShape = shapeResult.Get(); settings.mShape = shapeResult.Get();
settings.mLayer = 1; settings.mLayer = 1;
m_character = std::make_unique<JPH::Character>(&settings, ToJolt(position), ToJolt(rotation), 0, m_physicsWorld.GetPhysicsSystem()); m_character = std::make_unique<JPH::Character>(&settings, ToJolt(position), ToJolt(rotation), 0, m_world->GetPhysicsSystem());
m_character->AddToPhysicsSystem(); m_character->AddToPhysicsSystem();
m_physicsWorld.RegisterCharacter(this); m_bodyIndex = m_character->GetBodyID().GetIndex();
m_world->RegisterStepListener(this);
}
JoltCharacter::JoltCharacter(JoltCharacter&& character) noexcept :
m_impl(std::move(character.m_impl)),
m_collider(std::move(character.m_collider)),
m_character(std::move(character.m_character)),
m_world(std::move(character.m_world)),
m_bodyIndex(character.m_bodyIndex)
{
character.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_world)
m_world->RegisterStepListener(this);
} }
JoltCharacter::~JoltCharacter() JoltCharacter::~JoltCharacter()
{ {
m_character->RemoveFromPhysicsSystem(); Destroy();
m_physicsWorld.UnregisterCharacter(this);
} }
void JoltCharacter::EnableSleeping(bool enable) void JoltCharacter::EnableSleeping(bool enable)
{ {
const JPH::BodyLockInterfaceNoLock& bodyInterface = m_physicsWorld.GetPhysicsSystem()->GetBodyLockInterfaceNoLock(); const JPH::BodyLockInterfaceNoLock& bodyInterface = m_world->GetPhysicsSystem()->GetBodyLockInterfaceNoLock();
JPH::BodyLockWrite bodyLock(bodyInterface, m_character->GetBodyID()); JPH::BodyLockWrite bodyLock(bodyInterface, m_character->GetBodyID());
if (!bodyLock.Succeeded()) if (!bodyLock.Succeeded())
return; return;
@ -85,7 +98,7 @@ namespace Nz
void JoltCharacter::SetFriction(float friction) void JoltCharacter::SetFriction(float friction)
{ {
JPH::BodyInterface& bodyInterface = m_physicsWorld.GetPhysicsSystem()->GetBodyInterfaceNoLock(); JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetFriction(m_character->GetBodyID(), friction); bodyInterface.SetFriction(m_character->GetBodyID(), friction);
} }
@ -104,17 +117,71 @@ namespace Nz
m_character->SetUp(ToJolt(up)); m_character->SetUp(ToJolt(up));
} }
void JoltCharacter::TeleportTo(const Vector3f& position, const Quaternionf& rotation)
{
m_character->SetPositionAndRotation(ToJolt(position), ToJolt(rotation), JPH::EActivation::Activate, false);
}
void JoltCharacter::WakeUp() void JoltCharacter::WakeUp()
{ {
m_character->Activate(false); m_character->Activate(false);
} }
void JoltCharacter::PreSimulate(float /*elapsedTime*/) JoltCharacter& JoltCharacter::operator=(JoltCharacter&& character) noexcept
{ {
if (m_world)
m_world->UnregisterStepListener(this);
m_impl = std::move(character.m_impl);
m_collider = std::move(character.m_collider);
m_character = std::move(character.m_character);
m_bodyIndex = character.m_bodyIndex;
m_world = std::move(character.m_world);
if (m_world)
m_world->RegisterStepListener(this);
character.m_bodyIndex = std::numeric_limits<UInt32>::max();
return *this;
}
void JoltCharacter::Destroy()
{
if (m_character)
{
m_character->RemoveFromPhysicsSystem();
m_character = nullptr;
}
if (m_world)
{
m_world->UnregisterStepListener(this);
m_world = nullptr;
}
m_collider.reset();
} }
void JoltCharacter::PostSimulate() void JoltCharacter::PostSimulate()
{ {
m_character->PostSimulation(0.05f); m_character->PostSimulation(0.05f);
m_impl->PostSimulate(*this);
}
void JoltCharacter::PreSimulate(float elapsedTime)
{
m_impl->PreSimulate(*this, elapsedTime);
}
JoltCharacterImpl::~JoltCharacterImpl() = default;
void JoltCharacterImpl::PostSimulate(JoltCharacter& /*character*/)
{
}
void JoltCharacterImpl::PreSimulate(JoltCharacter& /*character*/, float /*elapsedTime*/)
{
} }
} }

View File

@ -3,9 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp> #include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp>
#include <Nazara/JoltPhysics3D/JoltCharacter.hpp>
#include <Nazara/JoltPhysics3D/JoltHelper.hpp> #include <Nazara/JoltPhysics3D/JoltHelper.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysics3D.hpp> #include <Nazara/JoltPhysics3D/JoltPhysics3D.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.hpp>
#include <NazaraUtils/MemoryPool.hpp> #include <NazaraUtils/MemoryPool.hpp>
#include <NazaraUtils/StackVector.hpp> #include <NazaraUtils/StackVector.hpp>
#include <Jolt/Jolt.h> #include <Jolt/Jolt.h>
@ -469,8 +469,8 @@ namespace Nz
{ {
m_world->physicsSystem.Update(stepSize, 1, 1, &m_world->tempAllocator, &jobSystem); m_world->physicsSystem.Update(stepSize, 1, 1, &m_world->tempAllocator, &jobSystem);
for (JoltCharacter* character : m_characters) for (JoltPhysicsStepListener* stepListener : m_stepListeners)
character->PostSimulate(); stepListener->PostSimulate();
m_timestepAccumulator -= m_stepSize; m_timestepAccumulator -= m_stepSize;
stepCount++; stepCount++;
@ -523,6 +523,14 @@ namespace Nz
} }
} }
std::shared_ptr<JoltCharacterImpl> JoltPhysWorld3D::GetDefaultCharacterImpl()
{
if (!m_defaultCharacterImpl)
m_defaultCharacterImpl = std::make_shared<JoltCharacterImpl>();
return m_defaultCharacterImpl;
}
const JPH::Shape* JoltPhysWorld3D::GetNullShape() const const JPH::Shape* JoltPhysWorld3D::GetNullShape() const
{ {
if (!m_world->nullShape) if (!m_world->nullShape)
@ -536,7 +544,7 @@ namespace Nz
void JoltPhysWorld3D::OnPreStep(float deltatime) void JoltPhysWorld3D::OnPreStep(float deltatime)
{ {
for (JoltCharacter* character : m_characters) for (JoltPhysicsStepListener* stepListener : m_stepListeners)
character->PreSimulate(deltatime); stepListener->PreSimulate(deltatime);
} }
} }

View File

@ -0,0 +1,19 @@
// 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/JoltPhysicsStepListener.hpp>
#include <Nazara/JoltPhysics3D/Debug.hpp>
namespace Nz
{
JoltPhysicsStepListener::~JoltPhysicsStepListener() = default;
void JoltPhysicsStepListener::PostSimulate()
{
}
void JoltPhysicsStepListener::PreSimulate(float /*elapsedTime*/)
{
}
}

View File

@ -56,13 +56,12 @@ namespace Nz
JoltRigidBody3D::JoltRigidBody3D(JoltRigidBody3D&& body) noexcept : JoltRigidBody3D::JoltRigidBody3D(JoltRigidBody3D&& body) noexcept :
m_geom(std::move(body.m_geom)), m_geom(std::move(body.m_geom)),
m_body(body.m_body), m_body(std::move(body.m_body)),
m_world(body.m_world), m_world(std::move(body.m_world)),
m_bodyIndex(body.m_bodyIndex), m_bodyIndex(body.m_bodyIndex),
m_isSimulationEnabled(body.m_isSimulationEnabled), m_isSimulationEnabled(body.m_isSimulationEnabled),
m_isTrigger(body.m_isTrigger) m_isTrigger(body.m_isTrigger)
{ {
body.m_body = nullptr;
body.m_bodyIndex = std::numeric_limits<UInt32>::max(); body.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_body) if (m_body)
@ -343,14 +342,13 @@ namespace Nz
{ {
Destroy(); Destroy();
m_body = body.m_body; m_body = std::move(body.m_body);
m_bodyIndex = body.m_bodyIndex; m_bodyIndex = body.m_bodyIndex;
m_geom = std::move(body.m_geom); m_geom = std::move(body.m_geom);
m_world = body.m_world; m_world = std::move(body.m_world);
m_isSimulationEnabled = body.m_isSimulationEnabled; m_isSimulationEnabled = body.m_isSimulationEnabled;
m_isTrigger = body.m_isTrigger; m_isTrigger = body.m_isTrigger;
body.m_body = nullptr;
body.m_bodyIndex = std::numeric_limits<UInt32>::max(); body.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_body) if (m_body)
@ -417,9 +415,10 @@ namespace Nz
{ {
creationSettings.SetShape(m_world->GetNullShape()); creationSettings.SetShape(m_world->GetNullShape());
creationSettings.mIsSensor = true; //< not the best solution but enough for now creationSettings.mIsSensor = true; //< not the best solution but enough for now
creationSettings.mPosition = ToJolt(settings.position);
creationSettings.mRotation = ToJolt(settings.rotation);
} }
creationSettings.mPosition = ToJolt(settings.position);
creationSettings.mRotation = ToJolt(settings.rotation);
} }
bool JoltRigidBody3D::ShouldActivate() const bool JoltRigidBody3D::ShouldActivate() const

View File

@ -3,19 +3,20 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/JoltPhysics3D/Systems/JoltPhysics3DSystem.hpp> #include <Nazara/JoltPhysics3D/Systems/JoltPhysics3DSystem.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/Components/DisabledComponent.hpp> #include <Nazara/Core/Components/DisabledComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp> #include <Nazara/Utility/Components/NodeComponent.hpp>
#include <iostream>
#include <Nazara/JoltPhysics3D/Debug.hpp> #include <Nazara/JoltPhysics3D/Debug.hpp>
namespace Nz namespace Nz
{ {
JoltPhysics3DSystem::JoltPhysics3DSystem(entt::registry& registry) : JoltPhysics3DSystem::JoltPhysics3DSystem(entt::registry& registry) :
m_registry(registry), m_registry(registry),
m_physicsConstructObserver(m_registry, entt::collector.group<JoltRigidBody3DComponent, NodeComponent>()) m_characterConstructObserver(m_registry, entt::collector.group<JoltCharacterComponent, NodeComponent>(entt::exclude<DisabledComponent, JoltRigidBody3DComponent>)),
m_rigidBodyConstructObserver(m_registry, entt::collector.group<JoltRigidBody3DComponent, NodeComponent>(entt::exclude<DisabledComponent, JoltCharacterComponent>))
{ {
m_constructConnection = registry.on_construct<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnConstruct>(this); m_constructConnection = registry.on_construct<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnBodyConstruct>(this);
m_destructConnection = registry.on_destroy<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnDestruct>(this); m_destructConnection = registry.on_destroy<JoltRigidBody3DComponent>().connect<&JoltPhysics3DSystem::OnBodyDestruct>(this);
m_stepCount = 0; m_stepCount = 0;
m_physicsTime = Time::Zero(); m_physicsTime = Time::Zero();
@ -24,9 +25,14 @@ namespace Nz
JoltPhysics3DSystem::~JoltPhysics3DSystem() JoltPhysics3DSystem::~JoltPhysics3DSystem()
{ {
m_physicsConstructObserver.disconnect(); m_characterConstructObserver.disconnect();
m_rigidBodyConstructObserver.disconnect();
// Ensure every RigidBody3D is destroyed before world is // Ensure every RigidBody3D is destroyed before world is
auto characterView = m_registry.view<JoltCharacterComponent>();
for (auto [entity, characterComponent] : characterView.each())
characterComponent.Destroy();
auto rigidBodyView = m_registry.view<JoltRigidBody3DComponent>(); auto rigidBodyView = m_registry.view<JoltRigidBody3DComponent>();
for (auto [entity, rigidBodyComponent] : rigidBodyView.each()) for (auto [entity, rigidBodyComponent] : rigidBodyView.each())
rigidBodyComponent.Destroy(true); rigidBodyComponent.Destroy(true);
@ -37,10 +43,12 @@ namespace Nz
if (m_stepCount == 0) if (m_stepCount == 0)
m_stepCount = 1; m_stepCount = 1;
std::cout << "Physics time: " << (m_physicsTime / Time::Nanoseconds(m_stepCount)) << std::endl; /*
std::cout << "Replication time: " << (m_updateTime / Time::Nanoseconds(m_stepCount)) << std::endl; NazaraDebug("Physics time: " << (m_physicsTime / Time::Nanoseconds(m_stepCount)));
std::cout << "Active entity count: " << m_physWorld.GetActiveBodyCount() << std::endl; NazaraDebug("Replication time: " << (m_updateTime / Time::Nanoseconds(m_stepCount)));
std::cout << "--" << std::endl; NazaraDebug("Active entity count: " << m_physWorld.GetActiveBodyCount()));
NazaraDebug("--");
*/
m_stepCount = 0; m_stepCount = 0;
m_physicsTime = Time::Zero(); m_physicsTime = Time::Zero();
@ -86,14 +94,23 @@ namespace Nz
void JoltPhysics3DSystem::Update(Time elapsedTime) void JoltPhysics3DSystem::Update(Time elapsedTime)
{ {
// Move newly-created physics entities to their node position/rotation // Move newly-created physics entities to their node position/rotation
m_physicsConstructObserver.each([&](entt::entity entity) m_characterConstructObserver.each([this](entt::entity entity)
{ {
JoltRigidBody3DComponent& entityPhysics = m_registry.get<JoltRigidBody3DComponent>(entity); JoltCharacterComponent& entityCharacter = m_registry.get<JoltCharacterComponent>(entity);
NodeComponent& entityNode = m_registry.get<NodeComponent>(entity); NodeComponent& entityNode = m_registry.get<NodeComponent>(entity);
entityPhysics.TeleportTo(entityNode.GetPosition(), entityNode.GetRotation()); entityCharacter.TeleportTo(entityNode.GetPosition(), entityNode.GetRotation());
}); });
m_rigidBodyConstructObserver.each([this](entt::entity entity)
{
JoltRigidBody3DComponent& entityBody = m_registry.get<JoltRigidBody3DComponent>(entity);
NodeComponent& entityNode = m_registry.get<NodeComponent>(entity);
entityBody.TeleportTo(entityNode.GetPosition(), entityNode.GetRotation());
});
Time t1 = GetElapsedNanoseconds(); Time t1 = GetElapsedNanoseconds();
// Update the physics world // Update the physics world
@ -102,18 +119,36 @@ namespace Nz
Time t2 = GetElapsedNanoseconds(); Time t2 = GetElapsedNanoseconds();
// Replicate active rigid body position to their node components // Replicate characters to their NodeComponent
auto view = m_registry.view<NodeComponent, const JoltRigidBody3DComponent>(entt::exclude<DisabledComponent>);
for (auto entity : view)
{ {
auto& rigidBodyComponent = view.get<const JoltRigidBody3DComponent>(entity); auto view = m_registry.view<NodeComponent, const JoltCharacterComponent>(entt::exclude<DisabledComponent>);
if (!m_physWorld.IsBodyActive(rigidBodyComponent.GetBodyIndex())) for (auto entity : view)
continue; {
auto& characterComponent = view.get<const JoltCharacterComponent>(entity);
if (!m_physWorld.IsBodyActive(characterComponent.GetBodyIndex()))
continue;
auto& nodeComponent = view.get<NodeComponent>(entity); auto& nodeComponent = view.get<NodeComponent>(entity);
auto [position, rotation] = rigidBodyComponent.GetPositionAndRotation(); auto [position, rotation] = characterComponent.GetPositionAndRotation();
nodeComponent.SetTransform(position, rotation); nodeComponent.SetTransform(position, rotation);
}
}
// Replicate active rigid body position to their node components
{
auto view = m_registry.view<NodeComponent, const JoltRigidBody3DComponent>(entt::exclude<DisabledComponent>);
for (auto entity : m_registry.view<NodeComponent, const JoltRigidBody3DComponent>(entt::exclude<DisabledComponent>))
{
auto& rigidBodyComponent = view.get<const JoltRigidBody3DComponent>(entity);
if (!m_physWorld.IsBodyActive(rigidBodyComponent.GetBodyIndex()))
continue;
auto& nodeComponent = view.get<NodeComponent>(entity);
auto [position, rotation] = rigidBodyComponent.GetPositionAndRotation();
nodeComponent.SetTransform(position, rotation);
}
} }
Time t3 = GetElapsedNanoseconds(); Time t3 = GetElapsedNanoseconds();
@ -122,7 +157,7 @@ namespace Nz
m_updateTime += (t3 - t2); m_updateTime += (t3 - t2);
} }
void JoltPhysics3DSystem::OnConstruct(entt::registry& registry, entt::entity entity) void JoltPhysics3DSystem::OnBodyConstruct(entt::registry& registry, entt::entity entity)
{ {
// Register rigid body owning entity // Register rigid body owning entity
JoltRigidBody3DComponent& rigidBody = registry.get<JoltRigidBody3DComponent>(entity); JoltRigidBody3DComponent& rigidBody = registry.get<JoltRigidBody3DComponent>(entity);
@ -133,7 +168,7 @@ namespace Nz
m_physicsEntities[uniqueIndex] = entity; m_physicsEntities[uniqueIndex] = entity;
} }
void JoltPhysics3DSystem::OnDestruct(entt::registry& registry, entt::entity entity) void JoltPhysics3DSystem::OnBodyDestruct(entt::registry& registry, entt::entity entity)
{ {
// Unregister owning entity // Unregister owning entity
JoltRigidBody3DComponent& rigidBody = registry.get<JoltRigidBody3DComponent>(entity); JoltRigidBody3DComponent& rigidBody = registry.get<JoltRigidBody3DComponent>(entity);