Continue Jolt integration

This commit is contained in:
SirLynix
2023-03-21 13:31:52 +01:00
committed by Jérôme Leclercq
parent 21e08798ce
commit 021801f02e
35 changed files with 612 additions and 214 deletions

View File

@@ -7,7 +7,7 @@
#ifndef NAZARA_JOLTPHYSICS3D_COMPONENTS_JOLTRIGIDBODY3DCOMPONENT_HPP
#define NAZARA_JOLTPHYSICS3D_COMPONENTS_JOLTRIGIDBODY3DCOMPONENT_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/JoltRigidBody3D.hpp>
namespace Nz

View File

@@ -15,6 +15,9 @@ namespace Nz
Compound,
Sphere,
TranslatedRotatedDecoration,
ScaleDecoration,
Max = Sphere
};
}

View File

@@ -0,0 +1,59 @@
// 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_JOLTCHARACTER_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTCHARACTER_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <memory>
namespace JPH
{
class Character;
}
namespace Nz
{
class JoltCollider3D;
class JoltPhysWorld3D;
class NAZARA_JOLTPHYSICS3D_API JoltCharacter
{
friend JoltPhysWorld3D;
public:
JoltCharacter(JoltPhysWorld3D& physWorld, std::shared_ptr<JoltCollider3D> collider, const Vector3f& position, const Quaternionf& rotation = Quaternionf::Identity());
JoltCharacter(const JoltCharacter&) = delete;
JoltCharacter(JoltCharacter&&) = delete;
~JoltCharacter();
Vector3f GetLinearVelocity() const;
Quaternionf GetRotation() const;
Vector3f GetPosition() const;
std::pair<Vector3f, Quaternionf> GetPositionAndRotation() const;
bool IsOnGround() const;
void SetLinearVelocity(const Vector3f& linearVel);
JoltCharacter& operator=(const JoltCharacter&) = delete;
JoltCharacter& operator=(JoltCharacter&&) = delete;
private:
void PostSimulate();
std::shared_ptr<JoltCollider3D> m_collider;
std::unique_ptr<JPH::Character> m_character;
JoltPhysWorld3D& m_physicsWorld;
};
}
#include <Nazara/JoltPhysics3D/JoltCharacter.inl>
#endif // NAZARA_JOLTPHYSICS3D_JOLTCHARACTER_HPP

View File

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

View File

@@ -7,23 +7,21 @@
#ifndef NAZARA_JOLTPHYSICS3D_JOLTCOLLIDER3D_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTCOLLIDER3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/JoltPhysics3D/Enums.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utils/Signal.hpp>
#include <Nazara/Utils/SparsePtr.hpp>
#include <NazaraUtils/Signal.hpp>
#include <NazaraUtils/SparsePtr.hpp>
#include <memory>
namespace JPH
{
class ShapeSettings;
class BoxShapeSettings;
class CompoundShapeSettings;
class SphereShapeSettings;
}
namespace Nz
@@ -35,7 +33,7 @@ namespace Nz
class NAZARA_JOLTPHYSICS3D_API JoltCollider3D
{
public:
JoltCollider3D() = default;
JoltCollider3D();
JoltCollider3D(const JoltCollider3D&) = delete;
JoltCollider3D(JoltCollider3D&&) = delete;
virtual ~JoltCollider3D();
@@ -44,7 +42,7 @@ namespace Nz
virtual std::shared_ptr<StaticMesh> GenerateDebugMesh() const;
virtual JPH::ShapeSettings* GetShapeSettings() const = 0;
inline JPH::ShapeSettings* GetShapeSettings() const;
virtual JoltColliderType3D GetType() const = 0;
JoltCollider3D& operator=(const JoltCollider3D&) = delete;
@@ -52,25 +50,28 @@ namespace Nz
static std::shared_ptr<JoltCollider3D> Build(const PrimitiveList& list);
protected:
template<typename T> const T* GetShapeSettingsAs() const;
template<typename T> void SetupShapeSettings(std::unique_ptr<T> shapeSettings);
private:
static std::shared_ptr<JoltCollider3D> CreateGeomFromPrimitive(const Primitive& primitive);
std::unique_ptr<JPH::ShapeSettings> m_shapeSettings;
};
/*********************************** Shapes ******************************************/
class NAZARA_JOLTPHYSICS3D_API JoltBoxCollider3D final : public JoltCollider3D
{
public:
JoltBoxCollider3D(const Vector3f& lengths, float convexRadius = 0.f);
~JoltBoxCollider3D();
~JoltBoxCollider3D() = default;
void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override;
Vector3f GetLengths() const;
JPH::ShapeSettings* GetShapeSettings() const override;
JoltColliderType3D GetType() const override;
private:
std::unique_ptr<JPH::BoxShapeSettings> m_shapeSettings;
Vector3f m_lengths;
};
class NAZARA_JOLTPHYSICS3D_API JoltCompoundCollider3D final : public JoltCollider3D
@@ -79,12 +80,11 @@ namespace Nz
struct ChildCollider;
JoltCompoundCollider3D(std::vector<ChildCollider> childs);
~JoltCompoundCollider3D();
~JoltCompoundCollider3D() = default;
void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override;
const std::vector<ChildCollider>& GetGeoms() const;
JPH::ShapeSettings* GetShapeSettings() const override;
JoltColliderType3D GetType() const override;
struct ChildCollider
@@ -95,7 +95,6 @@ namespace Nz
};
private:
std::unique_ptr<JPH::CompoundShapeSettings> m_shapeSettings;
std::vector<ChildCollider> m_childs;
};
@@ -103,19 +102,32 @@ namespace Nz
{
public:
JoltSphereCollider3D(float radius);
~JoltSphereCollider3D();
~JoltSphereCollider3D() = default;
void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override;
float GetRadius() const;
JPH::ShapeSettings* GetShapeSettings() const override;
JoltColliderType3D GetType() const override;
};
/*********************************** Decorated ******************************************/
class NAZARA_JOLTPHYSICS3D_API JoltTranslatedRotatedCollider3D final : public JoltCollider3D
{
public:
inline JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation);
inline JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Quaternionf& rotation);
JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation, const Quaternionf& rotation);
~JoltTranslatedRotatedCollider3D() = default;
void BuildDebugMesh(std::vector<Vector3f>& vertices, std::vector<UInt16>& indices, const Matrix4f& offsetMatrix) const override;
JoltColliderType3D GetType() const override;
private:
std::unique_ptr<JPH::SphereShapeSettings> m_shapeSettings;
Vector3f m_position;
float m_radius;
std::shared_ptr<JoltCollider3D> m_collider;
};
}
#include <Nazara/JoltPhysics3D/JoltCollider3D.inl>

