From 4f6bf30f240e5936ea43ee6bc7d37320ded313c4 Mon Sep 17 00:00:00 2001 From: Faymoon Date: Sat, 13 Jan 2018 11:03:03 +0100 Subject: [PATCH] Constraint 2D (#147) * Rename all Constraint by [name]Constraint3D * Create Refs and Libraries for Constraint2D * Remove ref to PhysWorld in Constraints ctors * Update Constraint2d ctor to have RigiBodies first * Add New static function for all constraints and fix an oopsie * Add Contraint Library and remove all library useless aliases * Add ConstraintComponent2D * remove useless definition of Constraint2D library * Fix : getting the world of the constraint before having created it * Make the GetStaticBody function return a ref * Remove : Useless AddJoint Fonction and Update CreateJoint function * Update PhysicsSystem3D because GetStaticBody return now a ref * Oops * Having done something with the 3D and i was needing 2D * Add ConstraintComponent as friend of Collision and PhysicsComponent * Update all the Get[Static/Rigid]Body function so they return a pointer * fix the bugs caused by the commit before * update : CreateConstraint lambda for the nullptr case * remove the useless use of const pointer * Update : CreateConstraint function * Update ChangeLog.md --- ChangeLog.md | 2 + SDK/include/NDK/Components.hpp | 1 + .../NDK/Components/CollisionComponent2D.hpp | 1 + .../NDK/Components/ConstraintComponent2D.hpp | 34 ++++ .../NDK/Components/ConstraintComponent2D.inl | 29 ++++ .../NDK/Components/PhysicsComponent2D.hpp | 3 +- .../NDK/Components/PhysicsComponent2D.inl | 4 +- .../NDK/Components/PhysicsComponent3D.hpp | 2 +- .../NDK/Components/PhysicsComponent3D.inl | 4 +- .../NDK/Components/CollisionComponent2D.cpp | 2 +- .../NDK/Components/CollisionComponent3D.cpp | 2 +- .../NDK/Components/ConstraintComponent2D.cpp | 6 + SDK/src/NDK/Sdk.cpp | 2 + SDK/src/NDK/Systems/PhysicsSystem2D.cpp | 6 +- SDK/src/NDK/Systems/PhysicsSystem3D.cpp | 6 +- include/Nazara/Physics2D/Constraint2D.hpp | 132 ++++++++++++---- include/Nazara/Physics2D/Constraint2D.inl | 80 ++++++++++ src/Nazara/Physics2D/Constraint2D.cpp | 145 +++++++++--------- 18 files changed, 344 insertions(+), 117 deletions(-) create mode 100644 SDK/include/NDK/Components/ConstraintComponent2D.hpp create mode 100644 SDK/include/NDK/Components/ConstraintComponent2D.inl create mode 100644 SDK/src/NDK/Components/ConstraintComponent2D.cpp diff --git a/ChangeLog.md b/ChangeLog.md index f5991da6f..59d366d33 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -51,6 +51,7 @@ Nazara Engine: - Calling PlacementDestroy on a null pointer is now a no-op (was triggering an undefined behavior) - Fix OBJParser relative offsets handling - Add JPEG image saver +- Update Constraint2Ds classes (Add : Ref, Library, ConstRef, New function and Update : ctors) Nazara Development Kit: - Added ImageWidget (#139) @@ -81,6 +82,7 @@ Nazara Development Kit: - It is now possible to disable synchronization between a PhysicsComponent3D and the NodeComponent - Fix PhysicsComponent3D copy which was not copying physics state (such as mass, mass center, damping values, gravity factor and auto-sleep mode) - Fix TextAreaWidget::Clear crash +- Add ConstraintComponent2D class # 0.4: diff --git a/SDK/include/NDK/Components.hpp b/SDK/include/NDK/Components.hpp index 3a2916009..e099b641b 100644 --- a/SDK/include/NDK/Components.hpp +++ b/SDK/include/NDK/Components.hpp @@ -17,5 +17,6 @@ #include #include #include +#include #endif // NDK_COMPONENTS_GLOBAL_HPP diff --git a/SDK/include/NDK/Components/CollisionComponent2D.hpp b/SDK/include/NDK/Components/CollisionComponent2D.hpp index 524097e32..35247ead6 100644 --- a/SDK/include/NDK/Components/CollisionComponent2D.hpp +++ b/SDK/include/NDK/Components/CollisionComponent2D.hpp @@ -17,6 +17,7 @@ namespace Ndk class NDK_API CollisionComponent2D : public Component { friend class PhysicsSystem2D; + friend class ConstraintComponent2D; public: CollisionComponent2D(Nz::Collider2DRef geom = Nz::Collider2DRef()); diff --git a/SDK/include/NDK/Components/ConstraintComponent2D.hpp b/SDK/include/NDK/Components/ConstraintComponent2D.hpp new file mode 100644 index 000000000..bf7cd3e31 --- /dev/null +++ b/SDK/include/NDK/Components/ConstraintComponent2D.hpp @@ -0,0 +1,34 @@ +#pragma once + +#ifndef NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP +#define NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP + +#include +#include +#include +#include +#include + +namespace Ndk +{ + class NDK_API ConstraintComponent2D : public Component + { + public: + ConstraintComponent2D() = default; + ConstraintComponent2D(const ConstraintComponent2D& joint) = default; + ConstraintComponent2D(ConstraintComponent2D&& joint) = default; + + template inline Nz::ObjectRef CreateConstraint(const Ndk::EntityHandle first, const Ndk::EntityHandle second, Args&&... args); + + static ComponentIndex componentIndex; + + private: + + std::vector m_constraints; + }; + +} + +#include + +#endif// NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP diff --git a/SDK/include/NDK/Components/ConstraintComponent2D.inl b/SDK/include/NDK/Components/ConstraintComponent2D.inl new file mode 100644 index 000000000..8c806e31b --- /dev/null +++ b/SDK/include/NDK/Components/ConstraintComponent2D.inl @@ -0,0 +1,29 @@ +#include +#include +#include + +namespace Ndk +{ + template + inline Nz::ObjectRef ConstraintComponent2D::CreateConstraint(const Ndk::EntityHandle first, const Ndk::EntityHandle second, Args && ...args) + { + auto QueryBody = [](const Ndk::EntityHandle& entity) -> Nz::RigidBody2D* + { + if (entity->HasComponent()) + return entity->GetComponent().GetRigidBody(); + else if (entity->HasComponent()) + return entity->GetComponent().GetStaticBody(); + return nullptr; + }; + + Nz::RigidBody2D* body_first{ QueryBody(first) }, body_second{ QueryBody(second) }; + + NazaraAssert(body_first && body_second, "RigidBodies of CollisionComponent2D or PhysicsComponent2D must be valid"); + + Nz::ObjectRef constraint = T::New(*body_first, *body_second, std::forward(args)...); + + m_constraints.push_back(constraint); + + return constraint; + } +} diff --git a/SDK/include/NDK/Components/PhysicsComponent2D.hpp b/SDK/include/NDK/Components/PhysicsComponent2D.hpp index 1d57e08b8..216e733b2 100644 --- a/SDK/include/NDK/Components/PhysicsComponent2D.hpp +++ b/SDK/include/NDK/Components/PhysicsComponent2D.hpp @@ -17,6 +17,7 @@ namespace Ndk { friend class CollisionComponent2D; friend class PhysicsSystem2D; + friend class ConstraintComponent2D; public: PhysicsComponent2D() = default; @@ -49,7 +50,7 @@ namespace Ndk static ComponentIndex componentIndex; private: - Nz::RigidBody2D& GetRigidBody(); + Nz::RigidBody2D* GetRigidBody(); void OnAttached() override; void OnComponentAttached(BaseComponent& component) override; diff --git a/SDK/include/NDK/Components/PhysicsComponent2D.inl b/SDK/include/NDK/Components/PhysicsComponent2D.inl index 39752b645..23ac0d5d3 100644 --- a/SDK/include/NDK/Components/PhysicsComponent2D.inl +++ b/SDK/include/NDK/Components/PhysicsComponent2D.inl @@ -307,8 +307,8 @@ namespace Ndk * \return A reference to the physics object */ - inline Nz::RigidBody2D& PhysicsComponent2D::GetRigidBody() + inline Nz::RigidBody2D* PhysicsComponent2D::GetRigidBody() { - return *m_object.get(); + return m_object.get(); } } diff --git a/SDK/include/NDK/Components/PhysicsComponent3D.hpp b/SDK/include/NDK/Components/PhysicsComponent3D.hpp index e7985ca3d..397f13cc2 100644 --- a/SDK/include/NDK/Components/PhysicsComponent3D.hpp +++ b/SDK/include/NDK/Components/PhysicsComponent3D.hpp @@ -62,7 +62,7 @@ namespace Ndk private: void ApplyPhysicsState(Nz::RigidBody3D& rigidBody) const; void CopyPhysicsState(const Nz::RigidBody3D& rigidBody); - Nz::RigidBody3D& GetRigidBody(); + Nz::RigidBody3D* GetRigidBody(); const Nz::RigidBody3D& GetRigidBody() const; void OnAttached() override; diff --git a/SDK/include/NDK/Components/PhysicsComponent3D.inl b/SDK/include/NDK/Components/PhysicsComponent3D.inl index 4fceefdff..65195a7f1 100644 --- a/SDK/include/NDK/Components/PhysicsComponent3D.inl +++ b/SDK/include/NDK/Components/PhysicsComponent3D.inl @@ -461,9 +461,9 @@ namespace Ndk * \brief Gets the underlying physics object * \return A reference to the physics object */ - inline Nz::RigidBody3D& PhysicsComponent3D::GetRigidBody() + inline Nz::RigidBody3D* PhysicsComponent3D::GetRigidBody() { - return *m_object.get(); + return m_object.get(); } /*! diff --git a/SDK/src/NDK/Components/CollisionComponent2D.cpp b/SDK/src/NDK/Components/CollisionComponent2D.cpp index 974a89cd8..96e2116da 100644 --- a/SDK/src/NDK/Components/CollisionComponent2D.cpp +++ b/SDK/src/NDK/Components/CollisionComponent2D.cpp @@ -33,7 +33,7 @@ namespace Ndk { // We update the geometry of the PhysiscsObject linked to the PhysicsComponent2D PhysicsComponent2D& physComponent = m_entity->GetComponent(); - physComponent.GetRigidBody().SetGeom(m_geom); + physComponent.GetRigidBody()->SetGeom(m_geom); } else { diff --git a/SDK/src/NDK/Components/CollisionComponent3D.cpp b/SDK/src/NDK/Components/CollisionComponent3D.cpp index 7fab3d9be..69ab957c3 100644 --- a/SDK/src/NDK/Components/CollisionComponent3D.cpp +++ b/SDK/src/NDK/Components/CollisionComponent3D.cpp @@ -32,7 +32,7 @@ namespace Ndk { // We update the geometry of the PhysiscsObject linked to the PhysicsComponent3D PhysicsComponent3D& physComponent = m_entity->GetComponent(); - physComponent.GetRigidBody().SetGeom(m_geom); + physComponent.GetRigidBody()->SetGeom(m_geom); } else { diff --git a/SDK/src/NDK/Components/ConstraintComponent2D.cpp b/SDK/src/NDK/Components/ConstraintComponent2D.cpp new file mode 100644 index 000000000..ef841c210 --- /dev/null +++ b/SDK/src/NDK/Components/ConstraintComponent2D.cpp @@ -0,0 +1,6 @@ +#include + +namespace Ndk +{ + ComponentIndex ConstraintComponent2D::componentIndex; +} diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp index ee3764224..f041cfd1f 100644 --- a/SDK/src/NDK/Sdk.cpp +++ b/SDK/src/NDK/Sdk.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -89,6 +90,7 @@ namespace Ndk InitializeComponent("NdkPhys2"); InitializeComponent("NdkPhys3"); InitializeComponent("NdkVeloc"); + InitializeComponent("NdkCons2"); #ifndef NDK_SERVER // Client components diff --git a/SDK/src/NDK/Systems/PhysicsSystem2D.cpp b/SDK/src/NDK/Systems/PhysicsSystem2D.cpp index b05815a89..7ab95e32a 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem2D.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem2D.cpp @@ -80,9 +80,9 @@ namespace Ndk NodeComponent& node = entity->GetComponent(); PhysicsComponent2D& phys = entity->GetComponent(); - Nz::RigidBody2D& body = phys.GetRigidBody(); - node.SetRotation(Nz::EulerAnglesf(0.f, 0.f, body.GetRotation()), Nz::CoordSys_Global); - node.SetPosition(Nz::Vector3f(body.GetPosition(), node.GetPosition(Nz::CoordSys_Global).z), Nz::CoordSys_Global); + Nz::RigidBody2D* body = phys.GetRigidBody(); + node.SetRotation(Nz::EulerAnglesf(0.f, 0.f, body->GetRotation()), Nz::CoordSys_Global); + node.SetPosition(Nz::Vector3f(body->GetPosition(), node.GetPosition(Nz::CoordSys_Global).z), Nz::CoordSys_Global); } float invElapsedTime = 1.f / elapsedTime; diff --git a/SDK/src/NDK/Systems/PhysicsSystem3D.cpp b/SDK/src/NDK/Systems/PhysicsSystem3D.cpp index 8aec2d8e2..5d5a1883d 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem3D.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem3D.cpp @@ -84,9 +84,9 @@ namespace Ndk NodeComponent& node = entity->GetComponent(); PhysicsComponent3D& phys = entity->GetComponent(); - Nz::RigidBody3D& physObj = phys.GetRigidBody(); - node.SetRotation(physObj.GetRotation(), Nz::CoordSys_Global); - node.SetPosition(physObj.GetPosition(), Nz::CoordSys_Global); + Nz::RigidBody3D* physObj = phys.GetRigidBody(); + node.SetRotation(physObj->GetRotation(), Nz::CoordSys_Global); + node.SetPosition(physObj->GetPosition(), Nz::CoordSys_Global); } float invElapsedTime = 1.f / elapsedTime; diff --git a/include/Nazara/Physics2D/Constraint2D.hpp b/include/Nazara/Physics2D/Constraint2D.hpp index 761742348..32d6ba719 100644 --- a/include/Nazara/Physics2D/Constraint2D.hpp +++ b/include/Nazara/Physics2D/Constraint2D.hpp @@ -18,7 +18,13 @@ struct cpConstraint; namespace Nz { - class NAZARA_PHYSICS2D_API Constraint2D + class Constraint2D; + + using Constraint2DConstRef = ObjectRef; + using Constraint2DLibrary = ObjectLibrary; + using Constraint2DRef = ObjectRef; + + class NAZARA_PHYSICS2D_API Constraint2D : public RefCounted { public: Constraint2D(const Constraint2D&) = delete; @@ -47,16 +53,24 @@ namespace Nz Constraint2D& operator=(Constraint2D&& rhs); protected: - Constraint2D(PhysWorld2D& world, cpConstraint* constraint); + Constraint2D(Nz::PhysWorld2D* world, cpConstraint* constraint); MovablePtr m_constraint; + + private: + static Constraint2DLibrary s_library; }; + + class DampedSpringConstraint2D; + + using DampedSpringConstraint2DConstRef = ObjectRef; + using DampedSpringConstraint2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API DampedSpring2D : public Constraint2D + class NAZARA_PHYSICS2D_API DampedSpringConstraint2D : public Constraint2D { public: - DampedSpring2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor, float restLength, float stiffness, float damping); - ~DampedSpring2D() = default; + DampedSpringConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor, float restLength, float stiffness, float damping); + ~DampedSpringConstraint2D() = default; float GetDamping() const; Vector2f GetFirstAnchor() const; @@ -69,13 +83,20 @@ namespace Nz void SetRestLength(float newLength); void SetSecondAnchor(const Vector2f& firstAnchor); void SetStiffness(float newStiffness); + + template static DampedSpringConstraint2DRef New(Args&&... args); }; - class NAZARA_PHYSICS2D_API DampedRotarySpring2D : public Constraint2D + class DampedRotarySpringConstraint2D; + + using DampedRotarySpringConstraint2DConstRef = ObjectRef; + using DampedRotarySpringConstraint2DRef = ObjectRef; + + class NAZARA_PHYSICS2D_API DampedRotarySpringConstraint2D : public Constraint2D { public: - DampedRotarySpring2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float restAngle, float stiffness, float damping); - ~DampedRotarySpring2D() = default; + DampedRotarySpringConstraint2D(RigidBody2D& first, RigidBody2D& second, float restAngle, float stiffness, float damping); + ~DampedRotarySpringConstraint2D() = default; float GetDamping() const; float GetRestAngle() const; @@ -84,36 +105,57 @@ namespace Nz void SetDamping(float newDamping); void SetRestAngle(float newAngle); void SetStiffness(float newStiffness); + + template static DampedRotarySpringConstraint2DRef New(Args&&... args); }; + + class GearConstraint2D; + + using GearConstraint2DConstRef = ObjectRef; + using GearConstraint2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API GearJoint2D : public Constraint2D + class NAZARA_PHYSICS2D_API GearConstraint2D : public Constraint2D { public: - GearJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float phase, float ratio); - ~GearJoint2D() = default; + GearConstraint2D(RigidBody2D& first, RigidBody2D& second, float phase, float ratio); + ~GearConstraint2D() = default; float GetPhase() const; float GetRatio() const; void SetPhase(float phase); void SetRatio(float ratio); + + template static GearConstraint2DRef New(Args&&... args); }; + + class MotorConstraint2D; + + using MotorConstraint2DConstRef = ObjectRef; + using MotorConstraint2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API MotorJoint2D : public Constraint2D + class NAZARA_PHYSICS2D_API MotorConstraint2D : public Constraint2D { public: - MotorJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float rate); - ~MotorJoint2D() = default; + MotorConstraint2D(RigidBody2D& first, RigidBody2D& second, float rate); + ~MotorConstraint2D() = default; float GetRate() const; void SetRate(float rate); + + template static MotorConstraint2DRef New(Args&&... args); }; - class NAZARA_PHYSICS2D_API PinJoint2D : public Constraint2D + class PinConstraint2D; + + using PinConstraint2DConstRef = ObjectRef; + using PinConstraint2DRef = ObjectRef; + + class NAZARA_PHYSICS2D_API PinConstraint2D : public Constraint2D { public: - PinJoint2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor); - ~PinJoint2D() = default; + PinConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor); + ~PinConstraint2D() = default; float GetDistance() const; Vector2f GetFirstAnchor() const; @@ -122,27 +164,41 @@ namespace Nz void SetDistance(float newDistance); void SetFirstAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor); + + template static PinConstraint2DRef New(Args&&... args); }; + + class PivotConstraint2D; + + using PivotConstraint2DConstRef = ObjectRef; + using PivotConstraint2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API PivotJoint2D : public Constraint2D + class NAZARA_PHYSICS2D_API PivotConstraint2D : public Constraint2D { public: - PivotJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, const Vector2f& anchor); - PivotJoint2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor); - ~PivotJoint2D() = default; + PivotConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& anchor); + PivotConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor); + ~PivotConstraint2D() = default; Vector2f GetFirstAnchor() const; Vector2f GetSecondAnchor() const; void SetFirstAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor); + + template static PivotConstraint2DRef New(Args&&... args); }; + + class RatchetConstraint2D; + + using RatchetConstraint2DConstRef = ObjectRef; + using RatchetConstraint2DRef = ObjectRef; - class NAZARA_PHYSICS2D_API RatchetJoint2D : public Constraint2D + class NAZARA_PHYSICS2D_API RatchetConstraint2D : public Constraint2D { public: - RatchetJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float phase, float ratchet); - ~RatchetJoint2D() = default; + RatchetConstraint2D(RigidBody2D& first, RigidBody2D& second, float phase, float ratchet); + ~RatchetConstraint2D() = default; float GetAngle() const; float GetPhase() const; @@ -151,26 +207,40 @@ namespace Nz void SetAngle(float angle); void SetPhase(float phase); void SetRatchet(float ratchet); + + template static RatchetConstraint2DRef New(Args&&... args); }; - class NAZARA_PHYSICS2D_API RotaryLimitJoint2D : public Constraint2D + class RotaryLimitConstraint2D; + + using RotaryLimitConstraint2DConstRef = ObjectRef; + using RotaryLimitConstraint2DRef = ObjectRef; + + class NAZARA_PHYSICS2D_API RotaryLimitConstraint2D : public Constraint2D { public: - RotaryLimitJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float minAngle, float maxAngle); - ~RotaryLimitJoint2D() = default; + RotaryLimitConstraint2D(RigidBody2D& first, RigidBody2D& second, float minAngle, float maxAngle); + ~RotaryLimitConstraint2D() = default; float GetMaxAngle() const; float GetMinAngle() const; void SetMaxAngle(float maxAngle); void SetMinAngle(float minAngle); + + template static RotaryLimitConstraint2DRef New(Args&&... args); }; - class NAZARA_PHYSICS2D_API SlideJoint2D : public Constraint2D + class SlideConstraint2D; + + using SlideConstraint2DConstRef = ObjectRef; + using SlideConstraint2DRef = ObjectRef; + + class NAZARA_PHYSICS2D_API SlideConstraint2D : public Constraint2D { public: - SlideJoint2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor, float min, float max); - ~SlideJoint2D() = default; + SlideConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor, float min, float max); + ~SlideConstraint2D() = default; Vector2f GetFirstAnchor() const; float GetMaxDistance() const; @@ -181,6 +251,8 @@ namespace Nz void SetMaxDistance(float newMaxDistance); void SetMinDistance(float newMinDistance); void SetSecondAnchor(const Vector2f& firstAnchor); + + template static SlideConstraint2DRef New(Args&&... args); }; } diff --git a/include/Nazara/Physics2D/Constraint2D.inl b/include/Nazara/Physics2D/Constraint2D.inl index 2ed7744f3..9bdb1d3ad 100644 --- a/include/Nazara/Physics2D/Constraint2D.inl +++ b/include/Nazara/Physics2D/Constraint2D.inl @@ -8,6 +8,86 @@ namespace Nz { + template + DampedSpringConstraint2DRef DampedSpringConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new DampedSpringConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + DampedRotarySpringConstraint2DRef DampedRotarySpringConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new DampedRotarySpringConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + GearConstraint2DRef GearConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new GearConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + MotorConstraint2DRef MotorConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new MotorConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + PinConstraint2DRef PinConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new PinConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + PivotConstraint2DRef PivotConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new PivotConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + RatchetConstraint2DRef RatchetConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new RatchetConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + RotaryLimitConstraint2DRef RotaryLimitConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new RotaryLimitConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } + + template + SlideConstraint2DRef SlideConstraint2D::New(Args&&... args) + { + std::unique_ptr object(new SlideConstraint2D(std::forward(args)...)); + object->SetPersistent(false); + + return object.release(); + } } #include diff --git a/src/Nazara/Physics2D/Constraint2D.cpp b/src/Nazara/Physics2D/Constraint2D.cpp index 39aa6a9f9..a17119545 100644 --- a/src/Nazara/Physics2D/Constraint2D.cpp +++ b/src/Nazara/Physics2D/Constraint2D.cpp @@ -8,11 +8,11 @@ namespace Nz { - Constraint2D::Constraint2D(PhysWorld2D& world, cpConstraint* constraint) : + Constraint2D::Constraint2D(Nz::PhysWorld2D* world, cpConstraint* constraint) : m_constraint(constraint) { cpConstraintSetUserData(m_constraint, this); - cpSpaceAddConstraint(world.GetHandle(), m_constraint); + cpSpaceAddConstraint(world->GetHandle(), m_constraint); } Constraint2D::Constraint2D(Constraint2D&& rhs) : @@ -105,320 +105,319 @@ namespace Nz } - DampedSpring2D::DampedSpring2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor, float restLength, float stiffness, float damping) : - Constraint2D(world, cpDampedSpringNew(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y), restLength, stiffness, damping)) + DampedSpringConstraint2D::DampedSpringConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor, float restLength, float stiffness, float damping) : + Constraint2D(first.GetWorld(), cpDampedSpringNew(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y), restLength, stiffness, damping)) { } - float DampedSpring2D::GetDamping() const + float DampedSpringConstraint2D::GetDamping() const { return float(cpDampedSpringGetDamping(m_constraint)); } - Vector2f DampedSpring2D::GetFirstAnchor() const + Vector2f DampedSpringConstraint2D::GetFirstAnchor() const { cpVect anchor = cpDampedSpringGetAnchorA(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - float DampedSpring2D::GetRestLength() const + float DampedSpringConstraint2D::GetRestLength() const { return float(cpDampedSpringGetRestLength(m_constraint)); } - Vector2f DampedSpring2D::GetSecondAnchor() const + Vector2f DampedSpringConstraint2D::GetSecondAnchor() const { cpVect anchor = cpDampedSpringGetAnchorB(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - float DampedSpring2D::GetStiffness() const + float DampedSpringConstraint2D::GetStiffness() const { return float(cpDampedSpringGetStiffness(m_constraint)); } - void DampedSpring2D::SetDamping(float newDamping) + void DampedSpringConstraint2D::SetDamping(float newDamping) { cpDampedSpringSetDamping(m_constraint, newDamping); } - void DampedSpring2D::SetFirstAnchor(const Vector2f& firstAnchor) + void DampedSpringConstraint2D::SetFirstAnchor(const Vector2f& firstAnchor) { cpDampedSpringSetAnchorA(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - void DampedSpring2D::SetRestLength(float newLength) + void DampedSpringConstraint2D::SetRestLength(float newLength) { cpDampedSpringSetRestLength(m_constraint, newLength); } - void DampedSpring2D::SetSecondAnchor(const Vector2f& firstAnchor) + void DampedSpringConstraint2D::SetSecondAnchor(const Vector2f& firstAnchor) { cpDampedSpringSetAnchorB(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - void DampedSpring2D::SetStiffness(float newStiffness) + void DampedSpringConstraint2D::SetStiffness(float newStiffness) { cpDampedSpringSetStiffness(m_constraint, newStiffness); } - DampedRotarySpring2D::DampedRotarySpring2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float restAngle, float stiffness, float damping) : - Constraint2D(world, cpDampedRotarySpringNew(first.GetHandle(), second.GetHandle(), restAngle, stiffness, damping)) + DampedRotarySpringConstraint2D::DampedRotarySpringConstraint2D(RigidBody2D& first, RigidBody2D& second, float restAngle, float stiffness, float damping) : + Constraint2D(first.GetWorld(), cpDampedRotarySpringNew(first.GetHandle(), second.GetHandle(), restAngle, stiffness, damping)) { } - float DampedRotarySpring2D::GetDamping() const + float DampedRotarySpringConstraint2D::GetDamping() const { return float(cpDampedRotarySpringGetDamping(m_constraint)); } - float DampedRotarySpring2D::GetRestAngle() const + float DampedRotarySpringConstraint2D::GetRestAngle() const { return float(cpDampedRotarySpringGetRestAngle(m_constraint)); } - float DampedRotarySpring2D::GetStiffness() const + float DampedRotarySpringConstraint2D::GetStiffness() const { return float(cpDampedRotarySpringGetStiffness(m_constraint)); } - void DampedRotarySpring2D::SetDamping(float newDamping) + void DampedRotarySpringConstraint2D::SetDamping(float newDamping) { cpDampedSpringSetDamping(m_constraint, newDamping); } - void DampedRotarySpring2D::SetRestAngle(float newAngle) + void DampedRotarySpringConstraint2D::SetRestAngle(float newAngle) { cpDampedRotarySpringSetRestAngle(m_constraint, newAngle); } - void DampedRotarySpring2D::SetStiffness(float newStiffness) + void DampedRotarySpringConstraint2D::SetStiffness(float newStiffness) { cpDampedRotarySpringSetStiffness(m_constraint, newStiffness); } - GearJoint2D::GearJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float phase, float ratio) : - Constraint2D(world, cpGearJointNew(first.GetHandle(), second.GetHandle(), phase, ratio)) + GearConstraint2D::GearConstraint2D(RigidBody2D& first, RigidBody2D& second, float phase, float ratio) : + Constraint2D(first.GetWorld(), cpGearJointNew(first.GetHandle(), second.GetHandle(), phase, ratio)) { } - float GearJoint2D::GetPhase() const + float GearConstraint2D::GetPhase() const { return float(cpGearJointGetPhase(m_constraint)); } - float GearJoint2D::GetRatio() const + float GearConstraint2D::GetRatio() const { return float(cpGearJointGetRatio(m_constraint)); } - void GearJoint2D::SetPhase(float phase) + void GearConstraint2D::SetPhase(float phase) { cpGearJointSetPhase(m_constraint, phase); } - void GearJoint2D::SetRatio(float ratio) + void GearConstraint2D::SetRatio(float ratio) { cpGearJointSetRatio(m_constraint, ratio); } - MotorJoint2D::MotorJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float rate) : - Constraint2D(world, cpSimpleMotorNew(first.GetHandle(), second.GetHandle(), rate)) + MotorConstraint2D::MotorConstraint2D(RigidBody2D& first, RigidBody2D& second, float rate) : + Constraint2D(first.GetWorld(), cpSimpleMotorNew(first.GetHandle(), second.GetHandle(), rate)) { } - float MotorJoint2D::GetRate() const + float MotorConstraint2D::GetRate() const { return float(cpSimpleMotorGetRate(m_constraint)); } - void MotorJoint2D::SetRate(float rate) + void MotorConstraint2D::SetRate(float rate) { cpSimpleMotorSetRate(m_constraint, rate); } - PinJoint2D::PinJoint2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor) : - Constraint2D(world, cpPinJointNew(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y))) + PinConstraint2D::PinConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor) : + Constraint2D(first.GetWorld(), cpPinJointNew(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y))) { } - float PinJoint2D::GetDistance() const + float PinConstraint2D::GetDistance() const { return float(cpPinJointGetDist(m_constraint)); } - Vector2f PinJoint2D::GetFirstAnchor() const + Vector2f PinConstraint2D::GetFirstAnchor() const { cpVect anchor = cpPinJointGetAnchorA(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - Vector2f PinJoint2D::GetSecondAnchor() const + Vector2f PinConstraint2D::GetSecondAnchor() const { cpVect anchor = cpPinJointGetAnchorB(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - void PinJoint2D::SetDistance(float newDistance) + void PinConstraint2D::SetDistance(float newDistance) { cpPinJointSetDist(m_constraint, newDistance); } - void PinJoint2D::SetFirstAnchor(const Vector2f& firstAnchor) + void PinConstraint2D::SetFirstAnchor(const Vector2f& firstAnchor) { cpPinJointSetAnchorA(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - void PinJoint2D::SetSecondAnchor(const Vector2f& firstAnchor) + void PinConstraint2D::SetSecondAnchor(const Vector2f& firstAnchor) { cpPinJointSetAnchorB(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - PivotJoint2D::PivotJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, const Vector2f& anchor) : - Constraint2D(world, cpPivotJointNew(first.GetHandle(), second.GetHandle(), cpv(anchor.x, anchor.y))) + PivotConstraint2D::PivotConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& anchor) : + Constraint2D(first.GetWorld(), cpPivotJointNew(first.GetHandle(), second.GetHandle(), cpv(anchor.x, anchor.y))) { } - PivotJoint2D::PivotJoint2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor) : - Constraint2D(world, cpPivotJointNew2(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y))) + PivotConstraint2D::PivotConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor) : + Constraint2D(first.GetWorld(), cpPivotJointNew2(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y))) { } - Vector2f PivotJoint2D::GetFirstAnchor() const + Vector2f PivotConstraint2D::GetFirstAnchor() const { cpVect anchor = cpPivotJointGetAnchorA(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - Vector2f PivotJoint2D::GetSecondAnchor() const + Vector2f PivotConstraint2D::GetSecondAnchor() const { cpVect anchor = cpPivotJointGetAnchorB(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - void PivotJoint2D::SetFirstAnchor(const Vector2f& firstAnchor) + void PivotConstraint2D::SetFirstAnchor(const Vector2f& firstAnchor) { cpPivotJointSetAnchorA(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - void PivotJoint2D::SetSecondAnchor(const Vector2f& firstAnchor) + void PivotConstraint2D::SetSecondAnchor(const Vector2f& firstAnchor) { cpPivotJointSetAnchorB(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - RatchetJoint2D::RatchetJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float phase, float ratchet) : - Constraint2D(world, cpRatchetJointNew(first.GetHandle(), second.GetHandle(), phase, ratchet)) + RatchetConstraint2D::RatchetConstraint2D(RigidBody2D& first, RigidBody2D& second, float phase, float ratchet) : + Constraint2D(first.GetWorld(), cpRatchetJointNew(first.GetHandle(), second.GetHandle(), phase, ratchet)) { } - float RatchetJoint2D::GetAngle() const + float RatchetConstraint2D::GetAngle() const { return float(cpRatchetJointGetAngle(m_constraint)); } - float RatchetJoint2D::GetPhase() const + float RatchetConstraint2D::GetPhase() const { return float(cpRatchetJointGetPhase(m_constraint)); } - float RatchetJoint2D::GetRatchet() const + float RatchetConstraint2D::GetRatchet() const { return float(cpRatchetJointGetRatchet(m_constraint)); } - void RatchetJoint2D::SetAngle(float angle) + void RatchetConstraint2D::SetAngle(float angle) { cpRatchetJointSetAngle(m_constraint, angle); } - void RatchetJoint2D::SetPhase(float phase) + void RatchetConstraint2D::SetPhase(float phase) { cpRatchetJointSetPhase(m_constraint, phase); } - void RatchetJoint2D::SetRatchet(float ratchet) + void RatchetConstraint2D::SetRatchet(float ratchet) { cpRatchetJointSetRatchet(m_constraint, ratchet); } - RotaryLimitJoint2D::RotaryLimitJoint2D(PhysWorld2D& world, RigidBody2D& first, RigidBody2D& second, float minAngle, float maxAngle) : - Constraint2D(world, cpRotaryLimitJointNew(first.GetHandle(), second.GetHandle(), minAngle, maxAngle)) + RotaryLimitConstraint2D::RotaryLimitConstraint2D(RigidBody2D& first, RigidBody2D& second, float minAngle, float maxAngle) : + Constraint2D(first.GetWorld(), cpRotaryLimitJointNew(first.GetHandle(), second.GetHandle(), minAngle, maxAngle)) { } - float RotaryLimitJoint2D::GetMaxAngle() const + float RotaryLimitConstraint2D::GetMaxAngle() const { return float(cpRotaryLimitJointGetMax(m_constraint)); } - float RotaryLimitJoint2D::GetMinAngle() const + float RotaryLimitConstraint2D::GetMinAngle() const { return float(cpRotaryLimitJointGetMax(m_constraint)); } - void RotaryLimitJoint2D::SetMaxAngle(float maxAngle) + void RotaryLimitConstraint2D::SetMaxAngle(float maxAngle) { cpRotaryLimitJointSetMax(m_constraint, maxAngle); } - void RotaryLimitJoint2D::SetMinAngle(float minAngle) + void RotaryLimitConstraint2D::SetMinAngle(float minAngle) { cpRotaryLimitJointSetMin(m_constraint, minAngle); } - SlideJoint2D::SlideJoint2D(PhysWorld2D& world, RigidBody2D& first, const Vector2f& firstAnchor, RigidBody2D& second, const Vector2f& secondAnchor, float min, float max) : - Constraint2D(world, cpSlideJointNew(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y), min, max)) + SlideConstraint2D::SlideConstraint2D(RigidBody2D& first, RigidBody2D& second, const Vector2f& firstAnchor, const Vector2f& secondAnchor, float min, float max) : + Constraint2D(first.GetWorld(), cpSlideJointNew(first.GetHandle(), second.GetHandle(), cpv(firstAnchor.x, firstAnchor.y), cpv(secondAnchor.x, secondAnchor.y), min, max)) { } - Vector2f SlideJoint2D::GetFirstAnchor() const + Vector2f SlideConstraint2D::GetFirstAnchor() const { cpVect anchor = cpSlideJointGetAnchorA(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - float SlideJoint2D::GetMaxDistance() const + float SlideConstraint2D::GetMaxDistance() const { return float(cpSlideJointGetMax(m_constraint)); } - float SlideJoint2D::GetMinDistance() const + float SlideConstraint2D::GetMinDistance() const { return float(cpSlideJointGetMin(m_constraint)); } - Vector2f SlideJoint2D::GetSecondAnchor() const + Vector2f SlideConstraint2D::GetSecondAnchor() const { cpVect anchor = cpSlideJointGetAnchorB(m_constraint); return Vector2f(static_cast(anchor.x), static_cast(anchor.y)); } - void SlideJoint2D::SetFirstAnchor(const Vector2f& firstAnchor) + void SlideConstraint2D::SetFirstAnchor(const Vector2f& firstAnchor) { cpSlideJointSetAnchorA(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - void SlideJoint2D::SetMaxDistance(float newMaxDistance) + void SlideConstraint2D::SetMaxDistance(float newMaxDistance) { cpSlideJointSetMax(m_constraint, newMaxDistance); } - void SlideJoint2D::SetMinDistance(float newMinDistance) + void SlideConstraint2D::SetMinDistance(float newMinDistance) { cpSlideJointSetMin(m_constraint, newMinDistance); } - void SlideJoint2D::SetSecondAnchor(const Vector2f& firstAnchor) + void SlideConstraint2D::SetSecondAnchor(const Vector2f& firstAnchor) { cpSlideJointSetAnchorB(m_constraint, cpv(firstAnchor.x, firstAnchor.y)); } - }