Rework constraint2D

This commit is contained in:
Lynix 2019-12-08 18:56:21 +01:00
parent 164633e518
commit ef030ddaac
7 changed files with 74 additions and 134 deletions

View File

@ -200,6 +200,7 @@ Nazara Engine:
- Add Flags<E>::Clear(Flags) helper method, to clear one or more flags. - Add Flags<E>::Clear(Flags) helper method, to clear one or more flags.
- Add Flags<E>::Clear() helper method, to reset flags - Add Flags<E>::Clear() helper method, to reset flags
- Add Flags<E>::Set(Flags) helper method, to enable flags - Add Flags<E>::Set(Flags) helper method, to enable flags
- ⚠ Constraint2D are no longer managed by references and are now handled objects
Nazara Development Kit: Nazara Development Kit:
- Added ImageWidget (#139) - Added ImageWidget (#139)
@ -297,6 +298,7 @@ Nazara Development Kit:
- Added World::CloneEntity overload taking an EntityHandle const reference, allowing to copy entities from other worlds - Added World::CloneEntity overload taking an EntityHandle const reference, allowing to copy entities from other worlds
- Fixed PhysicsComponent2D copy not copying physics attributes - Fixed PhysicsComponent2D copy not copying physics attributes
- Added Entity::DropComponent which detaches a component without necessarily destroying it - Added Entity::DropComponent which detaches a component without necessarily destroying it
- ⚠ ConstraintComponent2D has been reworked to handle entity destruction and remove constraints at will
# 0.4: # 0.4:

View File

@ -1,13 +1,18 @@
// Copyright (C) 2019 Jérôme Leclercq
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#pragma once #pragma once
#ifndef NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP #ifndef NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP
#define NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP #define NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP
#include <NDK/Component.hpp>
#include <Nazara/Physics2D/Constraint2D.hpp> #include <Nazara/Physics2D/Constraint2D.hpp>
#include <Nazara/Math/Vector3.hpp> #include <Nazara/Math/Vector3.hpp>
#include <vector> #include <NDK/Component.hpp>
#include <NDK/Entity.hpp>
#include <memory> #include <memory>
#include <vector>
namespace Ndk namespace Ndk
{ {
@ -19,16 +24,24 @@ namespace Ndk
{ {
public: public:
ConstraintComponent2D() = default; ConstraintComponent2D() = default;
ConstraintComponent2D(const ConstraintComponent2D& joint) = default; ConstraintComponent2D(const ConstraintComponent2D& joint);
ConstraintComponent2D(ConstraintComponent2D&& joint) = default; ConstraintComponent2D(ConstraintComponent2D&& joint) = default;
template<typename T, typename... Args> inline Nz::ObjectRef<T> CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&&... args); template<typename T, typename... Args> T* CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&&... args);
bool RemoveConstraint(Nz::Constraint2D* constraint);
static ComponentIndex componentIndex; static ComponentIndex componentIndex;
private: private:
struct ConstraintData
{
std::unique_ptr<Nz::Constraint2D> constraint;
std::vector<Nz::Constraint2DRef> m_constraints; NazaraSlot(Ndk::Entity, OnEntityDestruction, onBodyADestruction);
NazaraSlot(Ndk::Entity, OnEntityDestruction, onBodyBDestruction);
};
std::vector<ConstraintData> m_constraints;
}; };
} }

View File

