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
This commit is contained in:
Faymoon
2018-01-13 11:03:03 +01:00
committed by Jérôme Leclercq
parent 48b9203cb1
commit 4f6bf30f24
18 changed files with 344 additions and 117 deletions

View File

@@ -17,6 +17,7 @@ namespace Ndk
class NDK_API CollisionComponent2D : public Component<CollisionComponent2D>
{
friend class PhysicsSystem2D;
friend class ConstraintComponent2D;
public:
CollisionComponent2D(Nz::Collider2DRef geom = Nz::Collider2DRef());

View File

@@ -0,0 +1,34 @@
#pragma once
#ifndef NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP
#define NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP
#include <NDK/Component.hpp>
#include <Nazara/Physics2D/Constraint2D.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <vector>
#include <memory>
namespace Ndk
{
class NDK_API ConstraintComponent2D : public Component<ConstraintComponent2D>
{
public:
ConstraintComponent2D() = default;
ConstraintComponent2D(const 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);
static ComponentIndex componentIndex;
private:
std::vector<Nz::Constraint2DRef> m_constraints;
};
}
#include <NDK/Components/ConstraintComponent2D.inl>
#endif// NDK_COMPONENTS_CONSTRAINTCOMPONENT2D_HPP

View File

@@ -0,0 +1,29 @@
#include <NDK/Components/ConstraintComponent2D.hpp>
#include <NDK/Components/PhysicsComponent2D.hpp>
#include <NDK/Components/CollisionComponent2D.hpp>
namespace Ndk
{
template<typename T, typename ...Args>
inline Nz::ObjectRef<T> ConstraintComponent2D::CreateConstraint(const Ndk::EntityHandle first, const Ndk::EntityHandle second, Args && ...args)
{
auto QueryBody = [](const Ndk::EntityHandle& entity) -> Nz::RigidBody2D*
{
if (entity->HasComponent<Ndk::PhysicsComponent2D>())
return entity->GetComponent<Ndk::PhysicsComponent2D>().GetRigidBody();
else if (entity->HasComponent<Ndk::CollisionComponent2D>())
return entity->GetComponent<Ndk::CollisionComponent2D>().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<T> constraint = T::New(*body_first, *body_second, std::forward<Args>(args)...);
m_constraints.push_back(constraint);
return constraint;
}
}

View File

@@ -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;

View File

@@ -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();
}
}

View File

@@ -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;

View File

@@ -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();
}
/*!