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

@@ -29,6 +29,7 @@
#ifndef NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP
#define NAZARA_JOLTPHYSICS3D_COMPONENTS_HPP
#include <Nazara/JoltPhysics3D/Components/JoltCharacterComponent.hpp>
#include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.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 <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysicsStepListener.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <NazaraUtils/MovablePtr.hpp>
#include <memory>
namespace JPH
@@ -21,22 +23,24 @@ namespace JPH
namespace Nz
{
class JoltCharacterImpl;
class JoltCollider3D;
class JoltPhysWorld3D;
class NAZARA_JOLTPHYSICS3D_API JoltCharacter
class NAZARA_JOLTPHYSICS3D_API JoltCharacter : public JoltPhysicsStepListener
{
friend JoltPhysWorld3D;
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(JoltCharacter&&) = delete;
JoltCharacter(JoltCharacter&& character) noexcept;
~JoltCharacter();
inline void DisableSleeping();
void EnableSleeping(bool enable);
inline UInt32 GetBodyIndex() const;
Vector3f GetLinearVelocity() const;
Quaternionf GetRotation() const;
Vector3f GetPosition() const;
@@ -46,23 +50,45 @@ namespace Nz
bool IsOnGround() const;
void SetFriction(float friction);
inline void SetImpl(std::shared_ptr<JoltCharacterImpl> characterImpl);
void SetLinearVelocity(const Vector3f& linearVel);
void SetRotation(const Quaternionf& rotation);
void SetUp(const Vector3f& up);
void TeleportTo(const Vector3f& position, const Quaternionf& rotation);
void WakeUp();
JoltCharacter& operator=(const JoltCharacter&) = delete;
JoltCharacter& operator=(JoltCharacter&&) = delete;
JoltCharacter& operator=(JoltCharacter&& character) noexcept;
protected:
virtual void PreSimulate(float elapsedTime);
virtual void PostSimulate();
void Destroy();
private:
void PostSimulate() override;
void PreSimulate(float elapsedTime) override;
std::shared_ptr<JoltCharacterImpl> m_impl;
std::shared_ptr<JoltCollider3D> m_collider;
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);
}
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>

View File

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

View File

@@ -22,17 +22,17 @@ namespace Nz
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);
m_characters.insert(it, character);
auto it = std::lower_bound(m_stepListeners.begin(), m_stepListeners.end(), stepListener);
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);
assert(*it == character);
m_characters.erase(it);
auto it = std::lower_bound(m_stepListeners.begin(), m_stepListeners.end(), stepListener);
assert(*it == stepListener);
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;
std::shared_ptr<JoltCollider3D> m_geom;
JPH::Body* m_body;
JoltPhysWorld3D* m_world;
MovablePtr<JPH::Body> m_body;
MovablePtr<JoltPhysWorld3D> m_world;
UInt32 m_bodyIndex;
bool m_isSimulationEnabled;
bool m_isTrigger;

View File

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

View File

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