View File

@@ -2,11 +2,41 @@
// This file is part of the "Nazara Engine - JoltPhysics3D module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <NazaraUtils/Algorithm.hpp>
#include <memory>
#include <Nazara/JoltPhysics3D/Debug.hpp>
namespace Nz
{
inline JPH::ShapeSettings* JoltCollider3D::GetShapeSettings() const
{
return m_shapeSettings.get();
}
template<typename T>
const T* JoltCollider3D::GetShapeSettingsAs() const
{
return SafeCast<T*>(m_shapeSettings.get());
}
template<typename T>
void JoltCollider3D::SetupShapeSettings(std::unique_ptr<T> shapeSettings)
{
assert(!m_shapeSettings);
shapeSettings->SetEmbedded(); // Call SetEmbedded on the template type to prevent compiler to resolve it outside of a file including Jolt
m_shapeSettings = std::move(shapeSettings);
}
inline JoltTranslatedRotatedCollider3D::JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Vector3f& translation) :
JoltTranslatedRotatedCollider3D(std::move(collider), translation, Quaternionf::Identity())
{
}
inline JoltTranslatedRotatedCollider3D::JoltTranslatedRotatedCollider3D(std::shared_ptr<JoltCollider3D> collider, const Quaternionf& rotation) :
JoltTranslatedRotatedCollider3D(std::move(collider), Vector3f::Zero(), rotation)
{
}
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@@ -7,7 +7,7 @@
#ifndef NAZARA_JOLTPHYSICS3D_JOLTCONSTRAINT3D_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTCONSTRAINT3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/BulletPhysics3D/BulletPhysWorld3D.hpp>
#include <Nazara/BulletPhysics3D/BulletRigidBody3D.hpp>
#include <Nazara/BulletPhysics3D/Config.hpp>

View File

@@ -7,13 +7,15 @@
#ifndef NAZARA_JOLTPHYSICS3D_JOLTPHYSWORLD3D_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTPHYSWORLD3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utils/FunctionRef.hpp>
#include <Nazara/Utils/MovablePtr.hpp>
#include <NazaraUtils/FunctionRef.hpp>
#include <NazaraUtils/MovablePtr.hpp>
#include <atomic>
#include <vector>
namespace JPH
{
@@ -22,10 +24,13 @@ namespace JPH
namespace Nz
{
class JoltCharacter;
class JoltRigidBody3D;
class NAZARA_JOLTPHYSICS3D_API JoltPhysWorld3D
{
friend JoltCharacter;
public:
struct RaycastHit;
@@ -34,11 +39,14 @@ namespace Nz
JoltPhysWorld3D(JoltPhysWorld3D&& ph) = delete;
~JoltPhysWorld3D();
UInt32 GetActiveBodyCount() const;
Vector3f GetGravity() const;
std::size_t GetMaxStepCount() const;
JPH::PhysicsSystem* GetPhysicsSystem();
Time GetStepSize() const;
inline bool IsBodyActive(UInt32 bodyIndex) const;
bool RaycastQueryFirst(const Vector3f& from, const Vector3f& to, RaycastHit* hitInfo = nullptr);
void SetGravity(const Vector3f& gravity);
@@ -59,14 +67,24 @@ namespace Nz
};
private:
class BodyActivationListener;
friend BodyActivationListener;
struct JoltWorld;
inline void RegisterCharacter(JoltCharacter* character);
inline void UnregisterCharacter(JoltCharacter* character);
std::size_t m_maxStepCount;
std::unique_ptr<std::atomic_uint64_t[]> m_activeBodies;
std::unique_ptr<JoltWorld> m_world;
std::vector<JoltCharacter*> m_characters;
Vector3f m_gravity;
Time m_stepSize;
Time m_timestepAccumulator;
};
}
#include <Nazara/JoltPhysics3D/JoltPhysWorld3D.inl>
#endif // NAZARA_JOLTPHYSICS3D_JOLTPHYSWORLD3D_HPP