@ -1,3 +1,7 @@
// Copyright (C) 2019 Jérôme Leclercq
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <NDK/Components/ConstraintComponent2D.hpp> #include <NDK/Components/ConstraintComponent2D.hpp>
#include <NDK/Components/PhysicsComponent2D.hpp> #include <NDK/Components/PhysicsComponent2D.hpp>
#include <NDK/Components/CollisionComponent2D.hpp> #include <NDK/Components/CollisionComponent2D.hpp>
@ -5,7 +9,7 @@
namespace Ndk namespace Ndk
{ {
template<typename T, typename ...Args> template<typename T, typename ...Args>
Nz::ObjectRef<T> ConstraintComponent2D::CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args && ...args) T* ConstraintComponent2D::CreateConstraint(const Ndk::EntityHandle& first, const Ndk::EntityHandle& second, Args&& ...args)
{ {
auto FetchBody = [](const Ndk::EntityHandle& entity) -> Nz::RigidBody2D* auto FetchBody = [](const Ndk::EntityHandle& entity) -> Nz::RigidBody2D*
{ {
@ -23,9 +27,20 @@ namespace Ndk
Nz::RigidBody2D* secondBody = FetchBody(second); Nz::RigidBody2D* secondBody = FetchBody(second);
NazaraAssert(secondBody, "Second entity has no CollisionComponent2D nor PhysicsComponent2D component"); NazaraAssert(secondBody, "Second entity has no CollisionComponent2D nor PhysicsComponent2D component");
Nz::ObjectRef<T> constraint = T::New(*firstBody, *secondBody, std::forward<Args>(args)...); m_constraints.emplace_back();
m_constraints.push_back(constraint); auto& constraintData = m_constraints.back();
constraintData.constraint = std::make_unique<T>(*firstBody, *secondBody, std::forward<Args>(args)...);
return constraint; constraintData.onBodyADestruction.Connect(first->OnEntityDestruction, [this, constraint = constraintData.constraint.get()](const Ndk::Entity* /*entity*/)
{
RemoveConstraint(constraint);
});
constraintData.onBodyBDestruction.Connect(second->OnEntityDestruction, [this, constraint = constraintData.constraint.get()](const Ndk::Entity* /*entity*/)
{
RemoveConstraint(constraint);
});
return static_cast<T*>(constraintData.constraint.get());
} }
} }

View File

@ -1,6 +1,23 @@
// Copyright (C) 2019 Jérôme Leclercq
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
#include <NDK/Components/ConstraintComponent2D.hpp> #include <NDK/Components/ConstraintComponent2D.hpp>
namespace Ndk namespace Ndk
{ {
ConstraintComponent2D::ConstraintComponent2D(const ConstraintComponent2D& /*joint*/)
{
}
bool ConstraintComponent2D::RemoveConstraint(Nz::Constraint2D* constraintPtr)
{
auto it = std::find_if(m_constraints.begin(), m_constraints.end(), [constraintPtr](const ConstraintData& constraintData) { return constraintData.constraint.get() == constraintPtr; });
if (it != m_constraints.end())
m_constraints.erase(it);
return !m_constraints.empty();
}
ComponentIndex ConstraintComponent2D::componentIndex; ComponentIndex ConstraintComponent2D::componentIndex;
} }

View File

@ -335,7 +335,7 @@ namespace Nz
template<typename T> template<typename T>
bool operator<(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs) bool operator<(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs)
{ {
return lhs.m_object < rhs.m_object; return lhs.Get() < rhs.Get();
} }
/*! /*!
@ -348,7 +348,7 @@ namespace Nz
template<typename T> template<typename T>
bool operator<(const T& lhs, const ObjectRef<T>& rhs) bool operator<(const T& lhs, const ObjectRef<T>& rhs)
{ {
return &lhs < rhs.m_object; return &lhs < rhs.Get();
} }
/*! /*!
@ -361,7 +361,7 @@ namespace Nz
template<typename T> template<typename T>
bool operator<(const ObjectRef<T>& lhs, const T& rhs) bool operator<(const ObjectRef<T>& lhs, const T& rhs)
{ {
return lhs.m_object < &rhs; return lhs.Get() < &rhs;
} }
/*! /*!

View File

@ -8,7 +8,9 @@
#define NAZARA_CONSTRAINT2D_HPP #define NAZARA_CONSTRAINT2D_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/HandledObject.hpp>
#include <Nazara/Core/MovablePtr.hpp> #include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/Core/ObjectHandle.hpp>
#include <Nazara/Math/Angle.hpp> #include <Nazara/Math/Angle.hpp>
#include <Nazara/Physics2D/Config.hpp> #include <Nazara/Physics2D/Config.hpp>
#include <Nazara/Physics2D/PhysWorld2D.hpp> #include <Nazara/Physics2D/PhysWorld2D.hpp>
@ -21,11 +23,9 @@ namespace Nz
{ {
class Constraint2D; class Constraint2D;
using Constraint2DConstRef = ObjectRef<const Constraint2D>; using Constraint2DHandle = ObjectHandle<Constraint2D>;
using Constraint2DLibrary = ObjectLibrary<Constraint2D>;
using Constraint2DRef = ObjectRef<Constraint2D>;
class NAZARA_PHYSICS2D_API Constraint2D : public RefCounted class NAZARA_PHYSICS2D_API Constraint2D : public HandledObject<Constraint2D>
{ {
public: public:
Constraint2D(const Constraint2D&) = delete; Constraint2D(const Constraint2D&) = delete;
@ -57,16 +57,12 @@ namespace Nz
Constraint2D(Nz::PhysWorld2D* world, cpConstraint* constraint); Constraint2D(Nz::PhysWorld2D* world, cpConstraint* constraint);
MovablePtr<cpConstraint> m_constraint; MovablePtr<cpConstraint> m_constraint;
private:
static Constraint2DLibrary s_library;
}; };
class DampedSpringConstraint2D; class DampedSpringConstraint2D;
using DampedSpringConstraint2DConstRef = ObjectRef<const DampedSpringConstraint2D>; using DampedSpringConstraint2DHandle = ObjectHandle<DampedSpringConstraint2D>;
using DampedSpringConstraint2DRef = ObjectRef<DampedSpringConstraint2D>;
class NAZARA_PHYSICS2D_API DampedSpringConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API DampedSpringConstraint2D : public Constraint2D
{ {
public: public:
@ -84,14 +80,11 @@ namespace Nz
void SetRestLength(float newLength); void SetRestLength(float newLength);
void SetSecondAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor);
void SetStiffness(float newStiffness); void SetStiffness(float newStiffness);
template<typename... Args> static DampedSpringConstraint2DRef New(Args&&... args);
}; };
class DampedRotarySpringConstraint2D; class DampedRotarySpringConstraint2D;
using DampedRotarySpringConstraint2DConstRef = ObjectRef<const DampedRotarySpringConstraint2D>; using DampedRotarySpringConstraint2DHandle = ObjectHandle<DampedRotarySpringConstraint2D>;
using DampedRotarySpringConstraint2DRef = ObjectRef<DampedRotarySpringConstraint2D>;
class NAZARA_PHYSICS2D_API DampedRotarySpringConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API DampedRotarySpringConstraint2D : public Constraint2D
{ {
@ -106,13 +99,11 @@ namespace Nz
void SetDamping(float newDamping); void SetDamping(float newDamping);
void SetRestAngle(const RadianAnglef& newAngle); void SetRestAngle(const RadianAnglef& newAngle);
void SetStiffness(float newStiffness); void SetStiffness(float newStiffness);
template<typename... Args> static DampedRotarySpringConstraint2DRef New(Args&&... args);
}; };
class GearConstraint2D; class GearConstraint2D;
using GearConstraint2DConstRef = ObjectRef<const GearConstraint2D>; using GearConstraint2DHandle = ObjectHandle<GearConstraint2D>;
using GearConstraint2DRef = ObjectRef<GearConstraint2D>; using GearConstraint2DRef = ObjectRef<GearConstraint2D>;
class NAZARA_PHYSICS2D_API GearConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API GearConstraint2D : public Constraint2D
@ -132,8 +123,7 @@ namespace Nz
class MotorConstraint2D; class MotorConstraint2D;
using MotorConstraint2DConstRef = ObjectRef<const MotorConstraint2D>; using MotorConstraint2DHandle = ObjectHandle<MotorConstraint2D>;
using MotorConstraint2DRef = ObjectRef<MotorConstraint2D>;
class NAZARA_PHYSICS2D_API MotorConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API MotorConstraint2D : public Constraint2D
{ {
@ -143,14 +133,11 @@ namespace Nz
float GetRate() const; float GetRate() const;
void SetRate(float rate); void SetRate(float rate);
template<typename... Args> static MotorConstraint2DRef New(Args&&... args);
}; };
class PinConstraint2D; class PinConstraint2D;
using PinConstraint2DConstRef = ObjectRef<const PinConstraint2D>; using PinConstraint2DHandle = ObjectHandle<PinConstraint2D>;
using PinConstraint2DRef = ObjectRef<PinConstraint2D>;
class NAZARA_PHYSICS2D_API PinConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API PinConstraint2D : public Constraint2D
{ {
@ -165,14 +152,11 @@ namespace Nz
void SetDistance(float newDistance); void SetDistance(float newDistance);
void SetFirstAnchor(const Vector2f& firstAnchor); void SetFirstAnchor(const Vector2f& firstAnchor);
void SetSecondAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor);
template<typename... Args> static PinConstraint2DRef New(Args&&... args);
}; };
class PivotConstraint2D; class PivotConstraint2D;
using PivotConstraint2DConstRef = ObjectRef<const PivotConstraint2D>; using PivotConstraint2DHandle = ObjectHandle<PivotConstraint2D>;
using PivotConstraint2DRef = ObjectRef<PivotConstraint2D>;
class NAZARA_PHYSICS2D_API PivotConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API PivotConstraint2D : public Constraint2D
{ {
@ -186,14 +170,11 @@ namespace Nz
void SetFirstAnchor(const Vector2f& firstAnchor); void SetFirstAnchor(const Vector2f& firstAnchor);
void SetSecondAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor);
template<typename... Args> static PivotConstraint2DRef New(Args&&... args);
}; };
class RatchetConstraint2D; class RatchetConstraint2D;
using RatchetConstraint2DConstRef = ObjectRef<const RatchetConstraint2D>; using RatchetConstraint2DHandle = ObjectHandle<RatchetConstraint2D>;
using RatchetConstraint2DRef = ObjectRef<RatchetConstraint2D>;
class NAZARA_PHYSICS2D_API RatchetConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API RatchetConstraint2D : public Constraint2D
{ {
@ -208,14 +189,11 @@ namespace Nz
void SetAngle(const RadianAnglef& angle); void SetAngle(const RadianAnglef& angle);
void SetPhase(float phase); void SetPhase(float phase);
void SetRatchet(float ratchet); void SetRatchet(float ratchet);
template<typename... Args> static RatchetConstraint2DRef New(Args&&... args);
}; };
class RotaryLimitConstraint2D; class RotaryLimitConstraint2D;
using RotaryLimitConstraint2DConstRef = ObjectRef<const RotaryLimitConstraint2D>; using RotaryLimitConstraint2DHandle = ObjectHandle<RotaryLimitConstraint2D>;
using RotaryLimitConstraint2DRef = ObjectRef<RotaryLimitConstraint2D>;
class NAZARA_PHYSICS2D_API RotaryLimitConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API RotaryLimitConstraint2D : public Constraint2D
{ {
@ -228,14 +206,11 @@ namespace Nz
void SetMaxAngle(const RadianAnglef& maxAngle); void SetMaxAngle(const RadianAnglef& maxAngle);
void SetMinAngle(const RadianAnglef& minAngle); void SetMinAngle(const RadianAnglef& minAngle);
template<typename... Args> static RotaryLimitConstraint2DRef New(Args&&... args);
}; };
class SlideConstraint2D; class SlideConstraint2D;
using SlideConstraint2DConstRef = ObjectRef<const SlideConstraint2D>; using SlideConstraint2DHandle = ObjectHandle<SlideConstraint2D>;
using SlideConstraint2DRef = ObjectRef<SlideConstraint2D>;
class NAZARA_PHYSICS2D_API SlideConstraint2D : public Constraint2D class NAZARA_PHYSICS2D_API SlideConstraint2D : public Constraint2D
{ {
@ -252,8 +227,6 @@ namespace Nz
void SetMaxDistance(float newMaxDistance); void SetMaxDistance(float newMaxDistance);
void SetMinDistance(float newMinDistance); void SetMinDistance(float newMinDistance);
void SetSecondAnchor(const Vector2f& firstAnchor); void SetSecondAnchor(const Vector2f& firstAnchor);
template<typename... Args> static SlideConstraint2DRef New(Args&&... args);
}; };
} }

View File

@ -8,86 +8,6 @@
namespace Nz namespace Nz
{ {
template<typename... Args>
DampedSpringConstraint2DRef DampedSpringConstraint2D::New(Args&&... args)
{
std::unique_ptr<DampedSpringConstraint2D> object(new DampedSpringConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
DampedRotarySpringConstraint2DRef DampedRotarySpringConstraint2D::New(Args&&... args)
{
std::unique_ptr<DampedRotarySpringConstraint2D> object(new DampedRotarySpringConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
GearConstraint2DRef GearConstraint2D::New(Args&&... args)
{
std::unique_ptr<GearConstraint2D> object(new GearConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
MotorConstraint2DRef MotorConstraint2D::New(Args&&... args)
{
std::unique_ptr<MotorConstraint2D> object(new MotorConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
PinConstraint2DRef PinConstraint2D::New(Args&&... args)
{
std::unique_ptr<PinConstraint2D> object(new PinConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
PivotConstraint2DRef PivotConstraint2D::New(Args&&... args)
{
std::unique_ptr<PivotConstraint2D> object(new PivotConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
RatchetConstraint2DRef RatchetConstraint2D::New(Args&&... args)
{
std::unique_ptr<RatchetConstraint2D> object(new RatchetConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
RotaryLimitConstraint2DRef RotaryLimitConstraint2D::New(Args&&... args)
{
std::unique_ptr<RotaryLimitConstraint2D> object(new RotaryLimitConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
template<typename... Args>
SlideConstraint2DRef SlideConstraint2D::New(Args&&... args)
{
std::unique_ptr<SlideConstraint2D> object(new SlideConstraint2D(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
} }
#include <Nazara/Physics2D/DebugOff.hpp> #include <Nazara/Physics2D/DebugOff.hpp>