Upgrade Physics2D and Physics3D

This commit is contained in:
Jérôme Leclercq
2021-05-24 19:12:21 +02:00
parent 4bcb63d776
commit 8b0b5295f7
25 changed files with 239 additions and 448 deletions

View File

@@ -9,8 +9,6 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Box.hpp>
@@ -30,17 +28,11 @@ namespace Nz
///TODO: SceneGeom
///TODO: TreeGeom
class Collider3D;
class PrimitiveList;
class PhysWorld3D;
using Collider3DConstRef = ObjectRef<const Collider3D>;
using Collider3DLibrary = ObjectLibrary<Collider3D>;
using Collider3DRef = ObjectRef<Collider3D>;
class NAZARA_PHYSICS3D_API Collider3D : public RefCounted
class NAZARA_PHYSICS3D_API Collider3D
{
friend Collider3DLibrary;
friend class Physics3D;
public:
@@ -62,7 +54,7 @@ namespace Nz
Collider3D& operator=(const Collider3D&) = delete;
Collider3D& operator=(Collider3D&&) = delete;
static Collider3DRef Build(const PrimitiveList& list);
static std::shared_ptr<Collider3D> Build(const PrimitiveList& list);
// Signals:
NazaraSignal(OnColliderRelease, const Collider3D* /*collider*/);
@@ -70,19 +62,9 @@ namespace Nz
protected:
virtual NewtonCollision* CreateHandle(PhysWorld3D* world) const = 0;
static bool Initialize();
static void Uninitialize();
mutable std::unordered_map<PhysWorld3D*, NewtonCollision*> m_handles;
static Collider3DLibrary::LibraryMap s_library;
};
class BoxCollider3D;
using BoxCollider3DConstRef = ObjectRef<const BoxCollider3D>;
using BoxCollider3DRef = ObjectRef<BoxCollider3D>;
class NAZARA_PHYSICS3D_API BoxCollider3D : public Collider3D
{
public:
@@ -95,8 +77,6 @@ namespace Nz
Vector3f GetLengths() const;
ColliderType3D GetType() const override;
template<typename... Args> static BoxCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
@@ -104,11 +84,6 @@ namespace Nz
Vector3f m_lengths;
};
class CapsuleCollider3D;
using CapsuleCollider3DConstRef = ObjectRef<const CapsuleCollider3D>;
using CapsuleCollider3DRef = ObjectRef<CapsuleCollider3D>;
class NAZARA_PHYSICS3D_API CapsuleCollider3D : public Collider3D
{
public:
@@ -119,8 +94,6 @@ namespace Nz
float GetRadius() const;
ColliderType3D GetType() const override;
template<typename... Args> static CapsuleCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
@@ -129,32 +102,20 @@ namespace Nz
float m_radius;
};
class CompoundCollider3D;
using CompoundCollider3DConstRef = ObjectRef<const CompoundCollider3D>;
using CompoundCollider3DRef = ObjectRef<CompoundCollider3D>;
class NAZARA_PHYSICS3D_API CompoundCollider3D : public Collider3D
{
public:
CompoundCollider3D(std::vector<Collider3DRef> geoms);
CompoundCollider3D(std::vector<std::shared_ptr<Collider3D>> geoms);
const std::vector<Collider3DRef>& GetGeoms() const;
const std::vector<std::shared_ptr<Collider3D>>& GetGeoms() const;
ColliderType3D GetType() const override;
template<typename... Args> static CompoundCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
std::vector<Collider3DRef> m_geoms;
std::vector<std::shared_ptr<Collider3D>> m_geoms;
};
class ConeCollider3D;
using ConeCollider3DConstRef = ObjectRef<const ConeCollider3D>;
using ConeCollider3DRef = ObjectRef<ConeCollider3D>;
class NAZARA_PHYSICS3D_API ConeCollider3D : public Collider3D
{
public:
@@ -165,8 +126,6 @@ namespace Nz
float GetRadius() const;
ColliderType3D GetType() const override;
template<typename... Args> static ConeCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
@@ -175,11 +134,6 @@ namespace Nz
float m_radius;
};
class ConvexCollider3D;
using ConvexCollider3DConstRef = ObjectRef<const ConvexCollider3D>;
using ConvexCollider3DRef = ObjectRef<ConvexCollider3D>;
class NAZARA_PHYSICS3D_API ConvexCollider3D : public Collider3D
{
public:
@@ -188,8 +142,6 @@ namespace Nz
ColliderType3D GetType() const override;
template<typename... Args> static ConvexCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
@@ -198,11 +150,6 @@ namespace Nz
float m_tolerance;
};
class CylinderCollider3D;
using CylinderCollider3DConstRef = ObjectRef<const CylinderCollider3D>;
using CylinderCollider3DRef = ObjectRef<CylinderCollider3D>;
class NAZARA_PHYSICS3D_API CylinderCollider3D : public Collider3D
{
public:
@@ -213,8 +160,6 @@ namespace Nz
float GetRadius() const;
ColliderType3D GetType() const override;
template<typename... Args> static CylinderCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
@@ -223,11 +168,6 @@ namespace Nz
float m_radius;
};
class NullCollider3D;
using NullCollider3DConstRef = ObjectRef<const NullCollider3D>;
using NullCollider3DRef = ObjectRef<NullCollider3D>;
class NAZARA_PHYSICS3D_API NullCollider3D : public Collider3D
{
public:
@@ -237,17 +177,10 @@ namespace Nz
ColliderType3D GetType() const override;
template<typename... Args> static NullCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;
};
class SphereCollider3D;
using SphereCollider3DConstRef = ObjectRef<const SphereCollider3D>;
using SphereCollider3DRef = ObjectRef<SphereCollider3D>;
class NAZARA_PHYSICS3D_API SphereCollider3D : public Collider3D
{
public:
@@ -260,8 +193,6 @@ namespace Nz
float GetRadius() const;
ColliderType3D GetType() const override;
template<typename... Args> static SphereCollider3DRef New(Args&&... args);
private:
NewtonCollision* CreateHandle(PhysWorld3D* world) const override;

View File

@@ -2,82 +2,12 @@
// This file is part of the "Nazara Engine - Physics 3D module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics3D/Collider3D.hpp>
#include <memory>
#include <Nazara/Physics3D/Debug.hpp>
namespace Nz
{
template<typename... Args>
BoxCollider3DRef BoxCollider3D::New(Args&&... args)
{
std::unique_ptr<BoxCollider3D> object(new BoxCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
CapsuleCollider3DRef CapsuleCollider3D::New(Args&&... args)
{
std::unique_ptr<CapsuleCollider3D> object(new CapsuleCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
CompoundCollider3DRef CompoundCollider3D::New(Args&&... args)
{
std::unique_ptr<CompoundCollider3D> object(new CompoundCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
ConeCollider3DRef ConeCollider3D::New(Args&&... args)
{
std::unique_ptr<ConeCollider3D> object(new ConeCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
ConvexCollider3DRef ConvexCollider3D::New(Args&&... args)
{
std::unique_ptr<ConvexCollider3D> object(new ConvexCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
CylinderCollider3DRef CylinderCollider3D::New(Args&&... args)
{
std::unique_ptr<CylinderCollider3D> object(new CylinderCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
NullCollider3DRef NullCollider3D::New(Args&&... args)
{
std::unique_ptr<NullCollider3D> object(new NullCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
SphereCollider3DRef SphereCollider3D::New(Args&&... args)
{
std::unique_ptr<SphereCollider3D> object(new SphereCollider3D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
}
#include <Nazara/Physics3D/DebugOff.hpp>

View File

@@ -9,21 +9,21 @@
namespace Nz
{
enum ColliderType3D
enum class ColliderType3D
{
ColliderType3D_Box,
ColliderType3D_Capsule,
ColliderType3D_Cone,
ColliderType3D_Compound,
ColliderType3D_ConvexHull,
ColliderType3D_Cylinder,
ColliderType3D_Heightfield,
ColliderType3D_Null,
ColliderType3D_Scene,
ColliderType3D_Sphere,
ColliderType3D_Tree,
Box,
Capsule,
Cone,
Compound,
ConvexHull,
Cylinder,
Heightfield,
Null,
Scene,
Sphere,
Tree,
ColliderType3D_Max = ColliderType3D_Tree
Max = Tree
};
}

View File

@@ -23,7 +23,7 @@ namespace Nz
struct Config {};
Physics3D(Config /*config*/);
~Physics3D();
~Physics3D() = default;
unsigned int GetMemoryUsed();

View File

@@ -25,7 +25,7 @@ namespace Nz
{
public:
RigidBody3D(PhysWorld3D* world, const Matrix4f& mat = Matrix4f::Identity());
RigidBody3D(PhysWorld3D* world, Collider3DRef geom, const Matrix4f& mat = Matrix4f::Identity());
RigidBody3D(PhysWorld3D* world, std::shared_ptr<Collider3D> geom, const Matrix4f& mat = Matrix4f::Identity());
RigidBody3D(const RigidBody3D& object);
RigidBody3D(RigidBody3D&& object);
~RigidBody3D();
@@ -40,7 +40,7 @@ namespace Nz
Boxf GetAABB() const;
Vector3f GetAngularDamping() const;
Vector3f GetAngularVelocity() const;
const Collider3DRef& GetGeom() const;
const std::shared_ptr<Collider3D>& GetGeom() const;
float GetGravityFactor() const;
NewtonBody* GetHandle() const;
float GetLinearDamping() const;
@@ -61,7 +61,7 @@ namespace Nz
void SetAngularDamping(const Vector3f& angularDamping);
void SetAngularVelocity(const Vector3f& angularVelocity);
void SetGeom(Collider3DRef geom);
void SetGeom(std::shared_ptr<Collider3D> geom);
void SetGravityFactor(float gravityFactor);
void SetLinearDamping(float damping);
void SetLinearVelocity(const Vector3f& velocity);
@@ -81,7 +81,7 @@ namespace Nz
static void ForceAndTorqueCallback(const NewtonBody* body, float timeStep, int threadIndex);
static void TransformCallback(const NewtonBody* body, const float* matrix, int threadIndex);
Collider3DRef m_geom;
std::shared_ptr<Collider3D> m_geom;
Matrix4f m_matrix;
Vector3f m_forceAccumulator;
Vector3f m_torqueAccumulator;