diff --git a/include/Nazara/Physics2D/Collider2D.hpp b/include/Nazara/Physics2D/Collider2D.hpp index b713e510d..4710f3ea1 100644 --- a/include/Nazara/Physics2D/Collider2D.hpp +++ b/include/Nazara/Physics2D/Collider2D.hpp @@ -8,7 +8,6 @@ #define NAZARA_COLLIDER2D_HPP #include -#include #include #include #include @@ -18,20 +17,15 @@ #include #include +struct cpBody; struct cpShape; namespace Nz { - class Collider2D; class RigidBody2D; - using Collider2DConstRef = ObjectRef; - using Collider2DLibrary = ObjectLibrary; - using Collider2DRef = ObjectRef; - - 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* shapes) const = 0; + virtual std::size_t CreateShapes(cpBody* body, std::vector* 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* shapes) const; - - static Collider2DLibrary::LibraryMap s_library; + virtual std::size_t GenerateShapes(cpBody* body, std::vector* shapes) const; }; - class BoxCollider2D; - - using BoxCollider2DConstRef = ObjectRef; - using BoxCollider2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API BoxCollider2D : public Collider2D { public: @@ -110,20 +97,13 @@ namespace Nz inline Vector2f GetSize() const; ColliderType2D GetType() const override; - template static BoxCollider2DRef New(Args&&... args); - private: - std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; + std::size_t CreateShapes(cpBody* body, std::vector* shapes) const override; Rectf m_rect; float m_radius; }; - class CircleCollider2D; - - using CircleCollider2DConstRef = ObjectRef; - using CircleCollider2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API CircleCollider2D : public Collider2D { public: @@ -136,50 +116,36 @@ namespace Nz inline float GetRadius() const; ColliderType2D GetType() const override; - template static CircleCollider2DRef New(Args&&... args); - private: - std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; + std::size_t CreateShapes(cpBody* body, std::vector* shapes) const override; Vector2f m_offset; float m_radius; }; - class CompoundCollider2D; - - using CompoundCollider2DConstRef = ObjectRef; - using CompoundCollider2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API CompoundCollider2D : public Collider2D { public: - CompoundCollider2D(std::vector geoms); + CompoundCollider2D(std::vector> geoms); Nz::Vector2f ComputeCenterOfMass() const override; float ComputeMomentOfInertia(float mass) const override; inline bool DoesOverrideCollisionProperties() const; - inline const std::vector& GetGeoms() const; + inline const std::vector>& GetGeoms() const; ColliderType2D GetType() const override; inline void OverridesCollisionProperties(bool shouldOverride); - template static CompoundCollider2DRef New(Args&&... args); - private: - std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; - std::size_t GenerateShapes(RigidBody2D* body, std::vector* shapes) const override; + std::size_t CreateShapes(cpBody* body, std::vector* shapes) const override; + std::size_t GenerateShapes(cpBody* body, std::vector* shapes) const override; - std::vector m_geoms; + std::vector> m_geoms; bool m_doesOverrideCollisionProperties; }; - class ConvexCollider2D; - - using ConvexCollider2DConstRef = ObjectRef; - using ConvexCollider2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API ConvexCollider2D : public Collider2D { public: @@ -191,20 +157,13 @@ namespace Nz ColliderType2D GetType() const override; inline const std::vector& GetVertices() const; - template static ConvexCollider2DRef New(Args&&... args); - private: - std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; + std::size_t CreateShapes(cpBody* body, std::vector* shapes) const override; std::vector m_vertices; float m_radius; }; - class NullCollider2D; - - using NullCollider2DConstRef = ObjectRef; - using NullCollider2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API NullCollider2D : public Collider2D { public: @@ -215,17 +174,10 @@ namespace Nz ColliderType2D GetType() const override; - template static NullCollider2DRef New(Args&&... args); - private: - std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; + std::size_t CreateShapes(cpBody* body, std::vector* shapes) const override; }; - class SegmentCollider2D; - - using SegmentCollider2DConstRef = ObjectRef; - using SegmentCollider2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API SegmentCollider2D : public Collider2D { public: @@ -243,10 +195,8 @@ namespace Nz inline float GetThickness() const; ColliderType2D GetType() const override; - template static SegmentCollider2DRef New(Args&&... args); - private: - std::size_t CreateShapes(RigidBody2D* body, std::vector* shapes) const override; + std::size_t CreateShapes(cpBody* body, std::vector* shapes) const override; Vector2f m_first; Vector2f m_firstNeighbor; diff --git a/include/Nazara/Physics2D/Collider2D.inl b/include/Nazara/Physics2D/Collider2D.inl index ad85b0304..478d034aa 100644 --- a/include/Nazara/Physics2D/Collider2D.inl +++ b/include/Nazara/Physics2D/Collider2D.inl @@ -115,14 +115,6 @@ namespace Nz return m_rect.GetLengths(); } - template - BoxCollider2DRef BoxCollider2D::New(Args&&... args) - { - std::unique_ptr object(new BoxCollider2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } inline const Vector2f& CircleCollider2D::GetOffset() const { @@ -134,21 +126,13 @@ namespace Nz return m_radius; } - template - CircleCollider2DRef CircleCollider2D::New(Args&&... args) - { - std::unique_ptr object(new CircleCollider2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } inline bool Nz::CompoundCollider2D::DoesOverrideCollisionProperties() const { return m_doesOverrideCollisionProperties; } - inline const std::vector& CompoundCollider2D::GetGeoms() const + inline const std::vector>& CompoundCollider2D::GetGeoms() const { return m_geoms; } @@ -158,37 +142,12 @@ namespace Nz m_doesOverrideCollisionProperties = shouldOverride; } - template - CompoundCollider2DRef CompoundCollider2D::New(Args&&... args) - { - std::unique_ptr object(new CompoundCollider2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } inline const std::vector& ConvexCollider2D::GetVertices() const { return m_vertices; } - template - ConvexCollider2DRef ConvexCollider2D::New(Args&&... args) - { - std::unique_ptr object(new ConvexCollider2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - NullCollider2DRef NullCollider2D::New(Args&&... args) - { - std::unique_ptr object(new NullCollider2D(std::forward(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 - SegmentCollider2DRef SegmentCollider2D::New(Args&&... args) - { - std::unique_ptr object(new SegmentCollider2D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } } #include diff --git a/include/Nazara/Physics2D/Enums.hpp b/include/Nazara/Physics2D/Enums.hpp index e92d63e02..adf615165 100644 --- a/include/Nazara/Physics2D/Enums.hpp +++ b/include/Nazara/Physics2D/Enums.hpp @@ -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 }; } diff --git a/include/Nazara/Physics2D/RigidBody2D.hpp b/include/Nazara/Physics2D/RigidBody2D.hpp index 1fd826da1..ce97d3c79 100644 --- a/include/Nazara/Physics2D/RigidBody2D.hpp +++ b/include/Nazara/Physics2D/RigidBody2D.hpp @@ -30,7 +30,7 @@ namespace Nz using VelocityFunc = std::function; RigidBody2D(PhysWorld2D* world, float mass); - RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom); + RigidBody2D(PhysWorld2D* world, float mass, std::shared_ptr 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& 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 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 m_shapes; - Collider2DRef m_geom; + std::shared_ptr m_geom; cpBody* m_handle; void* m_userData; PhysWorld2D* m_world; diff --git a/include/Nazara/Physics3D/Collider3D.hpp b/include/Nazara/Physics3D/Collider3D.hpp index 7c96fca8a..519f9871b 100644 --- a/include/Nazara/Physics3D/Collider3D.hpp +++ b/include/Nazara/Physics3D/Collider3D.hpp @@ -9,8 +9,6 @@ #include #include -#include -#include #include #include #include @@ -30,17 +28,11 @@ namespace Nz ///TODO: SceneGeom ///TODO: TreeGeom - class Collider3D; class PrimitiveList; class PhysWorld3D; - using Collider3DConstRef = ObjectRef; - using Collider3DLibrary = ObjectLibrary; - using Collider3DRef = ObjectRef; - - 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 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 m_handles; - - static Collider3DLibrary::LibraryMap s_library; }; - class BoxCollider3D; - - using BoxCollider3DConstRef = ObjectRef; - using BoxCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API BoxCollider3D : public Collider3D { public: @@ -95,8 +77,6 @@ namespace Nz Vector3f GetLengths() const; ColliderType3D GetType() const override; - template 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; - using CapsuleCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API CapsuleCollider3D : public Collider3D { public: @@ -119,8 +94,6 @@ namespace Nz float GetRadius() const; ColliderType3D GetType() const override; - template 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; - using CompoundCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API CompoundCollider3D : public Collider3D { public: - CompoundCollider3D(std::vector geoms); + CompoundCollider3D(std::vector> geoms); - const std::vector& GetGeoms() const; + const std::vector>& GetGeoms() const; ColliderType3D GetType() const override; - template static CompoundCollider3DRef New(Args&&... args); - private: NewtonCollision* CreateHandle(PhysWorld3D* world) const override; - std::vector m_geoms; + std::vector> m_geoms; }; - class ConeCollider3D; - - using ConeCollider3DConstRef = ObjectRef; - using ConeCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API ConeCollider3D : public Collider3D { public: @@ -165,8 +126,6 @@ namespace Nz float GetRadius() const; ColliderType3D GetType() const override; - template 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; - using ConvexCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API ConvexCollider3D : public Collider3D { public: @@ -188,8 +142,6 @@ namespace Nz ColliderType3D GetType() const override; - template 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; - using CylinderCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API CylinderCollider3D : public Collider3D { public: @@ -213,8 +160,6 @@ namespace Nz float GetRadius() const; ColliderType3D GetType() const override; - template 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; - using NullCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API NullCollider3D : public Collider3D { public: @@ -237,17 +177,10 @@ namespace Nz ColliderType3D GetType() const override; - template static NullCollider3DRef New(Args&&... args); - private: NewtonCollision* CreateHandle(PhysWorld3D* world) const override; }; - class SphereCollider3D; - - using SphereCollider3DConstRef = ObjectRef; - using SphereCollider3DRef = ObjectRef; - class NAZARA_PHYSICS3D_API SphereCollider3D : public Collider3D { public: @@ -260,8 +193,6 @@ namespace Nz float GetRadius() const; ColliderType3D GetType() const override; - template static SphereCollider3DRef New(Args&&... args); - private: NewtonCollision* CreateHandle(PhysWorld3D* world) const override; diff --git a/include/Nazara/Physics3D/Collider3D.inl b/include/Nazara/Physics3D/Collider3D.inl index 4caa926f1..01c3c4266 100644 --- a/include/Nazara/Physics3D/Collider3D.inl +++ b/include/Nazara/Physics3D/Collider3D.inl @@ -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 #include #include namespace Nz { - template - BoxCollider3DRef BoxCollider3D::New(Args&&... args) - { - std::unique_ptr object(new BoxCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - CapsuleCollider3DRef CapsuleCollider3D::New(Args&&... args) - { - std::unique_ptr object(new CapsuleCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - CompoundCollider3DRef CompoundCollider3D::New(Args&&... args) - { - std::unique_ptr object(new CompoundCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - ConeCollider3DRef ConeCollider3D::New(Args&&... args) - { - std::unique_ptr object(new ConeCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - ConvexCollider3DRef ConvexCollider3D::New(Args&&... args) - { - std::unique_ptr object(new ConvexCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - CylinderCollider3DRef CylinderCollider3D::New(Args&&... args) - { - std::unique_ptr object(new CylinderCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - NullCollider3DRef NullCollider3D::New(Args&&... args) - { - std::unique_ptr object(new NullCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } - - template - SphereCollider3DRef SphereCollider3D::New(Args&&... args) - { - std::unique_ptr object(new SphereCollider3D(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } } #include diff --git a/include/Nazara/Physics3D/Enums.hpp b/include/Nazara/Physics3D/Enums.hpp index 68b93454b..ec78ee304 100644 --- a/include/Nazara/Physics3D/Enums.hpp +++ b/include/Nazara/Physics3D/Enums.hpp @@ -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 }; } diff --git a/include/Nazara/Physics3D/Physics3D.hpp b/include/Nazara/Physics3D/Physics3D.hpp index 0a1dac109..242bd2c75 100644 --- a/include/Nazara/Physics3D/Physics3D.hpp +++ b/include/Nazara/Physics3D/Physics3D.hpp @@ -23,7 +23,7 @@ namespace Nz struct Config {}; Physics3D(Config /*config*/); - ~Physics3D(); + ~Physics3D() = default; unsigned int GetMemoryUsed(); diff --git a/include/Nazara/Physics3D/RigidBody3D.hpp b/include/Nazara/Physics3D/RigidBody3D.hpp index 6b9bce67c..c7c0272c9 100644 --- a/include/Nazara/Physics3D/RigidBody3D.hpp +++ b/include/Nazara/Physics3D/RigidBody3D.hpp @@ -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 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& 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 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 m_geom; Matrix4f m_matrix; Vector3f m_forceAccumulator; Vector3f m_torqueAccumulator; diff --git a/include/NazaraSDK/Components/CollisionComponent2D.hpp b/include/NazaraSDK/Components/CollisionComponent2D.hpp index d10dec8af..69753a3e9 100644 --- a/include/NazaraSDK/Components/CollisionComponent2D.hpp +++ b/include/NazaraSDK/Components/CollisionComponent2D.hpp @@ -25,20 +25,20 @@ namespace Ndk friend class PhysicsSystem2D; public: - CollisionComponent2D(Nz::Collider2DRef geom = Nz::Collider2DRef()); + CollisionComponent2D(std::shared_ptr geom = std::shared_ptr()); CollisionComponent2D(const CollisionComponent2D& collision); ~CollisionComponent2D() = default; Nz::Rectf GetAABB() const; - const Nz::Collider2DRef& GetGeom() const; + const std::shared_ptr& 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 geom, bool recomputeMoment = true, bool recomputeMassCenter = true); void SetGeomOffset(const Nz::Vector2f& geomOffset); - CollisionComponent2D& operator=(Nz::Collider2DRef geom); + CollisionComponent2D& operator=(std::shared_ptr geom); CollisionComponent2D& operator=(CollisionComponent2D&& collision) = delete; static ComponentIndex componentIndex; @@ -56,7 +56,7 @@ namespace Ndk void OnDetached() override; std::unique_ptr m_staticBody; - Nz::Collider2DRef m_geom; + std::shared_ptr m_geom; bool m_bodyUpdated; }; } diff --git a/include/NazaraSDK/Components/CollisionComponent2D.inl b/include/NazaraSDK/Components/CollisionComponent2D.inl index 35df6467c..6be748111 100644 --- a/include/NazaraSDK/Components/CollisionComponent2D.inl +++ b/include/NazaraSDK/Components/CollisionComponent2D.inl @@ -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 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& 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 geom) { SetGeom(geom); diff --git a/include/NazaraSDK/Components/CollisionComponent3D.hpp b/include/NazaraSDK/Components/CollisionComponent3D.hpp index 7994232a8..083c4ef1c 100644 --- a/include/NazaraSDK/Components/CollisionComponent3D.hpp +++ b/include/NazaraSDK/Components/CollisionComponent3D.hpp @@ -23,15 +23,15 @@ namespace Ndk friend class PhysicsSystem3D; public: - CollisionComponent3D(Nz::Collider3DRef geom = Nz::Collider3DRef()); + CollisionComponent3D(std::shared_ptr geom = std::shared_ptr()); CollisionComponent3D(const CollisionComponent3D& collision); ~CollisionComponent3D() = default; - const Nz::Collider3DRef& GetGeom() const; + const std::shared_ptr& GetGeom() const; - void SetGeom(Nz::Collider3DRef geom); + void SetGeom(std::shared_ptr geom); - CollisionComponent3D& operator=(Nz::Collider3DRef geom); + CollisionComponent3D& operator=(std::shared_ptr geom); CollisionComponent3D& operator=(CollisionComponent3D&& collision) = delete; static ComponentIndex componentIndex; @@ -48,7 +48,7 @@ namespace Ndk void OnEntityEnabled() override; std::unique_ptr m_staticBody; - Nz::Collider3DRef m_geom; + std::shared_ptr m_geom; bool m_bodyUpdated; }; } diff --git a/include/NazaraSDK/Components/CollisionComponent3D.inl b/include/NazaraSDK/Components/CollisionComponent3D.inl index fc31fe0b5..a2a8ede7f 100644 --- a/include/NazaraSDK/Components/CollisionComponent3D.inl +++ b/include/NazaraSDK/Components/CollisionComponent3D.inl @@ -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 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& 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 geom) { SetGeom(geom); diff --git a/src/Nazara/Physics2D/Collider2D.cpp b/src/Nazara/Physics2D/Collider2D.cpp index 9d0743b88..4546df0e3 100644 --- a/src/Nazara/Physics2D/Collider2D.cpp +++ b/src/Nazara/Physics2D/Collider2D.cpp @@ -3,6 +3,8 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include +#include #include #include #include @@ -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& 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 shapeVector; - rigidBody.SetGeom(const_cast(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 shapes; + CreateShapes(body, &shapes); + CallOnExit shapeRRID([&] { + for (cpShape* shape : shapes) + cpShapeDestroy(shape); + }); + + for (cpShape* shape : shapes) + cpSpaceAddShape(space, shape); + + using CallbackType = std::decay_t; + + cpSpaceDebugDrawOptions drawOptions; + drawOptions.collisionPointColor = white; + drawOptions.constraintColor = white; + drawOptions.shapeOutlineColor = white; + drawOptions.data = const_cast(static_cast(&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(userdata); + constexpr std::size_t circleVerticesCount = 20; std::array vertices; + Vector2f origin = FromChipmunk(pos); + float r = static_cast(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 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 vertices = { first, second }; + const auto& callback = *static_cast(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 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(radius); + std::array 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(userdata); + + StackArray 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(userdata); + + std::array vertices = { FromChipmunk(a), FromChipmunk(b) }; + callback(vertices.data(), vertices.size()); + }; + + cpSpaceDebugDraw(space, &drawOptions); } - std::size_t Collider2D::GenerateShapes(RigidBody2D* body, std::vector* shapes) const + std::size_t Collider2D::GenerateShapes(cpBody* body, std::vector* 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* shapes) const + std::size_t BoxCollider2D::CreateShapes(cpBody* body, std::vector* 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* shapes) const + std::size_t CircleCollider2D::CreateShapes(cpBody* body, std::vector* 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 geoms) : + CompoundCollider2D::CompoundCollider2D(std::vector> 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* shapes) const + std::size_t CompoundCollider2D::CreateShapes(cpBody* body, std::vector* 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* shapes) const + std::size_t CompoundCollider2D::GenerateShapes(cpBody* body, std::vector* 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* shapes) const + std::size_t ConvexCollider2D::CreateShapes(cpBody* body, std::vector* shapes) const { - shapes->push_back(cpPolyShapeNew(body->GetHandle(), int(m_vertices.size()), reinterpret_cast(m_vertices.data()), cpTransformIdentity, m_radius)); + shapes->push_back(cpPolyShapeNew(body, int(m_vertices.size()), reinterpret_cast(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* /*shapes*/) const + std::size_t NullCollider2D::CreateShapes(cpBody* /*body*/, std::vector* /*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* shapes) const + std::size_t SegmentCollider2D::CreateShapes(cpBody* body, std::vector* 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); diff --git a/src/Nazara/Physics2D/RigidBody2D.cpp b/src/Nazara/Physics2D/RigidBody2D.cpp index 409ec357f..1fe42df42 100644 --- a/src/Nazara/Physics2D/RigidBody2D.cpp +++ b/src/Nazara/Physics2D/RigidBody2D.cpp @@ -18,7 +18,7 @@ namespace Nz { } - RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom) : + RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, std::shared_ptr 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& 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 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(); - m_geom->GenerateShapes(this, &m_shapes); + m_geom->GenerateShapes(m_handle, &m_shapes); for (cpShape* shape : m_shapes) cpShapeSetUserData(shape, this); diff --git a/src/Nazara/Physics3D/Collider3D.cpp b/src/Nazara/Physics3D/Collider3D.cpp index 3944ee8cc..24f435fc6 100644 --- a/src/Nazara/Physics3D/Collider3D.cpp +++ b/src/Nazara/Physics3D/Collider3D.cpp @@ -12,26 +12,26 @@ namespace Nz { namespace { - Collider3DRef CreateGeomFromPrimitive(const Primitive& primitive) + std::shared_ptr CreateGeomFromPrimitive(const Primitive& primitive) { switch (primitive.type) { case PrimitiveType_Box: - return BoxCollider3D::New(primitive.box.lengths, primitive.matrix); + return std::make_shared(primitive.box.lengths, primitive.matrix); case PrimitiveType_Cone: - return ConeCollider3D::New(primitive.cone.length, primitive.cone.radius, primitive.matrix); + return std::make_shared(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(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(primitive.sphere.size, primitive.matrix.GetTranslation()); } NazaraError("Primitive type not handled (0x" + NumberToString(primitive.type, 16) + ')'); - return Collider3DRef(); + return std::shared_ptr(); } } @@ -148,42 +148,24 @@ namespace Nz return it->second; } - Collider3DRef Collider3D::Build(const PrimitiveList& list) + std::shared_ptr Collider3D::Build(const PrimitiveList& list) { std::size_t primitiveCount = list.GetSize(); if (primitiveCount > 1) { - std::vector geoms(primitiveCount); + std::vector> 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(std::move(geoms)); } else if (primitiveCount > 0) return CreateGeomFromPrimitive(list.GetPrimitive(0)); else - return NullCollider3D::New(); + return std::make_shared(); } - 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 geoms) : + CompoundCollider3D::CompoundCollider3D(std::vector> geoms) : m_geoms(std::move(geoms)) { } - const std::vector& CompoundCollider3D::GetGeoms() const + const std::vector>& 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& geom : m_geoms) { - if (geom->GetType() == ColliderType3D_Compound) + if (geom->GetType() == ColliderType3D::Compound) { - CompoundCollider3D* compoundGeom = static_cast(geom.Get()); - for (const Collider3DRef& piece : compoundGeom->GetGeoms()) + CompoundCollider3D& compoundGeom = static_cast(*geom); + for (const std::shared_ptr& 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 diff --git a/src/Nazara/Physics3D/Physics3D.cpp b/src/Nazara/Physics3D/Physics3D.cpp index 730f24eab..5795dbeff 100644 --- a/src/Nazara/Physics3D/Physics3D.cpp +++ b/src/Nazara/Physics3D/Physics3D.cpp @@ -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() diff --git a/src/Nazara/Physics3D/RigidBody3D.cpp b/src/Nazara/Physics3D/RigidBody3D.cpp index 8afc3f58d..5aafcd1cf 100644 --- a/src/Nazara/Physics3D/RigidBody3D.cpp +++ b/src/Nazara/Physics3D/RigidBody3D.cpp @@ -12,11 +12,11 @@ namespace Nz { RigidBody3D::RigidBody3D(PhysWorld3D* world, const Matrix4f& mat) : - RigidBody3D(world, NullCollider3D::New(), mat) + RigidBody3D(world, std::make_shared(), mat) { } - RigidBody3D::RigidBody3D(PhysWorld3D* world, Collider3DRef geom, const Matrix4f& mat) : + RigidBody3D::RigidBody3D(PhysWorld3D* world, std::shared_ptr 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(); 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& 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 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(); NewtonBodySetCollision(m_body, m_geom->GetHandle(m_world)); } diff --git a/src/NazaraSDK/Components/CollisionComponent2D.cpp b/src/NazaraSDK/Components/CollisionComponent2D.cpp index ced3d8053..a2bbb95e7 100644 --- a/src/NazaraSDK/Components/CollisionComponent2D.cpp +++ b/src/NazaraSDK/Components/CollisionComponent2D.cpp @@ -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 geom, bool recomputeMoment, bool recomputeMassCenter) { m_geom = std::move(geom); diff --git a/src/NazaraSDK/Components/CollisionComponent3D.cpp b/src/NazaraSDK/Components/CollisionComponent3D.cpp index 4983a8af1..e7ec5ebbf 100644 --- a/src/NazaraSDK/Components/CollisionComponent3D.cpp +++ b/src/NazaraSDK/Components/CollisionComponent3D.cpp @@ -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 geom) { m_geom = std::move(geom); diff --git a/src/NazaraSDK/Components/PhysicsComponent2D.cpp b/src/NazaraSDK/Components/PhysicsComponent2D.cpp index 91d610eb4..ce33e8ce0 100644 --- a/src/NazaraSDK/Components/PhysicsComponent2D.cpp +++ b/src/NazaraSDK/Components/PhysicsComponent2D.cpp @@ -33,7 +33,7 @@ namespace Ndk Nz::Vector2f positionOffset; - Nz::Collider2DRef geom; + std::shared_ptr geom; if (m_entity->HasComponent()) { const CollisionComponent2D& entityCollision = m_entity->GetComponent(); @@ -94,7 +94,7 @@ namespace Ndk if (IsComponent(component)) { NazaraAssert(m_object, "Invalid object"); - m_object->SetGeom(Nz::NullCollider2D::New(), false, false); + m_object->SetGeom(std::make_shared(), false, false); } } diff --git a/src/NazaraSDK/Components/PhysicsComponent3D.cpp b/src/NazaraSDK/Components/PhysicsComponent3D.cpp index c830960a2..718b7fc7d 100644 --- a/src/NazaraSDK/Components/PhysicsComponent3D.cpp +++ b/src/NazaraSDK/Components/PhysicsComponent3D.cpp @@ -30,7 +30,7 @@ namespace Ndk Nz::PhysWorld3D& world = entityWorld->GetSystem().GetWorld(); - Nz::Collider3DRef geom; + std::shared_ptr geom; if (m_entity->HasComponent()) geom = m_entity->GetComponent().GetGeom(); @@ -81,7 +81,7 @@ namespace Ndk { NazaraAssert(m_object, "Invalid object"); - m_object->SetGeom(Nz::NullCollider3D::New()); + m_object->SetGeom(std::make_shared()); } } diff --git a/tests/Engine/Physics2D/Collider2D.cpp b/tests/Engine/Physics2D/Collider2D.cpp index 811b75ded..c4b65c761 100644 --- a/tests/Engine/Physics2D/Collider2D.cpp +++ b/tests/Engine/Physics2D/Collider2D.cpp @@ -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 box1 = std::make_shared(aabb); aabb.Translate(Nz::Vector2f::Unit()); - Nz::BoxCollider2DRef box2 = Nz::BoxCollider2D::New(aabb); + std::shared_ptr box2 = std::make_shared(aabb); - std::vector colliders; + std::vector> 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); } } diff --git a/tests/Engine/Physics2D/PhysWorld2D.cpp b/tests/Engine/Physics2D/PhysWorld2D.cpp index e71c9f2cb..dc3579776 100644 --- a/tests/Engine/Physics2D/PhysWorld2D.cpp +++ b/tests/Engine/Physics2D/PhysWorld2D.cpp @@ -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 characterBox = std::make_shared(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 wallBox = std::make_shared(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 triggerBox = std::make_shared(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 box = std::make_shared(aabb); box->SetCategoryMask(categoryMask); box->SetCollisionMask(collisionMask); float mass = isMoving ? 1.f : 0.f; diff --git a/tests/Engine/Physics2D/RigidBody2D.cpp b/tests/Engine/Physics2D/RigidBody2D.cpp index e554746d8..cb128de64 100644 --- a/tests/Engine/Physics2D/RigidBody2D.cpp +++ b/tests/Engine/Physics2D/RigidBody2D.cpp @@ -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 box = std::make_shared(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(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 box = std::make_shared(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 circle = std::make_shared(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::max()); Nz::Rectf aabb(0.f, 0.f, 1.f, 1.f); - Nz::BoxCollider2DRef box1 = Nz::BoxCollider2D::New(aabb); + std::shared_ptr box1 = std::make_shared(aabb); aabb.Translate(Nz::Vector2f::Unit()); - Nz::BoxCollider2DRef box2 = Nz::BoxCollider2D::New(aabb); + std::shared_ptr box2 = std::make_shared(aabb); - std::vector colliders; + std::vector> colliders; colliders.push_back(box1); colliders.push_back(box2); - Nz::CompoundCollider2DRef compound = Nz::CompoundCollider2D::New(colliders); + std::shared_ptr compound = std::make_shared(colliders); float mass = 1.f; Nz::RigidBody2D body(&world, mass); @@ -268,13 +268,13 @@ SCENARIO("RigidBody2D", "[PHYSICS2D][RIGIDBODY2D]") world.SetMaxStepCount(std::numeric_limits::max()); std::vector 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 sparsePtr(vertices.data()); - Nz::ConvexCollider2DRef convex = Nz::ConvexCollider2D::New(sparsePtr, vertices.size()); + std::shared_ptr convex = std::make_shared(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 segment = std::make_shared(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 box = std::make_shared(aabb); float mass = 1.f; Nz::RigidBody2D body(&world, mass, box);