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;

View File

@ -25,20 +25,20 @@ namespace Ndk
friend class PhysicsSystem2D;
public:
CollisionComponent2D(Nz::Collider2DRef geom = Nz::Collider2DRef());
CollisionComponent2D(std::shared_ptr<Nz::Collider2D> geom = std::shared_ptr<Nz::Collider2D>());
CollisionComponent2D(const CollisionComponent2D& collision);
~CollisionComponent2D() = default;
Nz::Rectf GetAABB() const;
const Nz::Collider2DRef& GetGeom() const;
const std::shared_ptr<Nz::Collider2D>& GetGeom() const;
const Nz::Vector2f& GetGeomOffset() const;
void Recenter(const Nz::Vector2f& origin);
void SetGeom(Nz::Collider2DRef geom, bool recomputeMoment = true, bool recomputeMassCenter = true);
void SetGeom(std::shared_ptr<Nz::Collider2D> geom, bool recomputeMoment = true, bool recomputeMassCenter = true);
void SetGeomOffset(const Nz::Vector2f& geomOffset);
CollisionComponent2D& operator=(Nz::Collider2DRef geom);
CollisionComponent2D& operator=(std::shared_ptr<Nz::Collider2D> geom);
CollisionComponent2D& operator=(CollisionComponent2D&& collision) = delete;
static ComponentIndex componentIndex;
@ -56,7 +56,7 @@ namespace Ndk
void OnDetached() override;
std::unique_ptr<Nz::RigidBody2D> m_staticBody;
Nz::Collider2DRef m_geom;
std::shared_ptr<Nz::Collider2D> m_geom;
bool m_bodyUpdated;
};
}

View File

@ -10,7 +10,7 @@ namespace Ndk
* \param geom Reference to a geometry symbolizing the entity
*/
inline CollisionComponent2D::CollisionComponent2D(Nz::Collider2DRef geom) :
inline CollisionComponent2D::CollisionComponent2D(std::shared_ptr<Nz::Collider2D> geom) :
m_geom(std::move(geom)),
m_bodyUpdated(false)
{
@ -33,7 +33,7 @@ namespace Ndk
* \return A constant reference to the physics geometry
*/
inline const Nz::Collider2DRef& CollisionComponent2D::GetGeom() const
inline const std::shared_ptr<Nz::Collider2D>& CollisionComponent2D::GetGeom() const
{
return m_geom;
}
@ -45,7 +45,7 @@ namespace Ndk
* \param geom Reference to a geometry symbolizing the entity
*/
inline CollisionComponent2D& CollisionComponent2D::operator=(Nz::Collider2DRef geom)
inline CollisionComponent2D& CollisionComponent2D::operator=(std::shared_ptr<Nz::Collider2D> geom)
{
SetGeom(geom);

View File

@ -23,15 +23,15 @@ namespace Ndk
friend class PhysicsSystem3D;
public:
CollisionComponent3D(Nz::Collider3DRef geom = Nz::Collider3DRef());
CollisionComponent3D(std::shared_ptr<Nz::Collider3D> geom = std::shared_ptr<Nz::Collider3D>());
CollisionComponent3D(const CollisionComponent3D& collision);
~CollisionComponent3D() = default;
const Nz::Collider3DRef& GetGeom() const;
const std::shared_ptr<Nz::Collider3D>& GetGeom() const;
void SetGeom(Nz::Collider3DRef geom);
void SetGeom(std::shared_ptr<Nz::Collider3D> geom);
CollisionComponent3D& operator=(Nz::Collider3DRef geom);
CollisionComponent3D& operator=(std::shared_ptr<Nz::Collider3D> geom);
CollisionComponent3D& operator=(CollisionComponent3D&& collision) = delete;
static ComponentIndex componentIndex;
@ -48,7 +48,7 @@ namespace Ndk
void OnEntityEnabled() override;
std::unique_ptr<Nz::RigidBody3D> m_staticBody;
Nz::Collider3DRef m_geom;
std::shared_ptr<Nz::Collider3D> m_geom;
bool m_bodyUpdated;
};
}

View File

