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

@@ -8,7 +8,6 @@
#define NAZARA_COLLIDER2D_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Core/SparsePtr.hpp>
@@ -18,20 +17,15 @@
#include <Nazara/Physics2D/Enums.hpp>
#include <vector>
struct cpBody;
struct cpShape;
namespace Nz
{
class Collider2D;
class RigidBody2D;
using Collider2DConstRef = ObjectRef<const Collider2D>;
using Collider2DLibrary = ObjectLibrary<Collider2D>;
using Collider2DRef = ObjectRef<Collider2D>;
class NAZARA_PHYSICS2D_API Collider2D : public RefCounted
class NAZARA_PHYSICS2D_API Collider2D
{
friend Collider2DLibrary;
friend RigidBody2D;
friend class CompoundCollider2D; //< See CompoundCollider2D::CreateShapes
@@ -74,7 +68,7 @@ namespace Nz
NazaraSignal(OnColliderRelease, const Collider2D* /*collider*/);
protected:
virtual std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const = 0;
virtual std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const = 0;
UInt32 m_categoryMask;
UInt32 m_collisionGroup;
@@ -86,16 +80,9 @@ namespace Nz
unsigned int m_collisionId;
private:
virtual std::size_t GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const;
static Collider2DLibrary::LibraryMap s_library;
virtual std::size_t GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const;
};
class BoxCollider2D;
using BoxCollider2DConstRef = ObjectRef<const BoxCollider2D>;
using BoxCollider2DRef = ObjectRef<BoxCollider2D>;
class NAZARA_PHYSICS2D_API BoxCollider2D : public Collider2D
{
public:
@@ -110,20 +97,13 @@ namespace Nz
inline Vector2f GetSize() const;
ColliderType2D GetType() const override;
template<typename... Args> static BoxCollider2DRef New(Args&&... args);
private:
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
Rectf m_rect;
float m_radius;
};
class CircleCollider2D;
using CircleCollider2DConstRef = ObjectRef<const CircleCollider2D>;
using CircleCollider2DRef = ObjectRef<CircleCollider2D>;
class NAZARA_PHYSICS2D_API CircleCollider2D : public Collider2D
{
public:
@@ -136,50 +116,36 @@ namespace Nz
inline float GetRadius() const;
ColliderType2D GetType() const override;
template<typename... Args> static CircleCollider2DRef New(Args&&... args);
private:
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
Vector2f m_offset;
float m_radius;
};
class CompoundCollider2D;
using CompoundCollider2DConstRef = ObjectRef<const CompoundCollider2D>;
using CompoundCollider2DRef = ObjectRef<CompoundCollider2D>;
class NAZARA_PHYSICS2D_API CompoundCollider2D : public Collider2D
{
public:
CompoundCollider2D(std::vector<Collider2DRef> geoms);
CompoundCollider2D(std::vector<std::shared_ptr<Collider2D>> geoms);
Nz::Vector2f ComputeCenterOfMass() const override;
float ComputeMomentOfInertia(float mass) const override;
inline bool DoesOverrideCollisionProperties() const;
inline const std::vector<Collider2DRef>& GetGeoms() const;
inline const std::vector<std::shared_ptr<Collider2D>>& GetGeoms() const;
ColliderType2D GetType() const override;
inline void OverridesCollisionProperties(bool shouldOverride);
template<typename... Args> static CompoundCollider2DRef New(Args&&... args);
private:
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
std::size_t GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
std::vector<Collider2DRef> m_geoms;
std::vector<std::shared_ptr<Collider2D>> m_geoms;
bool m_doesOverrideCollisionProperties;
};
class ConvexCollider2D;
using ConvexCollider2DConstRef = ObjectRef<const ConvexCollider2D>;
using ConvexCollider2DRef = ObjectRef<ConvexCollider2D>;
class NAZARA_PHYSICS2D_API ConvexCollider2D : public Collider2D
{
public:
@@ -191,20 +157,13 @@ namespace Nz
ColliderType2D GetType() const override;
inline const std::vector<Vector2d>& GetVertices() const;
template<typename... Args> static ConvexCollider2DRef New(Args&&... args);
private:
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
std::vector<Vector2d> m_vertices;
float m_radius;
};
class NullCollider2D;
using NullCollider2DConstRef = ObjectRef<const NullCollider2D>;
using NullCollider2DRef = ObjectRef<NullCollider2D>;
class NAZARA_PHYSICS2D_API NullCollider2D : public Collider2D
{
public:
@@ -215,17 +174,10 @@ namespace Nz
ColliderType2D GetType() const override;
template<typename... Args> static NullCollider2DRef New(Args&&... args);
private:
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
};
class SegmentCollider2D;
using SegmentCollider2DConstRef = ObjectRef<const SegmentCollider2D>;
using SegmentCollider2DRef = ObjectRef<SegmentCollider2D>;
class NAZARA_PHYSICS2D_API SegmentCollider2D : public Collider2D
{
public:
@@ -243,10 +195,8 @@ namespace Nz
inline float GetThickness() const;
ColliderType2D GetType() const override;
template<typename... Args> static SegmentCollider2DRef New(Args&&... args);
private:
std::size_t CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const override;
std::size_t CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const override;
Vector2f m_first;
Vector2f m_firstNeighbor;

View File

@@ -115,14 +115,6 @@ namespace Nz
return m_rect.GetLengths();
}
template<typename... Args>
BoxCollider2DRef BoxCollider2D::New(Args&&... args)
{
std::unique_ptr<BoxCollider2D> object(new BoxCollider2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
inline const Vector2f& CircleCollider2D::GetOffset() const
{
@@ -134,21 +126,13 @@ namespace Nz
return m_radius;
}
template<typename... Args>
CircleCollider2DRef CircleCollider2D::New(Args&&... args)
{
std::unique_ptr<CircleCollider2D> object(new CircleCollider2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
inline bool Nz::CompoundCollider2D::DoesOverrideCollisionProperties() const
{
return m_doesOverrideCollisionProperties;
}
inline const std::vector<Collider2DRef>& CompoundCollider2D::GetGeoms() const
inline const std::vector<std::shared_ptr<Collider2D>>& CompoundCollider2D::GetGeoms() const
{
return m_geoms;
}
@@ -158,37 +142,12 @@ namespace Nz
m_doesOverrideCollisionProperties = shouldOverride;
}
template<typename... Args>
CompoundCollider2DRef CompoundCollider2D::New(Args&&... args)
{
std::unique_ptr<CompoundCollider2D> object(new CompoundCollider2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
inline const std::vector<Vector2d>& ConvexCollider2D::GetVertices() const
{
return m_vertices;
}
template<typename... Args>
ConvexCollider2DRef ConvexCollider2D::New(Args&&... args)
{
std::unique_ptr<ConvexCollider2D> object(new ConvexCollider2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
NullCollider2DRef NullCollider2D::New(Args&&... args)
{
std::unique_ptr<NullCollider2D> object(new NullCollider2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
SegmentCollider2D::SegmentCollider2D(const Vector2f& first, const Vector2f& second, float thickness) :
SegmentCollider2D(first, first, second, second, thickness)
@@ -233,15 +192,6 @@ namespace Nz
{
return m_thickness;
}
template<typename... Args>
SegmentCollider2DRef SegmentCollider2D::New(Args&&... args)
{
std::unique_ptr<SegmentCollider2D> object(new SegmentCollider2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
}
#include <Nazara/Physics2D/DebugOff.hpp>

View File

@@ -9,16 +9,16 @@
namespace Nz
{
enum ColliderType2D
enum class ColliderType2D
{
ColliderType2D_Box,
ColliderType2D_Compound,
ColliderType2D_Convex,
ColliderType2D_Circle,
ColliderType2D_Null,
ColliderType2D_Segment,
Box,
Compound,
Convex,
Circle,
Null,
Segment,
ColliderType2D_Max = ColliderType2D_Segment
Max = Segment
};
}

View File

@@ -30,7 +30,7 @@ namespace Nz
using VelocityFunc = std::function<void(RigidBody2D& body2D, const Nz::Vector2f& gravity, float damping, float deltaTime)>;
RigidBody2D(PhysWorld2D* world, float mass);
RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom);
RigidBody2D(PhysWorld2D* world, float mass, std::shared_ptr<Collider2D> geom);
RigidBody2D(const RigidBody2D& object);
RigidBody2D(RigidBody2D&& object) noexcept;
~RigidBody2D();
@@ -55,7 +55,7 @@ namespace Nz
inline Vector2f GetCenterOfGravity(CoordSys coordSys = CoordSys_Local) const;
float GetElasticity(std::size_t shapeIndex = 0) const;
float GetFriction(std::size_t shapeIndex = 0) const;
const Collider2DRef& GetGeom() const;
const std::shared_ptr<Collider2D>& GetGeom() const;
cpBody* GetHandle() const;
float GetMass() const;
Vector2f GetMassCenter(CoordSys coordSys = CoordSys_Local) const;
@@ -84,7 +84,7 @@ namespace Nz
void SetElasticity(std::size_t shapeIndex, float elasticity);
void SetFriction(float friction);
void SetFriction(std::size_t shapeIndex, float friction);
void SetGeom(Collider2DRef geom, bool recomputeMoment = true, bool recomputeMassCenter = true);
void SetGeom(std::shared_ptr<Collider2D> geom, bool recomputeMoment = true, bool recomputeMassCenter = true);
void SetMass(float mass, bool recomputeMoment = true);
void SetMassCenter(const Vector2f& center, CoordSys coordSys = CoordSys_Local);
void SetMomentOfInertia(float moment);
@@ -122,7 +122,7 @@ namespace Nz
Vector2f m_positionOffset;
VelocityFunc m_velocityFunc;
std::vector<cpShape*> m_shapes;
Collider2DRef m_geom;
std::shared_ptr<Collider2D> m_geom;
cpBody* m_handle;
void* m_userData;
PhysWorld2D* m_world;

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;