SDK: Add components and system for 2D physics
This commit is contained in:
parent
48f7a955f6
commit
74f3ac8021
|
|
@ -6,6 +6,7 @@
|
|||
#define NDK_COMPONENTS_GLOBAL_HPP
|
||||
|
||||
#include <NDK/Components/CameraComponent.hpp>
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/LightComponent.hpp>
|
||||
|
|
@ -13,6 +14,7 @@
|
|||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/ParticleEmitterComponent.hpp>
|
||||
#include <NDK/Components/ParticleGroupComponent.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <NDK/Components/VelocityComponent.hpp>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_COMPONENTS_COLLISIONCOMPONENT2D_HPP
|
||||
#define NDK_COMPONENTS_COLLISIONCOMPONENT2D_HPP
|
||||
|
||||
#include <Nazara/Physics2D/Collider2D.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class RigidBody2D;
|
||||
}
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class Entity;
|
||||
|
||||
class NDK_API CollisionComponent2D : public Component<CollisionComponent2D>
|
||||
{
|
||||
friend class PhysicsSystem2D;
|
||||
|
||||
public:
|
||||
CollisionComponent2D(Nz::Collider2DRef geom = Nz::Collider2DRef());
|
||||
CollisionComponent2D(const CollisionComponent2D& collision);
|
||||
~CollisionComponent2D() = default;
|
||||
|
||||
const Nz::Collider2DRef& GetGeom() const;
|
||||
|
||||
void SetGeom(Nz::Collider2DRef geom);
|
||||
|
||||
CollisionComponent2D& operator=(Nz::Collider2DRef geom);
|
||||
CollisionComponent2D& operator=(CollisionComponent2D&& collision) = default;
|
||||
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
void InitializeStaticBody();
|
||||
Nz::RigidBody2D* GetStaticBody();
|
||||
|
||||
void OnAttached() override;
|
||||
void OnComponentAttached(BaseComponent& component) override;
|
||||
void OnComponentDetached(BaseComponent& component) override;
|
||||
void OnDetached() override;
|
||||
|
||||
std::unique_ptr<Nz::RigidBody2D> m_staticBody;
|
||||
Nz::Collider2DRef m_geom;
|
||||
bool m_bodyUpdated;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Components/CollisionComponent2D.inl>
|
||||
|
||||
#endif // NDK_COMPONENTS_COLLISIONCOMPONENT2D_HPP
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Entity.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \brief Constructs a CollisionComponent2D object with a geometry
|
||||
*
|
||||
* \param geom Reference to a geometry symbolizing the entity
|
||||
*/
|
||||
|
||||
inline CollisionComponent2D::CollisionComponent2D(Nz::Collider2DRef geom) :
|
||||
m_geom(std::move(geom)),
|
||||
m_bodyUpdated(false)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a CollisionComponent2D object by copy semantic
|
||||
*
|
||||
* \param collision CollisionComponent2D to copy
|
||||
*/
|
||||
|
||||
inline CollisionComponent2D::CollisionComponent2D(const CollisionComponent2D& collision) :
|
||||
m_geom(collision.m_geom),
|
||||
m_bodyUpdated(false)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the geometry representing the entity
|
||||
* \return A constant reference to the physics geometry
|
||||
*/
|
||||
|
||||
inline const Nz::Collider2DRef& CollisionComponent2D::GetGeom() const
|
||||
{
|
||||
return m_geom;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Assigns the geometry to this component
|
||||
* \return A reference to this
|
||||
*
|
||||
* \param geom Reference to a geometry symbolizing the entity
|
||||
*/
|
||||
|
||||
inline CollisionComponent2D& CollisionComponent2D::operator=(Nz::Collider2DRef geom)
|
||||
{
|
||||
SetGeom(geom);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the static body used by the entity
|
||||
* \return A pointer to the entity
|
||||
*/
|
||||
|
||||
inline Nz::RigidBody2D* CollisionComponent2D::GetStaticBody()
|
||||
{
|
||||
return m_staticBody.get();
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_COMPONENTS_COLLISIONCOMPONENT_HPP
|
||||
#define NDK_COMPONENTS_COLLISIONCOMPONENT_HPP
|
||||
#ifndef NDK_COMPONENTS_COLLISIONCOMPONENT3D_HPP
|
||||
#define NDK_COMPONENTS_COLLISIONCOMPONENT3D_HPP
|
||||
|
||||
#include <Nazara/Physics3D/Collider3D.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
|
|
@ -23,7 +23,6 @@ namespace Ndk
|
|||
class NDK_API CollisionComponent3D : public Component<CollisionComponent3D>
|
||||
{
|
||||
friend class PhysicsSystem3D;
|
||||
friend class StaticCollisionSystem;
|
||||
|
||||
public:
|
||||
CollisionComponent3D(Nz::Collider3DRef geom = Nz::Collider3DRef());
|
||||
|
|
@ -56,4 +55,4 @@ namespace Ndk
|
|||
|
||||
#include <NDK/Components/CollisionComponent3D.inl>
|
||||
|
||||
#endif // NDK_COMPONENTS_COLLISIONCOMPONENT_HPP
|
||||
#endif // NDK_COMPONENTS_COLLISIONCOMPONENT3D_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_COMPONENTS_PHYSICSCOMPONENT2D_HPP
|
||||
#define NDK_COMPONENTS_PHYSICSCOMPONENT2D_HPP
|
||||
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class Entity;
|
||||
|
||||
class NDK_API PhysicsComponent2D : public Component<PhysicsComponent2D>
|
||||
{
|
||||
friend class CollisionComponent2D;
|
||||
friend class PhysicsSystem2D;
|
||||
|
||||
public:
|
||||
PhysicsComponent2D() = default;
|
||||
PhysicsComponent2D(const PhysicsComponent2D& physics);
|
||||
~PhysicsComponent2D() = default;
|
||||
|
||||
void AddForce(const Nz::Vector2f& force, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddForce(const Nz::Vector2f& force, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddTorque(float torque);
|
||||
|
||||
Nz::Rectf GetAABB() const;
|
||||
float GetAngularVelocity() const;
|
||||
Nz::Vector2f GetCenterOfGravity(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||
float GetMass() const;
|
||||
Nz::Vector2f GetPosition() const;
|
||||
float GetRotation() const;
|
||||
Nz::Vector2f GetVelocity() const;
|
||||
|
||||
bool IsSleeping() const;
|
||||
|
||||
void SetAngularVelocity(float angularVelocity);
|
||||
void SetMass(float mass);
|
||||
void SetMassCenter(const Nz::Vector2f& center);
|
||||
void SetPosition(const Nz::Vector2f& position);
|
||||
void SetRotation(float rotation);
|
||||
void SetVelocity(const Nz::Vector2f& velocity);
|
||||
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
Nz::RigidBody2D& GetRigidBody();
|
||||
|
||||
void OnAttached() override;
|
||||
void OnComponentAttached(BaseComponent& component) override;
|
||||
void OnComponentDetached(BaseComponent& component) override;
|
||||
void OnDetached() override;
|
||||
|
||||
std::unique_ptr<Nz::RigidBody2D> m_object;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Components/PhysicsComponent2D.inl>
|
||||
|
||||
#endif // NDK_COMPONENTS_PHYSICSCOMPONENT2D_HPP
|
||||
|
|
@ -0,0 +1,284 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include "PhysicsComponent2D.hpp"
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \brief Constructs a PhysicsComponent2D object by copy semantic
|
||||
*
|
||||
* \param physics PhysicsComponent2D to copy
|
||||
*/
|
||||
|
||||
inline PhysicsComponent2D::PhysicsComponent2D(const PhysicsComponent2D& physics)
|
||||
{
|
||||
// No copy of physical object (because we only create it when attached to an entity)
|
||||
NazaraUnused(physics);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Applies a physics force to the entity
|
||||
*
|
||||
* \param force Force to apply on the entity
|
||||
* \param coordSys System coordinates to consider
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::AddForce(const Nz::Vector2f& force, Nz::CoordSys coordSys)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->AddForce(force, coordSys);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Applies a physics force to the entity
|
||||
*
|
||||
* \param force Force to apply on the entity
|
||||
* \param point Point where to apply the force
|
||||
* \param coordSys System coordinates to consider
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::AddForce(const Nz::Vector2f& force, const Nz::Vector2f& point, Nz::CoordSys coordSys)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->AddForce(force, point, coordSys);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Applies a torque to the entity
|
||||
*
|
||||
* \param torque Torque to apply on the entity
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::AddTorque(float torque)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->AddTorque(torque);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the AABB of the physics object
|
||||
* \return AABB of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
inline Nz::Rectf PhysicsComponent2D::GetAABB() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetAABB();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the angular velocity of the physics object
|
||||
* \return Angular velocity of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetAngularVelocity() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetAngularVelocity();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the gravity center of the physics object
|
||||
* \return Gravity center of the object
|
||||
*
|
||||
* \param coordSys System coordinates to consider
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline Nz::Vector2f PhysicsComponent2D::GetCenterOfGravity(Nz::CoordSys coordSys) const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetCenterOfGravity(coordSys);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the mass of the physics object
|
||||
* \return Mass of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetMass() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetMass();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the position of the physics object
|
||||
* \return Position of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline Nz::Vector2f PhysicsComponent2D::GetPosition() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetPosition();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the rotation of the physics object
|
||||
* \return Rotation of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetRotation() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetRotation();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the velocity of the physics object
|
||||
* \return Velocity of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline Nz::Vector2f PhysicsComponent2D::GetVelocity() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetVelocity();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the entity is currently sleeping
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline bool PhysicsComponent2D::IsSleeping() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->IsSleeping();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the angular velocity of the physics object
|
||||
*
|
||||
* \param angularVelocity Angular velocity of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetAngularVelocity(float angularVelocity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetAngularVelocity(angularVelocity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the mass of the physics object
|
||||
*
|
||||
* \param mass Mass of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
* \remark Produces a NazaraAssert if the mass is negative
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetMass(float mass)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(mass > 0.f, "Mass should be positive");
|
||||
|
||||
m_object->SetMass(mass);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the gravity center of the physics object
|
||||
*
|
||||
* \param center Gravity center of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetMassCenter(const Nz::Vector2f& center)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetMassCenter(center);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the position of the physics object
|
||||
*
|
||||
* \param position Position of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetPosition(const Nz::Vector2f& position)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetPosition(position);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the rotation of the physics object
|
||||
*
|
||||
* \param rotation Rotation of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetRotation(float rotation)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetRotation(rotation);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the velocity of the physics object
|
||||
*
|
||||
* \param velocity Velocity of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetVelocity(const Nz::Vector2f& velocity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetVelocity(velocity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the underlying physics object
|
||||
* \return A reference to the physics object
|
||||
*/
|
||||
|
||||
inline Nz::RigidBody2D& PhysicsComponent2D::GetRigidBody()
|
||||
{
|
||||
return *m_object.get();
|
||||
}
|
||||
}
|
||||
|
|
@ -56,7 +56,7 @@ namespace Ndk
|
|||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
Nz::RigidBody3D& GetPhysObject();
|
||||
Nz::RigidBody3D& GetRigidBody();
|
||||
|
||||
void OnAttached() override;
|
||||
void OnComponentAttached(BaseComponent& component) override;
|
||||
|
|
|
|||
|
|
@ -350,7 +350,7 @@ namespace Ndk
|
|||
* \return A reference to the physics object
|
||||
*/
|
||||
|
||||
inline Nz::RigidBody3D& PhysicsComponent3D::GetPhysObject()
|
||||
inline Nz::RigidBody3D& PhysicsComponent3D::GetRigidBody()
|
||||
{
|
||||
return *m_object.get();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <NDK/Systems/ListenerSystem.hpp>
|
||||
#include <NDK/Systems/ParticleSystem.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
||||
#include <NDK/Systems/RenderSystem.hpp>
|
||||
#include <NDK/Systems/VelocitySystem.hpp>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_SYSTEMS_PHYSICSSYSTEM2D_HPP
|
||||
#define NDK_SYSTEMS_PHYSICSSYSTEM2D_HPP
|
||||
|
||||
#include <Nazara/Physics2D/PhysWorld2D.hpp>
|
||||
#include <NDK/EntityList.hpp>
|
||||
#include <NDK/System.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class NDK_API PhysicsSystem2D : public System<PhysicsSystem2D>
|
||||
{
|
||||
public:
|
||||
PhysicsSystem2D();
|
||||
PhysicsSystem2D(const PhysicsSystem2D& system);
|
||||
~PhysicsSystem2D() = default;
|
||||
|
||||
Nz::PhysWorld2D& GetWorld();
|
||||
const Nz::PhysWorld2D& GetWorld() const;
|
||||
|
||||
static SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
void CreatePhysWorld() const;
|
||||
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
||||
void OnUpdate(float elapsedTime) override;
|
||||
|
||||
EntityList m_dynamicObjects;
|
||||
EntityList m_staticObjects;
|
||||
mutable std::unique_ptr<Nz::PhysWorld2D> m_world; ///TODO: std::optional (Should I make a Nz::Optional class?)
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Systems/PhysicsSystem2D.inl>
|
||||
|
||||
#endif // NDK_SYSTEMS_PHYSICSSYSTEM2D_HPP
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \brief Gets the physical world
|
||||
* \return A reference to the physical world
|
||||
*/
|
||||
|
||||
inline Nz::PhysWorld2D& PhysicsSystem2D::GetWorld()
|
||||
{
|
||||
if (!m_world)
|
||||
CreatePhysWorld();
|
||||
|
||||
return *m_world;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the physical world
|
||||
* \return A constant reference to the physical world
|
||||
*/
|
||||
|
||||
inline const Nz::PhysWorld2D& PhysicsSystem2D::GetWorld() const
|
||||
{
|
||||
if (!m_world)
|
||||
CreatePhysWorld();
|
||||
|
||||
return *m_world;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,108 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NDK/Algorithm.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::CollisionComponent2D
|
||||
* \brief NDK class that represents a two-dimensional collision geometry
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Sets geometry for the entity
|
||||
*
|
||||
* \param geom Geometry used for collisions
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the entity has no physics component and has no static body
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::SetGeom(Nz::Collider2DRef geom)
|
||||
{
|
||||
m_geom = std::move(geom);
|
||||
|
||||
if (m_entity->HasComponent<PhysicsComponent2D>())
|
||||
{
|
||||
// We update the geometry of the PhysiscsObject linked to the PhysicsComponent2D
|
||||
PhysicsComponent2D& physComponent = m_entity->GetComponent<PhysicsComponent2D>();
|
||||
physComponent.GetRigidBody().SetGeom(m_geom);
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraAssert(m_staticBody, "An entity without physics component should have a static body");
|
||||
m_staticBody->SetGeom(m_geom);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Initializes the static body
|
||||
*
|
||||
* \remark Produces a NazaraAssert if entity is invalid
|
||||
* \remark Produces a NazaraAssert if entity is not linked to a world, or the world has no physics system
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::InitializeStaticBody()
|
||||
{
|
||||
NazaraAssert(m_entity, "Invalid entity");
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
|
||||
NazaraAssert(entityWorld, "Entity must have world");
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a physics system");
|
||||
Nz::PhysWorld2D& physWorld = entityWorld->GetSystem<PhysicsSystem2D>().GetWorld();
|
||||
|
||||
m_staticBody.reset(new Nz::RigidBody2D(&physWorld, m_geom));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnAttached()
|
||||
{
|
||||
if (!m_entity->HasComponent<PhysicsComponent2D>())
|
||||
InitializeStaticBody();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<PhysicsComponent2D>(component))
|
||||
m_staticBody.reset();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<PhysicsComponent2D>(component))
|
||||
InitializeStaticBody();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::OnDetached()
|
||||
{
|
||||
m_staticBody.reset();
|
||||
}
|
||||
|
||||
ComponentIndex CollisionComponent2D::componentIndex;
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ namespace Ndk
|
|||
{
|
||||
// We update the geometry of the PhysiscsObject linked to the PhysicsComponent3D
|
||||
PhysicsComponent3D& physComponent = m_entity->GetComponent<PhysicsComponent3D>();
|
||||
physComponent.GetPhysObject().SetGeom(m_geom);
|
||||
physComponent.GetRigidBody().SetGeom(m_geom);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,92 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NDK/Algorithm.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::PhysicsComponent2D
|
||||
* \brief NDK class that represents a physics point, without any collision
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to an entity
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the world does not have a physics system
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnAttached()
|
||||
{
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a 2D physics system");
|
||||
|
||||
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetWorld();
|
||||
|
||||
Nz::Collider2DRef geom;
|
||||
if (m_entity->HasComponent<CollisionComponent2D>())
|
||||
geom = m_entity->GetComponent<CollisionComponent2D>().GetGeom();
|
||||
|
||||
Nz::Matrix4f matrix;
|
||||
if (m_entity->HasComponent<NodeComponent>())
|
||||
matrix = m_entity->GetComponent<NodeComponent>().GetTransformMatrix();
|
||||
else
|
||||
matrix.MakeIdentity();
|
||||
|
||||
m_object.reset(new Nz::RigidBody2D(&world, 1.f, geom));
|
||||
m_object->SetPosition(Nz::Vector2f(matrix.GetTranslation()));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is attached to this component
|
||||
*
|
||||
* \param component Component being attached
|
||||
*
|
||||
* \remark Produces a NazaraAssert if physical object is invalid
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnComponentAttached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<CollisionComponent2D>(component))
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid object");
|
||||
m_object->SetGeom(static_cast<CollisionComponent2D&>(component).GetGeom());
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from this component
|
||||
*
|
||||
* \param component Component being detached
|
||||
*
|
||||
* \remark Produces a NazaraAssert if physical object is invalid
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnComponentDetached(BaseComponent& component)
|
||||
{
|
||||
if (IsComponent<CollisionComponent2D>(component))
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid object");
|
||||
m_object->SetGeom(Nz::NullCollider2D::New());
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when component is detached from an entity
|
||||
*/
|
||||
|
||||
void PhysicsComponent2D::OnDetached()
|
||||
{
|
||||
m_object.reset();
|
||||
}
|
||||
|
||||
ComponentIndex PhysicsComponent2D::componentIndex;
|
||||
}
|
||||
|
|
@ -9,14 +9,18 @@
|
|||
#include <Nazara/Graphics/Graphics.hpp>
|
||||
#include <Nazara/Lua/Lua.hpp>
|
||||
#include <Nazara/Noise/Noise.hpp>
|
||||
#include <Nazara/Physics2D/Physics2D.hpp>
|
||||
#include <Nazara/Physics3D/Physics3D.hpp>
|
||||
#include <Nazara/Utility/Utility.hpp>
|
||||
#include <NDK/Algorithm.hpp>
|
||||
#include <NDK/BaseSystem.hpp>
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <NDK/Components/VelocityComponent.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
||||
#include <NDK/Systems/VelocitySystem.hpp>
|
||||
|
||||
|
|
@ -68,6 +72,7 @@ namespace Ndk
|
|||
|
||||
Nz::Lua::Initialize();
|
||||
Nz::Noise::Initialize();
|
||||
Nz::Physics2D::Initialize();
|
||||
Nz::Physics3D::Initialize();
|
||||
Nz::Utility::Initialize();
|
||||
|
||||
|
|
@ -83,9 +88,11 @@ namespace Ndk
|
|||
BaseComponent::Initialize();
|
||||
|
||||
// Shared components
|
||||
InitializeComponent<CollisionComponent3D>("NdkColli");
|
||||
InitializeComponent<CollisionComponent2D>("NdkColl2");
|
||||
InitializeComponent<CollisionComponent3D>("NdkColl3");
|
||||
InitializeComponent<NodeComponent>("NdkNode");
|
||||
InitializeComponent<PhysicsComponent3D>("NdkPhys");
|
||||
InitializeComponent<PhysicsComponent2D>("NdkPhys2");
|
||||
InitializeComponent<PhysicsComponent3D>("NdkPhys3");
|
||||
InitializeComponent<VelocityComponent>("NdkVeloc");
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
|
|
@ -103,6 +110,7 @@ namespace Ndk
|
|||
BaseSystem::Initialize();
|
||||
|
||||
// Shared systems
|
||||
InitializeSystem<PhysicsSystem2D>();
|
||||
InitializeSystem<PhysicsSystem3D>();
|
||||
InitializeSystem<VelocitySystem>();
|
||||
|
||||
|
|
@ -161,6 +169,7 @@ namespace Ndk
|
|||
// Shared modules
|
||||
Nz::Lua::Uninitialize();
|
||||
Nz::Noise::Uninitialize();
|
||||
Nz::Physics2D::Uninitialize();
|
||||
Nz::Physics3D::Uninitialize();
|
||||
Nz::Utility::Uninitialize();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,140 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Components/PhysicsComponent3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::PhysicsSystem2D
|
||||
* \brief NDK class that represents a two-dimensional physics system
|
||||
*
|
||||
* \remark This system is enabled if the entity has the trait: NodeComponent and any of these two: CollisionComponent3D or PhysicsComponent3D
|
||||
* \remark Static objects do not have a velocity specified by the physical engine
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an PhysicsSystem object by default
|
||||
*/
|
||||
|
||||
PhysicsSystem2D::PhysicsSystem2D()
|
||||
{
|
||||
Requires<NodeComponent>();
|
||||
RequiresAny<CollisionComponent2D, PhysicsComponent2D>();
|
||||
Excludes<PhysicsComponent3D>();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a PhysicsSystem object by copy semantic
|
||||
*
|
||||
* \param system PhysicsSystem to copy
|
||||
*/
|
||||
|
||||
PhysicsSystem2D::PhysicsSystem2D(const PhysicsSystem2D& system) :
|
||||
System(system),
|
||||
m_world()
|
||||
{
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::CreatePhysWorld() const
|
||||
{
|
||||
NazaraAssert(!m_world, "Physics world should not be created twice");
|
||||
|
||||
m_world = std::make_unique<Nz::PhysWorld2D>();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when entity is validated for the system
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
* \param justAdded Is the entity newly added
|
||||
*/
|
||||
|
||||
void PhysicsSystem2D::OnEntityValidation(Entity* entity, bool justAdded)
|
||||
{
|
||||
// It's possible our entity got revalidated because of the addition/removal of a PhysicsComponent3D
|
||||
if (!justAdded)
|
||||
{
|
||||
// We take the opposite array from which the entity should belong to
|
||||
auto& entities = (entity->HasComponent<PhysicsComponent2D>()) ? m_staticObjects : m_dynamicObjects;
|
||||
entities.Remove(entity);
|
||||
}
|
||||
|
||||
auto& entities = (entity->HasComponent<PhysicsComponent2D>()) ? m_dynamicObjects : m_staticObjects;
|
||||
entities.Insert(entity);
|
||||
|
||||
if (!m_world)
|
||||
CreatePhysWorld();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to perform when system is updated
|
||||
*
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*/
|
||||
|
||||
void PhysicsSystem2D::OnUpdate(float elapsedTime)
|
||||
{
|
||||
if (!m_world)
|
||||
return;
|
||||
|
||||
m_world->Step(elapsedTime);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
|
||||
{
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
PhysicsComponent2D& phys = entity->GetComponent<PhysicsComponent2D>();
|
||||
|
||||
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;
|
||||
for (const Ndk::EntityHandle& entity : m_staticObjects)
|
||||
{
|
||||
CollisionComponent2D& collision = entity->GetComponent<CollisionComponent2D>();
|
||||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
|
||||
Nz::RigidBody2D* body = collision.GetStaticBody();
|
||||
|
||||
Nz::Vector2f oldPosition = body->GetPosition();
|
||||
Nz::Vector2f newPosition = Nz::Vector2f(node.GetPosition(Nz::CoordSys_Global));
|
||||
|
||||
// To move static objects and ensure their collisions, we have to specify them a velocity
|
||||
// (/!\: the physical motor does not apply the speed on static objects)
|
||||
if (newPosition != oldPosition)
|
||||
{
|
||||
body->SetPosition(newPosition);
|
||||
body->SetVelocity((newPosition - oldPosition) * invElapsedTime);
|
||||
}
|
||||
else
|
||||
body->SetVelocity(Nz::Vector2f::Zero());
|
||||
|
||||
/*
|
||||
if (newRotation != oldRotation)
|
||||
{
|
||||
Nz::Quaternionf transition = newRotation * oldRotation.GetConjugate();
|
||||
Nz::EulerAnglesf angles = transition.ToEulerAngles();
|
||||
Nz::Vector3f angularVelocity(Nz::ToRadians(angles.pitch * invElapsedTime),
|
||||
Nz::ToRadians(angles.yaw * invElapsedTime),
|
||||
Nz::ToRadians(angles.roll * invElapsedTime));
|
||||
|
||||
physObj->SetRotation(oldRotation);
|
||||
physObj->SetAngularVelocity(angularVelocity);
|
||||
}
|
||||
else
|
||||
physObj->SetAngularVelocity(Nz::Vector3f::Zero());
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
SystemIndex PhysicsSystem2D::systemIndex;
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
#include <Nazara/Physics3D/RigidBody3D.hpp>
|
||||
#include <NDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Components/PhysicsComponent3D.hpp>
|
||||
|
||||
namespace Ndk
|
||||
|
|
@ -27,6 +28,7 @@ namespace Ndk
|
|||
{
|
||||
Requires<NodeComponent>();
|
||||
RequiresAny<CollisionComponent3D, PhysicsComponent3D>();
|
||||
Excludes<PhysicsComponent2D>();
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
@ -90,7 +92,7 @@ namespace Ndk
|
|||
NodeComponent& node = entity->GetComponent<NodeComponent>();
|
||||
PhysicsComponent3D& phys = entity->GetComponent<PhysicsComponent3D>();
|
||||
|
||||
Nz::RigidBody3D& physObj = phys.GetPhysObject();
|
||||
Nz::RigidBody3D& physObj = phys.GetRigidBody();
|
||||
node.SetRotation(physObj.GetRotation(), Nz::CoordSys_Global);
|
||||
node.SetPosition(physObj.GetPosition(), Nz::CoordSys_Global);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include <NDK/World.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <NDK/BaseComponent.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem3D.hpp>
|
||||
#include <NDK/Systems/VelocitySystem.hpp>
|
||||
|
||||
|
|
@ -40,6 +41,7 @@ namespace Ndk
|
|||
|
||||
void World::AddDefaultSystems()
|
||||
{
|
||||
AddSystem<PhysicsSystem2D>();
|
||||
AddSystem<PhysicsSystem3D>();
|
||||
AddSystem<VelocitySystem>();
|
||||
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ namespace Nz
|
|||
RigidBody2D& operator=(RigidBody2D&& object);
|
||||
|
||||
private:
|
||||
void Create(float mass = 0.f, float moment = 0.f);
|
||||
void Create(float mass = 1.f, float moment = 1.f);
|
||||
void Destroy();
|
||||
|
||||
std::vector<cpShape*> m_shapes;
|
||||
|
|
|
|||
Loading…
Reference in New Issue