@ -10,7 +10,7 @@ namespace Ndk
* \param geom Reference to a geometry symbolizing the entity
*/
inline CollisionComponent3D::CollisionComponent3D(Nz::Collider3DRef geom) :
inline CollisionComponent3D::CollisionComponent3D(std::shared_ptr<Nz::Collider3D> geom) :
m_geom(std::move(geom)),
m_bodyUpdated(false)
{
@ -33,7 +33,7 @@ namespace Ndk
* \return A constant reference to the physics geometry
*/
inline const Nz::Collider3DRef& CollisionComponent3D::GetGeom() const
inline const std::shared_ptr<Nz::Collider3D>& CollisionComponent3D::GetGeom() const
{
return m_geom;
}
@ -45,7 +45,7 @@ namespace Ndk
* \param geom Reference to a geometry symbolizing the entity
*/
inline CollisionComponent3D& CollisionComponent3D::operator=(Nz::Collider3DRef geom)
inline CollisionComponent3D& CollisionComponent3D::operator=(std::shared_ptr<Nz::Collider3D> geom)
{
SetGeom(geom);

View File

@ -3,6 +3,8 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Physics2D/Collider2D.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/StackArray.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Physics2D/RigidBody2D.hpp>
#include <Nazara/Physics2D/PhysWorld2D.hpp>
@ -13,6 +15,16 @@
namespace Nz
{
namespace
{
constexpr cpSpaceDebugColor white = { 1.f, 1.f, 1.f, 1.f };
Vector2f FromChipmunk(const cpVect& v)
{
return Vector2f(float(v.x), float(v.y));
}
}
Collider2D::~Collider2D() = default;
void Collider2D::ForEachPolygon(const std::function<void(const Vector2f* vertices, std::size_t vertexCount)>& callback) const
@ -20,65 +32,108 @@ namespace Nz
// Currently, the only way to get only the polygons of a shape is to create a temporary cpSpace containing only this shape
// A better way to do this would be to reimplement this function in every subclass type in the very same way chipmunk does
PhysWorld2D physWorld;
RigidBody2D rigidBody(&physWorld, 0.f);
cpSpace* space = cpSpaceNew();
if (!space)
throw std::runtime_error("failed to create chipmunk space");
std::vector<cpShape*> shapeVector;
rigidBody.SetGeom(const_cast<Collider2D*>(this), false, false); //< Won't be used for writing, but still ugly
CallOnExit spaceRRID([&] { cpSpaceFree(space); });
PhysWorld2D::DebugDrawOptions drawCallbacks;
drawCallbacks.circleCallback = [&](const Vector2f& origin, const RadianAnglef& /*rotation*/, float radius, Nz::Color /*outlineColor*/, Nz::Color /*fillColor*/, void* /*userData*/)
cpBody* body = cpSpaceGetStaticBody(space);
std::vector<cpShape*> shapes;
CreateShapes(body, &shapes);
CallOnExit shapeRRID([&]
{
for (cpShape* shape : shapes)
cpShapeDestroy(shape);
});
for (cpShape* shape : shapes)
cpSpaceAddShape(space, shape);
using CallbackType = std::decay_t<decltype(callback)>;
cpSpaceDebugDrawOptions drawOptions;
drawOptions.collisionPointColor = white;
drawOptions.constraintColor = white;
drawOptions.shapeOutlineColor = white;
drawOptions.data = const_cast<void*>(static_cast<const void*>(&callback));
drawOptions.flags = CP_SPACE_DEBUG_DRAW_SHAPES;
// Callback trampoline
drawOptions.colorForShape = [](cpShape* /*shape*/, cpDataPointer /*userdata*/) { return white; };
drawOptions.drawCircle = [](cpVect pos, cpFloat /*angle*/, cpFloat radius, cpSpaceDebugColor /*outlineColor*/, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
{
const auto& callback = *static_cast<const CallbackType*>(userdata);
constexpr std::size_t circleVerticesCount = 20;
std::array<Vector2f, circleVerticesCount> vertices;
Vector2f origin = FromChipmunk(pos);
float r = static_cast<float>(radius);
RadianAnglef angleBetweenVertices = 2.f * float(M_PI) / vertices.size();
for (std::size_t i = 0; i < vertices.size(); ++i)
{
RadianAnglef angle = float(i) * angleBetweenVertices;
std::pair<float, float> sincos = angle.GetSinCos();
vertices[i] = origin + Vector2f(radius * sincos.first, radius * sincos.second);
vertices[i] = origin + Vector2f(r * sincos.first, r * sincos.second);
}
callback(vertices.data(), vertices.size());
};
drawCallbacks.polygonCallback = [&](const Vector2f* vertices, std::size_t vertexCount, float radius, Nz::Color /*outlineColor*/, Nz::Color /*fillColor*/, void* /*userData*/)
{
//TODO: Handle radius
callback(vertices, vertexCount);
};
drawOptions.drawDot = [](cpFloat /*size*/, cpVect /*pos*/, cpSpaceDebugColor /*color*/, cpDataPointer /*userdata*/) {}; //< Dummy
drawCallbacks.segmentCallback = [&](const Vector2f& first, const Vector2f& second, Nz::Color /*color*/, void* /*userData*/)
drawOptions.drawFatSegment = [](cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor /*outlineColor*/, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
{
std::array<Vector2f, 2> vertices = { first, second };
const auto& callback = *static_cast<const CallbackType*>(userdata);
callback(vertices.data(), vertices.size());
};
drawCallbacks.thickSegmentCallback = [&](const Vector2f& first, const Vector2f& second, float thickness, Nz::Color /*outlineColor*/, Nz::Color /*fillColor*/, void* /*userData*/)
{
static std::pair<float, float> sincos = Nz::DegreeAnglef(90.f).GetSinCos();
Vector2f normal = Vector2f::Normalize(second - first);
Vector2f from = FromChipmunk(a);
Vector2f to = FromChipmunk(b);
Vector2f normal = Vector2f::Normalize(to - from);
Vector2f thicknessNormal(sincos.second * normal.x - sincos.first * normal.y,
sincos.first * normal.x + sincos.second * normal.y);
float thickness = static_cast<float>(radius);
std::array<Vector2f, 4> vertices;
vertices[0] = first + thickness * thicknessNormal;
vertices[1] = first - thickness * thicknessNormal;
vertices[2] = second - thickness * thicknessNormal;
vertices[3] = second + thickness * thicknessNormal;
vertices[0] = from + thickness * thicknessNormal;
vertices[1] = from - thickness * thicknessNormal;
vertices[2] = to - thickness * thicknessNormal;
vertices[3] = to + thickness * thicknessNormal;
callback(vertices.data(), vertices.size());
};
physWorld.DebugDraw(drawCallbacks, true, false, false);
drawOptions.drawPolygon = [](int vertexCount, const cpVect* vertices, cpFloat /*radius*/, cpSpaceDebugColor /*outlineColor*/, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
{
const auto& callback = *static_cast<const CallbackType*>(userdata);
StackArray<Vector2f> nVertices = NazaraStackArray(Vector2f, vertexCount);
for (int i = 0; i < vertexCount; ++i)
nVertices[i].Set(float(vertices[i].x), float(vertices[i].y));
callback(nVertices.data(), nVertices.size());
};
drawOptions.drawSegment = [](cpVect a, cpVect b, cpSpaceDebugColor /*fillColor*/, cpDataPointer userdata)
{
const auto& callback = *static_cast<const CallbackType*>(userdata);
std::array<Vector2f, 2> vertices = { FromChipmunk(a), FromChipmunk(b) };
callback(vertices.data(), vertices.size());
};
cpSpaceDebugDraw(space, &drawOptions);
}
std::size_t Collider2D::GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t Collider2D::GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
std::size_t shapeCount = CreateShapes(body, shapes);
@ -123,12 +178,12 @@ namespace Nz
ColliderType2D BoxCollider2D::GetType() const
{
return ColliderType2D_Box;
return ColliderType2D::Box;
}
std::size_t BoxCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t BoxCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
shapes->push_back(cpBoxShapeNew2(body->GetHandle(), cpBBNew(m_rect.x, m_rect.y, m_rect.x + m_rect.width, m_rect.y + m_rect.height), m_radius));
shapes->push_back(cpBoxShapeNew2(body, cpBBNew(m_rect.x, m_rect.y, m_rect.x + m_rect.width, m_rect.y + m_rect.height), m_radius));
return 1;
}
@ -152,18 +207,18 @@ namespace Nz
ColliderType2D CircleCollider2D::GetType() const
{
return ColliderType2D_Circle;
return ColliderType2D::Circle;
}
std::size_t CircleCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t CircleCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
shapes->push_back(cpCircleShapeNew(body->GetHandle(), m_radius, cpv(m_offset.x, m_offset.y)));
shapes->push_back(cpCircleShapeNew(body, m_radius, cpv(m_offset.x, m_offset.y)));
return 1;
}
/******************************** CompoundCollider2D *********************************/
CompoundCollider2D::CompoundCollider2D(std::vector<Collider2DRef> geoms) :
CompoundCollider2D::CompoundCollider2D(std::vector<std::shared_ptr<Collider2D>> geoms) :
m_geoms(std::move(geoms)),
m_doesOverrideCollisionProperties(true)
{
@ -191,10 +246,10 @@ namespace Nz
ColliderType2D CompoundCollider2D::GetType() const
{
return ColliderType2D_Compound;
return ColliderType2D::Compound;
}
std::size_t CompoundCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t CompoundCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
// Since C++ does not allow protected call from other objects, we have to be a friend of Collider2D, yay
@ -205,7 +260,7 @@ namespace Nz
return shapeCount;
}
std::size_t CompoundCollider2D::GenerateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t CompoundCollider2D::GenerateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
// This is our parent's default behavior
if (m_doesOverrideCollisionProperties)
@ -248,12 +303,12 @@ namespace Nz
ColliderType2D ConvexCollider2D::GetType() const
{
return ColliderType2D_Convex;
return ColliderType2D::Convex;
}
std::size_t ConvexCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t ConvexCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
shapes->push_back(cpPolyShapeNew(body->GetHandle(), int(m_vertices.size()), reinterpret_cast<const cpVect*>(m_vertices.data()), cpTransformIdentity, m_radius));
shapes->push_back(cpPolyShapeNew(body, int(m_vertices.size()), reinterpret_cast<const cpVect*>(m_vertices.data()), cpTransformIdentity, m_radius));
return 1;
}
@ -261,7 +316,7 @@ namespace Nz
ColliderType2D NullCollider2D::GetType() const
{
return ColliderType2D_Null;
return ColliderType2D::Null;
}
Nz::Vector2f NullCollider2D::ComputeCenterOfMass() const
@ -274,7 +329,7 @@ namespace Nz
return (mass > 0.f) ? 1.f : 0.f; //< Null inertia is only possible for static/kinematic objects
}
std::size_t NullCollider2D::CreateShapes(RigidBody2D* /*body*/, std::vector<cpShape*>* /*shapes*/) const
std::size_t NullCollider2D::CreateShapes(cpBody* /*body*/, std::vector<cpShape*>* /*shapes*/) const
{
return 0;
}
@ -293,12 +348,12 @@ namespace Nz
ColliderType2D SegmentCollider2D::GetType() const
{
return ColliderType2D_Segment;
return ColliderType2D::Segment;
}
std::size_t SegmentCollider2D::CreateShapes(RigidBody2D* body, std::vector<cpShape*>* shapes) const
std::size_t SegmentCollider2D::CreateShapes(cpBody* body, std::vector<cpShape*>* shapes) const
{
cpShape* segment = cpSegmentShapeNew(body->GetHandle(), cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness);
cpShape* segment = cpSegmentShapeNew(body, cpv(m_first.x, m_first.y), cpv(m_second.x, m_second.y), m_thickness);
cpSegmentShapeSetNeighbors(segment, cpv(m_firstNeighbor.x, m_firstNeighbor.y), cpv(m_secondNeighbor.x, m_secondNeighbor.y));
shapes->push_back(segment);

View File

@ -18,7 +18,7 @@ namespace Nz
{
}
RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom) :
RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, std::shared_ptr<Collider2D> geom) :
m_positionOffset(Vector2f::Zero()),
m_geom(),
m_userData(nullptr),
@ -233,7 +233,7 @@ namespace Nz
return float(cpShapeGetFriction(m_shapes[shapeIndex]));
}
const Collider2DRef& RigidBody2D::GetGeom() const
const std::shared_ptr<Collider2D>& RigidBody2D::GetGeom() const
{
return m_geom;
}
@ -374,7 +374,7 @@ namespace Nz
cpShapeSetFriction(m_shapes[shapeIndex], cpFloat(friction));
}
void RigidBody2D::SetGeom(Collider2DRef geom, bool recomputeMoment, bool recomputeMassCenter)
void RigidBody2D::SetGeom(std::shared_ptr<Collider2D> geom, bool recomputeMoment, bool recomputeMassCenter)
{
// We have no public way of getting rid of an existing geom without removing the whole body
// So let's save some attributes of the body, destroy it and rebuild it
@ -393,11 +393,11 @@ namespace Nz
}
if (geom)
m_geom = geom;
m_geom = std::move(geom);
else
m_geom = NullCollider2D::New();
m_geom = std::make_shared<NullCollider2D>();
m_geom->GenerateShapes(this, &m_shapes);
m_geom->GenerateShapes(m_handle, &m_shapes);
for (cpShape* shape : m_shapes)
cpShapeSetUserData(shape, this);

View File

@ -12,26 +12,26 @@ namespace Nz
{
namespace
{
Collider3DRef CreateGeomFromPrimitive(const Primitive& primitive)
std::shared_ptr<Collider3D> CreateGeomFromPrimitive(const Primitive& primitive)
{
switch (primitive.type)
{
case PrimitiveType_Box:
return BoxCollider3D::New(primitive.box.lengths, primitive.matrix);
return std::make_shared<BoxCollider3D>(primitive.box.lengths, primitive.matrix);
case PrimitiveType_Cone:
return ConeCollider3D::New(primitive.cone.length, primitive.cone.radius, primitive.matrix);
return std::make_shared<ConeCollider3D>(primitive.cone.length, primitive.cone.radius, primitive.matrix);
case PrimitiveType_Plane:
return BoxCollider3D::New(Vector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
return std::make_shared<BoxCollider3D>(Vector3f(primitive.plane.size.x, 0.01f, primitive.plane.size.y), primitive.matrix);
///TODO: PlaneGeom?
case PrimitiveType_Sphere:
return SphereCollider3D::New(primitive.sphere.size, primitive.matrix.GetTranslation());
return std::make_shared<SphereCollider3D>(primitive.sphere.size, primitive.matrix.GetTranslation());
}
NazaraError("Primitive type not handled (0x" + NumberToString(primitive.type, 16) + ')');
return Collider3DRef();
return std::shared_ptr<Collider3D>();
}
}
@ -148,42 +148,24 @@ namespace Nz
return it->second;
}
Collider3DRef Collider3D::Build(const PrimitiveList& list)
std::shared_ptr<Collider3D> Collider3D::Build(const PrimitiveList& list)
{
std::size_t primitiveCount = list.GetSize();
if (primitiveCount > 1)
{
std::vector<Collider3DRef> geoms(primitiveCount);
std::vector<std::shared_ptr<Collider3D>> geoms(primitiveCount);
for (unsigned int i = 0; i < primitiveCount; ++i)
geoms[i] = CreateGeomFromPrimitive(list.GetPrimitive(i));
return CompoundCollider3D::New(std::move(geoms));
return std::make_shared<CompoundCollider3D>(std::move(geoms));
}
else if (primitiveCount > 0)
return CreateGeomFromPrimitive(list.GetPrimitive(0));
else
return NullCollider3D::New();
return std::make_shared<NullCollider3D>();
}
bool Collider3D::Initialize()
{
if (!Collider3DLibrary::Initialize())
{
NazaraError("Failed to initialise library");
return false;
}
return true;
}
void Collider3D::Uninitialize()
{
Collider3DLibrary::Uninitialize();
}
Collider3DLibrary::LibraryMap Collider3D::s_library;
/********************************** BoxCollider3D **********************************/
BoxCollider3D::BoxCollider3D(const Vector3f& lengths, const Matrix4f& transformMatrix) :
@ -220,7 +202,7 @@ namespace Nz
ColliderType3D BoxCollider3D::GetType() const
{
return ColliderType3D_Box;
return ColliderType3D::Box;
}
NewtonCollision* BoxCollider3D::CreateHandle(PhysWorld3D* world) const
@ -254,7 +236,7 @@ namespace Nz
ColliderType3D CapsuleCollider3D::GetType() const
{
return ColliderType3D_Capsule;
return ColliderType3D::Capsule;
}
NewtonCollision* CapsuleCollider3D::CreateHandle(PhysWorld3D* world) const
@ -264,19 +246,19 @@ namespace Nz
/******************************* CompoundCollider3D ********************************/
CompoundCollider3D::CompoundCollider3D(std::vector<Collider3DRef> geoms) :
CompoundCollider3D::CompoundCollider3D(std::vector<std::shared_ptr<Collider3D>> geoms) :
m_geoms(std::move(geoms))
{
}
const std::vector<Collider3DRef>& CompoundCollider3D::GetGeoms() const
const std::vector<std::shared_ptr<Collider3D>>& CompoundCollider3D::GetGeoms() const
{
return m_geoms;
}
ColliderType3D CompoundCollider3D::GetType() const
{
return ColliderType3D_Compound;
return ColliderType3D::Compound;
}
NewtonCollision* CompoundCollider3D::CreateHandle(PhysWorld3D* world) const
@ -284,12 +266,12 @@ namespace Nz
NewtonCollision* compoundCollision = NewtonCreateCompoundCollision(world->GetHandle(), 0);
NewtonCompoundCollisionBeginAddRemove(compoundCollision);
for (const Collider3DRef& geom : m_geoms)
for (const std::shared_ptr<Collider3D>& geom : m_geoms)
{
if (geom->GetType() == ColliderType3D_Compound)
if (geom->GetType() == ColliderType3D::Compound)
{
CompoundCollider3D* compoundGeom = static_cast<CompoundCollider3D*>(geom.Get());
for (const Collider3DRef& piece : compoundGeom->GetGeoms())
CompoundCollider3D& compoundGeom = static_cast<CompoundCollider3D&>(*geom);
for (const std::shared_ptr<Collider3D>& piece : compoundGeom.GetGeoms())
NewtonCompoundCollisionAddSubCollision(compoundCollision, piece->GetHandle(world));
}
else
@ -326,7 +308,7 @@ namespace Nz
ColliderType3D ConeCollider3D::GetType() const
{
return ColliderType3D_Cone;
return ColliderType3D::Cone;
}
NewtonCollision* ConeCollider3D::CreateHandle(PhysWorld3D* world) const
@ -357,7 +339,7 @@ namespace Nz
ColliderType3D ConvexCollider3D::GetType() const
{
return ColliderType3D_ConvexHull;
return ColliderType3D::ConvexHull;
}
NewtonCollision* ConvexCollider3D::CreateHandle(PhysWorld3D* world) const
@ -391,7 +373,7 @@ namespace Nz
ColliderType3D CylinderCollider3D::GetType() const
{
return ColliderType3D_Cylinder;
return ColliderType3D::Cylinder;
}
NewtonCollision* CylinderCollider3D::CreateHandle(PhysWorld3D* world) const
@ -407,7 +389,7 @@ namespace Nz
ColliderType3D NullCollider3D::GetType() const
{
return ColliderType3D_Null;
return ColliderType3D::Null;
}
void NullCollider3D::ComputeInertialMatrix(Vector3f* inertia, Vector3f* center) const
@ -458,7 +440,7 @@ namespace Nz
ColliderType3D SphereCollider3D::GetType() const
{
return ColliderType3D_Sphere;
return ColliderType3D::Sphere;
}
NewtonCollision* SphereCollider3D::CreateHandle(PhysWorld3D* world) const

View File

@ -16,13 +16,6 @@ namespace Nz
Physics3D::Physics3D(Config /*config*/) :
ModuleBase("Physics3D", this)
{
if (!Collider3D::Initialize())
throw std::runtime_error("failed to initialize colliders");
}
Physics3D::~Physics3D()
{
Collider3D::Uninitialize();
}
unsigned int Physics3D::GetMemoryUsed()

View File

@ -12,11 +12,11 @@
namespace Nz
{
RigidBody3D::RigidBody3D(PhysWorld3D* world, const Matrix4f& mat) :
RigidBody3D(world, NullCollider3D::New(), mat)
RigidBody3D(world, std::make_shared<NullCollider3D>(), mat)
{
}
RigidBody3D::RigidBody3D(PhysWorld3D* world, Collider3DRef geom, const Matrix4f& mat) :
RigidBody3D::RigidBody3D(PhysWorld3D* world, std::shared_ptr<Collider3D> geom, const Matrix4f& mat) :
m_geom(std::move(geom)),
m_matrix(mat),
m_forceAccumulator(Vector3f::Zero()),
@ -28,7 +28,7 @@ namespace Nz
NazaraAssert(m_world, "Invalid world");
if (!m_geom)
m_geom = NullCollider3D::New();
m_geom = std::make_shared<NullCollider3D>();
m_body = NewtonCreateDynamicBody(m_world->GetHandle(), m_geom->GetHandle(m_world), m_matrix);
NewtonBodySetUserData(m_body, this);
@ -165,7 +165,7 @@ namespace Nz
return angularVelocity;
}
const Collider3DRef& RigidBody3D::GetGeom() const
const std::shared_ptr<Collider3D>& RigidBody3D::GetGeom() const
{
return m_geom;
}
@ -276,14 +276,14 @@ namespace Nz
NewtonBodySetOmega(m_body, &angularVelocity.x);
}
void RigidBody3D::SetGeom(Collider3DRef geom)
void RigidBody3D::SetGeom(std::shared_ptr<Collider3D> geom)
{
if (m_geom.Get() != geom)
if (m_geom != geom)
{
if (geom)
m_geom = geom;
m_geom = std::move(geom);
else
m_geom = NullCollider3D::New();
m_geom = std::make_shared<NullCollider3D>();
NewtonBodySetCollision(m_body, m_geom->GetHandle(m_world));
}

View File

@ -53,7 +53,7 @@ namespace Ndk
*
* \param geom Geometry used for collisions
*/
void CollisionComponent2D::SetGeom(Nz::Collider2DRef geom, bool recomputeMoment, bool recomputeMassCenter)
void CollisionComponent2D::SetGeom(std::shared_ptr<Nz::Collider2D> geom, bool recomputeMoment, bool recomputeMassCenter)
{
m_geom = std::move(geom);

View File

@ -24,7 +24,7 @@ namespace Ndk
* \remark Produces a NazaraAssert if the entity has no physics component and has no static body
*/
void CollisionComponent3D::SetGeom(Nz::Collider3DRef geom)
void CollisionComponent3D::SetGeom(std::shared_ptr<Nz::Collider3D> geom)
{
m_geom = std::move(geom);

View File

@ -33,7 +33,7 @@ namespace Ndk
Nz::Vector2f positionOffset;
Nz::Collider2DRef geom;
std::shared_ptr<Nz::Collider2D> geom;
if (m_entity->HasComponent<CollisionComponent2D>())
{
const CollisionComponent2D& entityCollision = m_entity->GetComponent<CollisionComponent2D>();
@ -94,7 +94,7 @@ namespace Ndk
if (IsComponent<CollisionComponent2D>(component))
{
NazaraAssert(m_object, "Invalid object");
m_object->SetGeom(Nz::NullCollider2D::New(), false, false);
m_object->SetGeom(std::make_shared<Nz::NullCollider2D>(), false, false);
}
}

View File

@ -30,7 +30,7 @@ namespace Ndk
Nz::PhysWorld3D& world = entityWorld->GetSystem<PhysicsSystem3D>().GetWorld();
Nz::Collider3DRef geom;
std::shared_ptr<Nz::Collider3D> geom;
if (m_entity->HasComponent<CollisionComponent3D>())
geom = m_entity->GetComponent<CollisionComponent3D>().GetGeom();
@ -81,7 +81,7 @@ namespace Ndk
{
NazaraAssert(m_object, "Invalid object");
m_object->SetGeom(Nz::NullCollider3D::New());
m_object->SetGeom(std::make_shared<Nz::NullCollider3D>());
}
}

View File

@ -14,7 +14,7 @@ SCENARIO("Collider2D", "[PHYSICS2D][COLLIDER2D]")
{
CHECK(box.GetRect() == aabb);
CHECK(box.GetSize() == aabb.GetLengths());
CHECK(box.GetType() == Nz::ColliderType2D_Box);
CHECK(box.GetType() == Nz::ColliderType2D::Box);
}
}
@ -28,7 +28,7 @@ SCENARIO("Collider2D", "[PHYSICS2D][COLLIDER2D]")
{
CHECK(box.GetRect() == aabb);
CHECK(box.GetSize() == vec);
CHECK(box.GetType() == Nz::ColliderType2D_Box);
CHECK(box.GetType() == Nz::ColliderType2D::Box);
}
}
@ -41,25 +41,25 @@ SCENARIO("Collider2D", "[PHYSICS2D][COLLIDER2D]")
THEN("We expect those to be true")
{
CHECK(circle.GetRadius() == Approx(radius));
CHECK(circle.GetType() == Nz::ColliderType2D_Circle);
CHECK(circle.GetType() == Nz::ColliderType2D::Circle);
}
}
WHEN("We construct a compound")
{
Nz::Rectf aabb(0.f, 0.f, 1.f, 1.f);
Nz::BoxCollider2DRef box1 = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::BoxCollider2D> box1 = std::make_shared<Nz::BoxCollider2D>(aabb);
aabb.Translate(Nz::Vector2f::Unit());
Nz::BoxCollider2DRef box2 = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::BoxCollider2D> box2 = std::make_shared<Nz::BoxCollider2D>(aabb);
std::vector<Nz::Collider2DRef> colliders;
std::vector<std::shared_ptr<Nz::Collider2D>> colliders;
colliders.push_back(box1);
colliders.push_back(box2);
Nz::CompoundCollider2D compound(colliders);
THEN("We expect those to be true")
{
CHECK(compound.GetType() == Nz::ColliderType2D_Compound);
CHECK(compound.GetType() == Nz::ColliderType2D::Compound);
}
}
@ -75,7 +75,7 @@ SCENARIO("Collider2D", "[PHYSICS2D][COLLIDER2D]")
THEN("We expect those to be true")
{
CHECK(convex.GetType() == Nz::ColliderType2D_Convex);
CHECK(convex.GetType() == Nz::ColliderType2D::Convex);
}
}
@ -85,7 +85,7 @@ SCENARIO("Collider2D", "[PHYSICS2D][COLLIDER2D]")
THEN("We expect those to be true")
{
CHECK(null.GetType() == Nz::ColliderType2D_Null);
CHECK(null.GetType() == Nz::ColliderType2D::Null);
}
}
@ -100,7 +100,7 @@ SCENARIO("Collider2D", "[PHYSICS2D][COLLIDER2D]")
CHECK(segment.GetFirstPoint() == firstPoint);
CHECK(segment.GetLength() == Approx(firstPoint.Distance(secondPoint)));
CHECK(segment.GetSecondPoint() == secondPoint);
CHECK(segment.GetType() == Nz::ColliderType2D_Segment);
CHECK(segment.GetType() == Nz::ColliderType2D::Segment);
}
}

