Update Physics2D Component and Body (#178)
* Update * Add: [Get/Set]AngularDaming for standardization * Fix: Name error * Add: [Get/Set][AngularDamping/MomentOfInertia] in PhysicsComponent2D * Forgot in last commit * Add: param coordSys in [PhysicsComponent2D/RigidBody2D]::SetMassCenter * Add: Some forgotten inline * Fix little error * Fix: Indentation before case * Move and Change GetCenterOfGravity
This commit is contained in:
parent
4a09de7e0b
commit
2f3f02b2fc
|
|
@ -24,30 +24,36 @@ namespace Ndk
|
||||||
PhysicsComponent2D(const PhysicsComponent2D& physics);
|
PhysicsComponent2D(const PhysicsComponent2D& physics);
|
||||||
~PhysicsComponent2D() = default;
|
~PhysicsComponent2D() = default;
|
||||||
|
|
||||||
void AddForce(const Nz::Vector2f& force, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
inline 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);
|
inline void AddForce(const Nz::Vector2f& force, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||||
void AddImpulse(const Nz::Vector2f& impulse, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
inline void AddImpulse(const Nz::Vector2f& impulse, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||||
void AddImpulse(const Nz::Vector2f& impulse, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
inline void AddImpulse(const Nz::Vector2f& impulse, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||||
void AddTorque(float torque);
|
inline void AddTorque(float torque);
|
||||||
|
|
||||||
bool ClosestPointQuery(const Nz::Vector2f& position, Nz::Vector2f* closestPoint, float* closestDistance) const;
|
inline bool ClosestPointQuery(const Nz::Vector2f& position, Nz::Vector2f* closestPoint, float* closestDistance) const;
|
||||||
|
|
||||||
Nz::Rectf GetAABB() const;
|
inline Nz::Rectf GetAABB() const;
|
||||||
float GetAngularVelocity() const;
|
inline float GetAngularDamping() const;
|
||||||
Nz::Vector2f GetCenterOfGravity(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
inline float GetAngularVelocity() const;
|
||||||
float GetMass() const;
|
NAZARA_DEPRECATED("Name error, please use GetMassCenter")
|
||||||
Nz::Vector2f GetPosition() const;
|
inline Nz::Vector2f GetCenterOfGravity(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||||
float GetRotation() const;
|
inline float GetMass() const;
|
||||||
Nz::Vector2f GetVelocity() const;
|
inline Nz::Vector2f GetMassCenter(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||||
|
inline float GetMomentOfInertia() const;
|
||||||
|
inline Nz::Vector2f GetPosition() const;
|
||||||
|
inline float GetRotation() const;
|
||||||
|
inline Nz::Vector2f GetVelocity() const;
|
||||||
|
|
||||||
bool IsSleeping() const;
|
inline bool IsSleeping() const;
|
||||||
|
|
||||||
void SetAngularVelocity(float angularVelocity);
|
inline void SetAngularDamping(float angularDamping);
|
||||||
void SetMass(float mass);
|
inline void SetAngularVelocity(float angularVelocity);
|
||||||
void SetMassCenter(const Nz::Vector2f& center);
|
inline void SetMass(float mass);
|
||||||
void SetPosition(const Nz::Vector2f& position);
|
inline void SetMassCenter(const Nz::Vector2f& center, Nz::CoordSys coordSys = Nz::CoordSys_Local);
|
||||||
void SetRotation(float rotation);
|
inline void SetMomentOfInertia(float moment);
|
||||||
void SetVelocity(const Nz::Vector2f& velocity);
|
inline void SetPosition(const Nz::Vector2f& position);
|
||||||
|
inline void SetRotation(float rotation);
|
||||||
|
inline void SetVelocity(const Nz::Vector2f& velocity);
|
||||||
|
|
||||||
static ComponentIndex componentIndex;
|
static ComponentIndex componentIndex;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,22 @@ namespace Ndk
|
||||||
return m_object->GetAABB();
|
return m_object->GetAABB();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Gets the angular damping or moment of inertia of the physics object
|
||||||
|
* \return Angular damping of the object
|
||||||
|
*
|
||||||
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
|
*
|
||||||
|
* \see GetMomentOfInertia
|
||||||
|
*/
|
||||||
|
|
||||||
|
inline float PhysicsComponent2D::GetAngularDamping() const
|
||||||
|
{
|
||||||
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
||||||
|
return m_object->GetAngularDamping();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Gets the angular velocity of the physics object
|
* \brief Gets the angular velocity of the physics object
|
||||||
* \return Angular velocity of the object
|
* \return Angular velocity of the object
|
||||||
|
|
@ -172,6 +188,38 @@ namespace Ndk
|
||||||
return m_object->GetMass();
|
return m_object->GetMass();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \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::GetMassCenter(Nz::CoordSys coordSys) const
|
||||||
|
{
|
||||||
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
||||||
|
return m_object->GetMassCenter(coordSys);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Gets the angular damping or moment of inertia of the physics object
|
||||||
|
* \return Moment of inertia of the object
|
||||||
|
*
|
||||||
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
|
*
|
||||||
|
* \see GetAngularDamping
|
||||||
|
*/
|
||||||
|
|
||||||
|
inline float PhysicsComponent2D::GetMomentOfInertia() const
|
||||||
|
{
|
||||||
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
||||||
|
return m_object->GetMomentOfInertia();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Gets the position of the physics object
|
* \brief Gets the position of the physics object
|
||||||
* \return Position of the object
|
* \return Position of the object
|
||||||
|
|
@ -228,6 +276,23 @@ namespace Ndk
|
||||||
return m_object->IsSleeping();
|
return m_object->IsSleeping();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Sets the angular damping or moment of inertia of the physics object
|
||||||
|
*
|
||||||
|
* \param angularDamping Angular damping of the object
|
||||||
|
*
|
||||||
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
|
*
|
||||||
|
* \see SetMomentOfInertia
|
||||||
|
*/
|
||||||
|
|
||||||
|
inline void PhysicsComponent2D::SetAngularDamping(float angularDamping)
|
||||||
|
{
|
||||||
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
||||||
|
m_object->SetAngularDamping(angularDamping);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Sets the angular velocity of the physics object
|
* \brief Sets the angular velocity of the physics object
|
||||||
*
|
*
|
||||||
|
|
@ -268,11 +333,27 @@ namespace Ndk
|
||||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline void PhysicsComponent2D::SetMassCenter(const Nz::Vector2f& center)
|
inline void PhysicsComponent2D::SetMassCenter(const Nz::Vector2f& center, Nz::CoordSys coordSys)
|
||||||
{
|
{
|
||||||
NazaraAssert(m_object, "Invalid physics object");
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
||||||
m_object->SetMassCenter(center);
|
m_object->SetMassCenter(center, coordSys);
|
||||||
|
}
|
||||||
|
/*!
|
||||||
|
* \brief Sets the angular damping or moment of inertia of the physics object
|
||||||
|
*
|
||||||
|
* \param moment Moment of inertia of the object
|
||||||
|
*
|
||||||
|
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||||
|
*
|
||||||
|
* \see SetAngularDamping
|
||||||
|
*/
|
||||||
|
|
||||||
|
inline void PhysicsComponent2D::SetMomentOfInertia(float moment)
|
||||||
|
{
|
||||||
|
NazaraAssert(m_object, "Invalid physics object");
|
||||||
|
|
||||||
|
m_object->SetMomentOfInertia(moment);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
||||||
|
|
@ -43,10 +43,12 @@ namespace Nz
|
||||||
Rectf GetAABB() const;
|
Rectf GetAABB() const;
|
||||||
inline float GetAngularDamping() const;
|
inline float GetAngularDamping() const;
|
||||||
float GetAngularVelocity() const;
|
float GetAngularVelocity() const;
|
||||||
Vector2f GetCenterOfGravity(CoordSys coordSys = CoordSys_Local) const;
|
NAZARA_DEPRECATED("Name error, please use GetMassCenter")
|
||||||
|
inline Vector2f GetCenterOfGravity(CoordSys coordSys = CoordSys_Local) const;
|
||||||
const Collider2DRef& GetGeom() const;
|
const Collider2DRef& GetGeom() const;
|
||||||
cpBody* GetHandle() const;
|
cpBody* GetHandle() const;
|
||||||
float GetMass() const;
|
float GetMass() const;
|
||||||
|
Vector2f GetMassCenter(CoordSys coordSys = CoordSys_Local) const;
|
||||||
float GetMomentOfInertia() const;
|
float GetMomentOfInertia() const;
|
||||||
Vector2f GetPosition() const;
|
Vector2f GetPosition() const;
|
||||||
float GetRotation() const;
|
float GetRotation() const;
|
||||||
|
|
@ -64,7 +66,7 @@ namespace Nz
|
||||||
void SetAngularVelocity(float angularVelocity);
|
void SetAngularVelocity(float angularVelocity);
|
||||||
void SetGeom(Collider2DRef geom, bool recomputeMoment = true);
|
void SetGeom(Collider2DRef geom, bool recomputeMoment = true);
|
||||||
void SetMass(float mass, bool recomputeMoment = true);
|
void SetMass(float mass, bool recomputeMoment = true);
|
||||||
void SetMassCenter(const Vector2f& center);
|
void SetMassCenter(const Vector2f& center, CoordSys coordSys = CoordSys_Local);
|
||||||
void SetMomentOfInertia(float moment);
|
void SetMomentOfInertia(float moment);
|
||||||
void SetPosition(const Vector2f& position);
|
void SetPosition(const Vector2f& position);
|
||||||
void SetRotation(float rotation);
|
void SetRotation(float rotation);
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,11 @@ namespace Nz
|
||||||
return GetMomentOfInertia();
|
return GetMomentOfInertia();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Vector2f RigidBody2D::GetCenterOfGravity(CoordSys coordSys) const
|
||||||
|
{
|
||||||
|
return GetMassCenter(coordSys);
|
||||||
|
}
|
||||||
|
|
||||||
inline void RigidBody2D::SetAngularDamping(float angularDamping)
|
inline void RigidBody2D::SetAngularDamping(float angularDamping)
|
||||||
{
|
{
|
||||||
SetMomentOfInertia(angularDamping);
|
SetMomentOfInertia(angularDamping);
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ namespace Nz
|
||||||
|
|
||||||
void RigidBody2D::AddForce(const Vector2f& force, CoordSys coordSys)
|
void RigidBody2D::AddForce(const Vector2f& force, CoordSys coordSys)
|
||||||
{
|
{
|
||||||
return AddForce(force, GetCenterOfGravity(coordSys), coordSys);
|
return AddForce(force, GetMassCenter(coordSys), coordSys);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigidBody2D::AddForce(const Vector2f& force, const Vector2f& point, CoordSys coordSys)
|
void RigidBody2D::AddForce(const Vector2f& force, const Vector2f& point, CoordSys coordSys)
|
||||||
|
|
@ -106,7 +106,7 @@ namespace Nz
|
||||||
|
|
||||||
void RigidBody2D::AddImpulse(const Vector2f& impulse, CoordSys coordSys)
|
void RigidBody2D::AddImpulse(const Vector2f& impulse, CoordSys coordSys)
|
||||||
{
|
{
|
||||||
return AddImpulse(impulse, GetCenterOfGravity(coordSys), coordSys);
|
return AddImpulse(impulse, GetMassCenter(coordSys), coordSys);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigidBody2D::AddImpulse(const Vector2f& impulse, const Vector2f& point, CoordSys coordSys)
|
void RigidBody2D::AddImpulse(const Vector2f& impulse, const Vector2f& point, CoordSys coordSys)
|
||||||
|
|
@ -205,26 +205,26 @@ namespace Nz
|
||||||
return m_mass;
|
return m_mass;
|
||||||
}
|
}
|
||||||
|
|
||||||
float RigidBody2D::GetMomentOfInertia() const
|
Vector2f RigidBody2D::GetMassCenter(CoordSys coordSys) const
|
||||||
{
|
{
|
||||||
return float(cpBodyGetMoment(m_handle));
|
cpVect massCenter = cpBodyGetCenterOfGravity(m_handle);
|
||||||
}
|
|
||||||
|
|
||||||
Vector2f RigidBody2D::GetCenterOfGravity(CoordSys coordSys) const
|
|
||||||
{
|
|
||||||
cpVect cog = cpBodyGetCenterOfGravity(m_handle);
|
|
||||||
|
|
||||||
switch (coordSys)
|
switch (coordSys)
|
||||||
{
|
{
|
||||||
case CoordSys_Global:
|
case CoordSys_Global:
|
||||||
cog = cpBodyLocalToWorld(m_handle, cog);
|
massCenter = cpBodyLocalToWorld(m_handle, massCenter);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CoordSys_Local:
|
case CoordSys_Local:
|
||||||
break; // Nothing to do
|
break; // Nothing to do
|
||||||
}
|
}
|
||||||
|
|
||||||
return Vector2f(static_cast<float>(cog.x), static_cast<float>(cog.y));
|
return Vector2f(static_cast<float>(massCenter.x), static_cast<float>(massCenter.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
float RigidBody2D::GetMomentOfInertia() const
|
||||||
|
{
|
||||||
|
return float(cpBodyGetMoment(m_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2f RigidBody2D::GetPosition() const
|
Vector2f RigidBody2D::GetPosition() const
|
||||||
|
|
@ -362,9 +362,21 @@ namespace Nz
|
||||||
m_mass = mass;
|
m_mass = mass;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigidBody2D::SetMassCenter(const Vector2f& center)
|
void RigidBody2D::SetMassCenter(const Vector2f& center, CoordSys coordSys)
|
||||||
{
|
{
|
||||||
cpBodySetCenterOfGravity(m_handle, cpv(center.x, center.y));
|
cpVect massCenter = cpv(center.x, center.y);
|
||||||
|
|
||||||
|
switch (coordSys)
|
||||||
|
{
|
||||||
|
case CoordSys_Global:
|
||||||
|
massCenter = cpBodyWorldToLocal(m_handle, massCenter);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CoordSys_Local:
|
||||||
|
break; // Nothing to do
|
||||||
|
}
|
||||||
|
|
||||||
|
cpBodySetCenterOfGravity(m_handle, massCenter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigidBody2D::SetMomentOfInertia(float moment)
|
void RigidBody2D::SetMomentOfInertia(float moment)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue