JoltPhysics3D:: Improve characters

This commit is contained in:
SirLynix 2023-03-25 14:27:08 +01:00 committed by Jérôme Leclercq
parent 2b0239b8f0
commit a4ba7d6115
4 changed files with 66 additions and 8 deletions

View File

@ -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<JoltCollider3D> m_collider;
std::unique_ptr<JPH::Character> m_character;
JoltPhysWorld3D& m_physicsWorld;

View File

@ -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);

View File

@ -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<Vector3f, Quaternionf> 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()

View File

@ -18,6 +18,7 @@
#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>
#include <Jolt/Physics/PhysicsSettings.h>
#include <Jolt/Physics/PhysicsSystem.h>
#include <Jolt/Physics/PhysicsStepListener.h>
#include <Jolt/Physics/Body/BodyActivationListener.h>
#include <cassert>
#include <iostream>
@ -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<std::atomic_uint64_t[]>(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);
}
}