View File

@ -109,19 +109,19 @@ SCENARIO("PhysWorld2D", "[PHYSICS2D][PHYSWORLD2D]")
Nz::PhysWorld2D world;
Nz::Rectf characterAABB(0.f, 0.f, 1.f, 1.f);
Nz::Collider2DRef characterBox = Nz::BoxCollider2D::New(characterAABB);
std::shared_ptr<Nz::Collider2D> characterBox = std::make_shared<Nz::BoxCollider2D>(characterAABB);
characterBox->SetCollisionId(CHARACTER_COLLISION_ID);
Nz::RigidBody2D character(&world, 1.f, characterBox);
character.SetPosition(Nz::Vector2f::Zero());
Nz::Rectf wallAABB(0.f, 0.f, 1.f, 2.f);
Nz::Collider2DRef wallBox = Nz::BoxCollider2D::New(wallAABB);
std::shared_ptr<Nz::Collider2D> wallBox = std::make_shared<Nz::BoxCollider2D>(wallAABB);
wallBox->SetCollisionId(WALL_COLLISION_ID);
Nz::RigidBody2D wall(&world, 0.f, wallBox);
wall.SetPosition(Nz::Vector2f(5.f, 0.f));
Nz::Rectf triggerAABB(0.f, 0.f, 1.f, 1.f);
Nz::Collider2DRef triggerBox = Nz::BoxCollider2D::New(triggerAABB);
std::shared_ptr<Nz::Collider2D> triggerBox = std::make_shared<Nz::BoxCollider2D>(triggerAABB);
triggerBox->SetTrigger(true);
triggerBox->SetCollisionId(TRIGGER_COLLISION_ID);
Nz::RigidBody2D trigger(&world, 0.f, triggerBox);
@ -192,7 +192,7 @@ SCENARIO("PhysWorld2D", "[PHYSICS2D][PHYSWORLD2D]")
Nz::RigidBody2D CreateBody(Nz::PhysWorld2D& world, const Nz::Vector2f& position, bool isMoving, const Nz::Vector2f& lengths)
{
Nz::Rectf aabb(0.f, 0.f, lengths.x, lengths.y);
Nz::Collider2DRef box = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::Collider2D> box = std::make_shared<Nz::BoxCollider2D>(aabb);
box->SetCategoryMask(categoryMask);
box->SetCollisionMask(collisionMask);
float mass = isMoving ? 1.f : 0.f;

View File

@ -17,7 +17,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
Nz::Vector2f positionAABB(3.f, 4.f);
Nz::Rectf aabb(positionAABB.x, positionAABB.y, 1.f, 2.f);
Nz::Collider2DRef box = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::Collider2D> box = std::make_shared<Nz::BoxCollider2D>(aabb);
float mass = 1.f;
Nz::RigidBody2D body(&world, mass, box);
float angularVelocity = 0.2f;
@ -69,7 +69,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
WHEN("We set a new geometry")
{
float radius = 5.f;
body.SetGeom(Nz::CircleCollider2D::New(radius));
body.SetGeom(std::make_shared<Nz::CircleCollider2D>(radius));
world.Step(1.f);
@ -112,7 +112,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
Nz::Vector2f positionAABB(3.f, 4.f);
Nz::Rectf aabb(positionAABB.x, positionAABB.y, 1.f, 2.f);
Nz::Collider2DRef box = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::Collider2D> box = std::make_shared<Nz::BoxCollider2D>(aabb);
float mass = 1.f;
Nz::RigidBody2D body(&world, mass);
body.SetGeom(box, true, false);
@ -214,7 +214,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
Nz::Vector2f position(3.f, 4.f);
float radius = 5.f;
Nz::Collider2DRef circle = Nz::CircleCollider2D::New(radius, position);
std::shared_ptr<Nz::Collider2D> circle = std::make_shared<Nz::CircleCollider2D>(radius, position);
float mass = 1.f;
Nz::RigidBody2D body(&world, mass);
body.SetGeom(circle, true, false);
@ -237,14 +237,14 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
world.SetMaxStepCount(std::numeric_limits<std::size_t>::max());
Nz::Rectf aabb(0.f, 0.f, 1.f, 1.f);
Nz::BoxCollider2DRef box1 = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::BoxCollider2D> box1 = std::make_shared<Nz::BoxCollider2D>(aabb);
aabb.Translate(Nz::Vector2f::Unit());
Nz::BoxCollider2DRef box2 = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::BoxCollider2D> box2 = std::make_shared<Nz::BoxCollider2D>(aabb);
std::vector<Nz::Collider2DRef> colliders;
std::vector<std::shared_ptr<Nz::Collider2D>> colliders;
colliders.push_back(box1);
colliders.push_back(box2);
Nz::CompoundCollider2DRef compound = Nz::CompoundCollider2D::New(colliders);
std::shared_ptr<Nz::CompoundCollider2D> compound = std::make_shared<Nz::CompoundCollider2D>(colliders);
float mass = 1.f;
Nz::RigidBody2D body(&world, mass);
@ -268,13 +268,13 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
world.SetMaxStepCount(std::numeric_limits<std::size_t>::max());
std::vector<Nz::Vector2f> vertices;
vertices.push_back(Nz::Vector2f(0.f, 0.f));
vertices.push_back(Nz::Vector2f(0.f, 1.f));
vertices.push_back(Nz::Vector2f(1.f, 1.f));
vertices.push_back(Nz::Vector2f(1.f, 0.f));
vertices.emplace_back(0.f, 0.f);
vertices.emplace_back(0.f, 1.f);
vertices.emplace_back(1.f, 1.f);
vertices.emplace_back(1.f, 0.f);
Nz::SparsePtr<const Nz::Vector2f> sparsePtr(vertices.data());
Nz::ConvexCollider2DRef convex = Nz::ConvexCollider2D::New(sparsePtr, vertices.size());
std::shared_ptr<Nz::ConvexCollider2D> convex = std::make_shared<Nz::ConvexCollider2D>(sparsePtr, vertices.size());
float mass = 1.f;
Nz::RigidBody2D body(&world, mass);
body.SetGeom(convex, true, false);
@ -298,7 +298,7 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]")
Nz::Vector2f positionA(3.f, 4.f);
Nz::Vector2f positionB(1.f, -4.f);
Nz::Collider2DRef segment = Nz::SegmentCollider2D::New(positionA, positionB, 0.f);
std::shared_ptr<Nz::Collider2D> segment = std::make_shared<Nz::SegmentCollider2D>(positionA, positionB, 0.f);
float mass = 1.f;
Nz::RigidBody2D body(&world, mass);
body.SetGeom(segment, true, false);
@ -320,7 +320,7 @@ Nz::RigidBody2D CreateBody(Nz::PhysWorld2D& world)
{
Nz::Vector2f positionAABB(3.f, 4.f);
Nz::Rectf aabb(positionAABB.x, positionAABB.y, 1.f, 2.f);
Nz::Collider2DRef box = Nz::BoxCollider2D::New(aabb);
std::shared_ptr<Nz::Collider2D> box = std::make_shared<Nz::BoxCollider2D>(aabb);
float mass = 1.f;
Nz::RigidBody2D body(&world, mass, box);