From a4ba7d6115b07877325b4698596dc294cd7c4f57 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sat, 25 Mar 2023 14:27:08 +0100 Subject: [PATCH] JoltPhysics3D:: Improve characters --- .../Nazara/JoltPhysics3D/JoltCharacter.hpp | 10 +++++-- .../Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp | 5 ++++ src/Nazara/JoltPhysics3D/JoltCharacter.cpp | 29 ++++++++++++++---- src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp | 30 ++++++++++++++++++- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/include/Nazara/JoltPhysics3D/JoltCharacter.hpp b/include/Nazara/JoltPhysics3D/JoltCharacter.hpp index 813fd470f..8b2bfff57 100644 --- a/include/Nazara/JoltPhysics3D/JoltCharacter.hpp +++ b/include/Nazara/JoltPhysics3D/JoltCharacter.hpp @@ -41,13 +41,19 @@ namespace Nz bool IsOnGround() const; void SetLinearVelocity(const Vector3f& linearVel); + void SetRotation(const Quaternionf& rotation); + void SetUp(const Vector3f& up); + + void WakeUp(); JoltCharacter& operator=(const JoltCharacter&) = delete; JoltCharacter& operator=(JoltCharacter&&) = delete; - private: - void PostSimulate(); + protected: + virtual void PreSimulate(float elapsedTime); + virtual void PostSimulate(); + private: std::shared_ptr m_collider; std::unique_ptr m_character; JoltPhysWorld3D& m_physicsWorld; diff --git a/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp b/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp index 6b78a0c67..549ecac47 100644 --- a/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp +++ b/include/Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp @@ -70,8 +70,13 @@ namespace Nz class BodyActivationListener; friend BodyActivationListener; + class StepListener; + friend StepListener; + struct JoltWorld; + void OnPreStep(float deltatime); + inline void RegisterCharacter(JoltCharacter* character); inline void UnregisterCharacter(JoltCharacter* character); diff --git a/src/Nazara/JoltPhysics3D/JoltCharacter.cpp b/src/Nazara/JoltPhysics3D/JoltCharacter.cpp index 210c7b780..f837d3513 100644 --- a/src/Nazara/JoltPhysics3D/JoltCharacter.cpp +++ b/src/Nazara/JoltPhysics3D/JoltCharacter.cpp @@ -39,24 +39,24 @@ namespace Nz Vector3f JoltCharacter::GetLinearVelocity() const { - return FromJolt(m_character->GetLinearVelocity()); + return FromJolt(m_character->GetLinearVelocity(false)); } Quaternionf JoltCharacter::GetRotation() const { - return FromJolt(m_character->GetRotation()); + return FromJolt(m_character->GetRotation(false)); } Vector3f JoltCharacter::GetPosition() const { - return FromJolt(m_character->GetPosition()); + return FromJolt(m_character->GetPosition(false)); } std::pair JoltCharacter::GetPositionAndRotation() const { JPH::Vec3 position; JPH::Quat rotation; - m_character->GetPositionAndRotation(position, rotation); + m_character->GetPositionAndRotation(position, rotation, false); return { FromJolt(position), FromJolt(rotation) }; } @@ -68,7 +68,26 @@ namespace Nz void JoltCharacter::SetLinearVelocity(const Vector3f& linearVel) { - m_character->SetLinearVelocity(ToJolt(linearVel)); + m_character->SetLinearVelocity(ToJolt(linearVel), false); + } + + void JoltCharacter::SetRotation(const Quaternionf& rotation) + { + m_character->SetRotation(ToJolt(rotation), JPH::EActivation::Activate, false); + } + + void JoltCharacter::SetUp(const Vector3f& up) + { + m_character->SetUp(ToJolt(up)); + } + + void JoltCharacter::WakeUp() + { + m_character->Activate(false); + } + + void JoltCharacter::PreSimulate(float elapsedTime) + { } void JoltCharacter::PostSimulate() diff --git a/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp b/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp index f96d3cd33..cd34d0b36 100644 --- a/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp +++ b/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -245,12 +246,30 @@ namespace Nz JoltPhysWorld3D& m_physWorld; }; + class JoltPhysWorld3D::StepListener : public JPH::PhysicsStepListener + { + public: + StepListener(JoltPhysWorld3D& physWorld) : + m_physWorld(physWorld) + { + } + + void OnStep(float inDeltaTime, JPH::PhysicsSystem& /*inPhysicsSystem*/) override + { + m_physWorld.OnPreStep(inDeltaTime); + } + + private: + JoltPhysWorld3D& m_physWorld; + }; + struct JoltPhysWorld3D::JoltWorld { JPH::TempAllocatorImpl tempAllocator; JPH::PhysicsSystem physicsSystem; JoltPhysWorld3D::BodyActivationListener bodyActivationListener; + JoltPhysWorld3D::StepListener stepListener; DitchMeAsap::BPLayerInterfaceImpl layerInterface; DitchMeAsap::ObjectLayerPairFilterImpl objectLayerFilter; @@ -258,7 +277,8 @@ namespace Nz JoltWorld(JoltPhysWorld3D& world, JPH::uint tempAllocatorSize) : tempAllocator(tempAllocatorSize), - bodyActivationListener(world) + bodyActivationListener(world), + stepListener(world) { } @@ -279,6 +299,8 @@ namespace Nz m_world->physicsSystem.Init(0xFFFF, 0, 0xFFFF, 10 * 1024, m_world->layerInterface, m_world->objectBroadphaseLayerFilter, m_world->objectLayerFilter); m_world->physicsSystem.SetBodyActivationListener(&m_world->bodyActivationListener); + m_world->physicsSystem.AddStepListener(&m_world->stepListener); + std::size_t blockCount = (m_world->physicsSystem.GetMaxBodies() - 1) / 64 + 1; m_activeBodies = std::make_unique(blockCount); for (std::size_t i = 0; i < blockCount; ++i) @@ -390,4 +412,10 @@ namespace Nz stepCount++; } } + + void JoltPhysWorld3D::OnPreStep(float deltatime) + { + for (JoltCharacter* character : m_characters) + character->PreSimulate(deltatime); + } }