View File

@@ -0,0 +1,31 @@
// 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
{
inline bool JoltPhysWorld3D::IsBodyActive(UInt32 bodyIndex) const
{
UInt32 blockIndex = bodyIndex / 64;
UInt32 localIndex = bodyIndex % 64;
return m_activeBodies[blockIndex] & (UInt64(1u) << localIndex);
}
inline void JoltPhysWorld3D::RegisterCharacter(JoltCharacter* character)
{
auto it = std::lower_bound(m_characters.begin(), m_characters.end(), character);
m_characters.insert(it, character);
}
inline void JoltPhysWorld3D::UnregisterCharacter(JoltCharacter* character)
{
auto it = std::lower_bound(m_characters.begin(), m_characters.end(), character);
assert(*it == character);
m_characters.erase(it);
}
}
#include <Nazara/JoltPhysics3D/DebugOff.hpp>

View File

@@ -7,7 +7,7 @@
#ifndef NAZARA_JOLTPHYSICS3D_HPP
#define NAZARA_JOLTPHYSICS3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/Core/Core.hpp>
#include <memory>

View File

@@ -7,14 +7,19 @@
#ifndef NAZARA_JOLTPHYSICS3D_JOLTRIGIDBODY3D_HPP
#define NAZARA_JOLTPHYSICS3D_JOLTRIGIDBODY3D_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/JoltPhysics3D/JoltCollider3D.hpp>
#include <Nazara/JoltPhysics3D/Config.hpp>
#include <Nazara/Core/Enums.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Utils/MovablePtr.hpp>
#include <NazaraUtils/MovablePtr.hpp>
namespace JPH
{
class Body;
}
namespace Nz
{
@@ -41,6 +46,7 @@ namespace Nz
Boxf GetAABB() const;
float GetAngularDamping() const;
Vector3f GetAngularVelocity() const;
inline UInt32 GetBodyIndex() const;
inline const std::shared_ptr<JoltCollider3D>& GetGeom() const;
float GetLinearDamping() const;
Vector3f GetLinearVelocity() const;
@@ -48,6 +54,7 @@ namespace Nz
Vector3f GetMassCenter(CoordSys coordSys = CoordSys::Local) const;
Matrix4f GetMatrix() const;
Vector3f GetPosition() const;
std::pair<Vector3f, Quaternionf> GetPositionAndRotation() const;
Quaternionf GetRotation() const;
inline JoltPhysWorld3D* GetWorld() const;
@@ -60,11 +67,13 @@ namespace Nz
void SetGeom(std::shared_ptr<JoltCollider3D> geom, bool recomputeInertia = true);
void SetLinearDamping(float damping);
void SetLinearVelocity(const Vector3f& velocity);
void SetMass(float mass);
void SetMass(float mass, bool recomputeInertia = true);
void SetMassCenter(const Vector3f& center);
void SetPosition(const Vector3f& position);
void SetRotation(const Quaternionf& rotation);
void TeleportTo(const Vector3f& position, const Quaternionf& rotation);
Quaternionf ToLocal(const Quaternionf& worldRotation);
Vector3f ToLocal(const Vector3f& worldPosition);
Quaternionf ToWorld(const Quaternionf& localRotation);
@@ -80,6 +89,7 @@ namespace Nz
private:
std::shared_ptr<JoltCollider3D> m_geom;
JPH::Body* m_body;
JoltPhysWorld3D* m_world;
UInt32 m_bodyIndex;
};

View File

@@ -11,6 +11,11 @@ namespace Nz
return EnableSleeping(false);
}
inline UInt32 JoltRigidBody3D::GetBodyIndex() const
{
return m_bodyIndex;
}
inline const std::shared_ptr<JoltCollider3D>& JoltRigidBody3D::GetGeom() const
{
return m_geom;

View File

@@ -7,12 +7,12 @@
#ifndef NAZARA_JOLTPHYSICS3D_SYSTEMS_JOLTPHYSICS3DSYSTEM_HPP
#define NAZARA_JOLTPHYSICS3D_SYSTEMS_JOLTPHYSICS3DSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/JoltPhysics3D/JoltPhysWorld3D.hpp>
#include <Nazara/JoltPhysics3D/Components/JoltRigidBody3DComponent.hpp>
#include <Nazara/Core/Time.hpp>
#include <Nazara/Utils/TypeList.hpp>
#include <NazaraUtils/TypeList.hpp>
#include <entt/entt.hpp>
#include <vector>