ChipmunkPhysics2D/RigidBody2D: Add ToLocal/ToWorld methods
This commit is contained in:
parent
1ef61cc5ad
commit
65a1c195ac
|
|
@ -98,6 +98,11 @@ namespace Nz
|
||||||
|
|
||||||
void TeleportTo(const Vector2f& position, const RadianAnglef& rotation);
|
void TeleportTo(const Vector2f& position, const RadianAnglef& rotation);
|
||||||
|
|
||||||
|
RadianAnglef ToLocal(const RadianAnglef& worldRotation);
|
||||||
|
Vector2f ToLocal(const Vector2f& worldPosition);
|
||||||
|
RadianAnglef ToWorld(const RadianAnglef& localRotation);
|
||||||
|
Vector2f ToWorld(const Vector2f& localPosition);
|
||||||
|
|
||||||
void UpdateVelocity(const Vector2f& gravity, float damping, float deltaTime);
|
void UpdateVelocity(const Vector2f& gravity, float damping, float deltaTime);
|
||||||
|
|
||||||
void Wakeup();
|
void Wakeup();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - ChipmunkPhysics2D module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_CHIPMUNKPHYSICS2D_CHIPMUNKHELPER_HPP
|
||||||
|
#define NAZARA_CHIPMUNKPHYSICS2D_CHIPMUNKHELPER_HPP
|
||||||
|
|
||||||
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Math/Vector2.hpp>
|
||||||
|
#include <chipmunk/cpVect.h>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline Vector2f FromChipmunk(const cpVect& vect);
|
||||||
|
inline cpVect ToChipmunk(const Vector2f& vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/ChipmunkPhysics2D/ChipmunkHelper.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_CHIPMUNKPHYSICS2D_CHIPMUNKHELPER_HPP
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - ChipmunkPhysics2D module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/ChipmunkPhysics2D/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline Vector2f FromChipmunk(const cpVect& vect)
|
||||||
|
{
|
||||||
|
return { float(vect.x), float(vect.y) };
|
||||||
|
}
|
||||||
|
|
||||||
|
inline cpVect ToChipmunk(const Vector2f& vec)
|
||||||
|
{
|
||||||
|
return cpv(cpFloat(vec.x), cpFloat(vec.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/ChipmunkPhysics2D/DebugOff.hpp>
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <Nazara/ChipmunkPhysics2D/ChipmunkRigidBody2D.hpp>
|
#include <Nazara/ChipmunkPhysics2D/ChipmunkRigidBody2D.hpp>
|
||||||
#include <Nazara/ChipmunkPhysics2D/ChipmunkArbiter2D.hpp>
|
#include <Nazara/ChipmunkPhysics2D/ChipmunkArbiter2D.hpp>
|
||||||
|
#include <Nazara/ChipmunkPhysics2D/ChipmunkHelper.hpp>
|
||||||
#include <Nazara/ChipmunkPhysics2D/ChipmunkPhysWorld2D.hpp>
|
#include <Nazara/ChipmunkPhysics2D/ChipmunkPhysWorld2D.hpp>
|
||||||
#include <chipmunk/chipmunk.h>
|
#include <chipmunk/chipmunk.h>
|
||||||
#include <chipmunk/chipmunk_private.h>
|
#include <chipmunk/chipmunk_private.h>
|
||||||
|
|
@ -73,11 +74,11 @@ namespace Nz
|
||||||
switch (coordSys)
|
switch (coordSys)
|
||||||
{
|
{
|
||||||
case CoordSys::Global:
|
case CoordSys::Global:
|
||||||
cpBodyApplyForceAtWorldPoint(m_handle, cpv(force.x, force.y), cpv(point.x, point.y));
|
cpBodyApplyForceAtWorldPoint(m_handle, ToChipmunk(force), ToChipmunk(point));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CoordSys::Local:
|
case CoordSys::Local:
|
||||||
cpBodyApplyForceAtLocalPoint(m_handle, cpv(force.x, force.y), cpv(point.x, point.y));
|
cpBodyApplyForceAtLocalPoint(m_handle, ToChipmunk(force), ToChipmunk(point));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -87,11 +88,11 @@ namespace Nz
|
||||||
switch (coordSys)
|
switch (coordSys)
|
||||||
{
|
{
|
||||||
case CoordSys::Global:
|
case CoordSys::Global:
|
||||||
cpBodyApplyImpulseAtWorldPoint(m_handle, cpv(impulse.x, impulse.y), cpv(point.x, point.y));
|
cpBodyApplyImpulseAtWorldPoint(m_handle, ToChipmunk(impulse), ToChipmunk(point));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CoordSys::Local:
|
case CoordSys::Local:
|
||||||
cpBodyApplyImpulseAtLocalPoint(m_handle, cpv(impulse.x, impulse.y), cpv(point.x, point.y));
|
cpBodyApplyImpulseAtLocalPoint(m_handle, ToChipmunk(impulse), ToChipmunk(point));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -365,7 +366,7 @@ namespace Nz
|
||||||
|
|
||||||
void ChipmunkRigidBody2D::SetMassCenter(const Vector2f& center, CoordSys coordSys)
|
void ChipmunkRigidBody2D::SetMassCenter(const Vector2f& center, CoordSys coordSys)
|
||||||
{
|
{
|
||||||
cpVect massCenter = cpv(center.x, center.y);
|
cpVect massCenter = ToChipmunk(center);
|
||||||
|
|
||||||
switch (coordSys)
|
switch (coordSys)
|
||||||
{
|
{
|
||||||
|
|
@ -392,7 +393,7 @@ namespace Nz
|
||||||
void ChipmunkRigidBody2D::SetPosition(const Vector2f& position)
|
void ChipmunkRigidBody2D::SetPosition(const Vector2f& position)
|
||||||
{
|
{
|
||||||
// Use cpTransformVect to rotate/scale the position offset
|
// Use cpTransformVect to rotate/scale the position offset
|
||||||
cpBodySetPosition(m_handle, cpvadd(cpv(position.x, position.y), cpTransformVect(m_handle->transform, cpv(m_positionOffset.x, m_positionOffset.y))));
|
cpBodySetPosition(m_handle, cpvadd(ToChipmunk(position), cpTransformVect(m_handle->transform, ToChipmunk(m_positionOffset))));
|
||||||
if (m_isStatic)
|
if (m_isStatic)
|
||||||
{
|
{
|
||||||
m_world->DeferBodyAction(*this, [](ChipmunkRigidBody2D* body)
|
m_world->DeferBodyAction(*this, [](ChipmunkRigidBody2D* body)
|
||||||
|
|
@ -453,7 +454,7 @@ namespace Nz
|
||||||
|
|
||||||
void ChipmunkRigidBody2D::SetVelocity(const Vector2f& velocity)
|
void ChipmunkRigidBody2D::SetVelocity(const Vector2f& velocity)
|
||||||
{
|
{
|
||||||
cpBodySetVelocity(m_handle, cpv(velocity.x, velocity.y));
|
cpBodySetVelocity(m_handle, ToChipmunk(velocity));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChipmunkRigidBody2D::SetVelocityFunction(VelocityFunc velocityFunc)
|
void ChipmunkRigidBody2D::SetVelocityFunction(VelocityFunc velocityFunc)
|
||||||
|
|
@ -478,7 +479,7 @@ namespace Nz
|
||||||
void ChipmunkRigidBody2D::TeleportTo(const Vector2f& position, const RadianAnglef& rotation)
|
void ChipmunkRigidBody2D::TeleportTo(const Vector2f& position, const RadianAnglef& rotation)
|
||||||
{
|
{
|
||||||
// Use cpTransformVect to rotate/scale the position offset
|
// Use cpTransformVect to rotate/scale the position offset
|
||||||
cpBodySetPosition(m_handle, cpvadd(cpv(position.x, position.y), cpTransformVect(m_handle->transform, cpv(m_positionOffset.x, m_positionOffset.y))));
|
cpBodySetPosition(m_handle, cpvadd(ToChipmunk(position), cpTransformVect(m_handle->transform, ToChipmunk(m_positionOffset))));
|
||||||
cpBodySetAngle(m_handle, rotation.value);
|
cpBodySetAngle(m_handle, rotation.value);
|
||||||
if (m_isStatic)
|
if (m_isStatic)
|
||||||
{
|
{
|
||||||
|
|
@ -489,9 +490,29 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChipmunkRigidBody2D::UpdateVelocity(const Vector2f & gravity, float damping, float deltaTime)
|
RadianAnglef ChipmunkRigidBody2D::ToLocal(const RadianAnglef& worldRotation)
|
||||||
{
|
{
|
||||||
cpBodyUpdateVelocity(m_handle, cpv(gravity.x, gravity.y), damping, deltaTime);
|
return worldRotation - GetRotation();
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2f ChipmunkRigidBody2D::ToLocal(const Vector2f& worldPosition)
|
||||||
|
{
|
||||||
|
return FromChipmunk(cpBodyWorldToLocal(m_handle, ToChipmunk(worldPosition)));
|
||||||
|
}
|
||||||
|
|
||||||
|
RadianAnglef ChipmunkRigidBody2D::ToWorld(const RadianAnglef& localRotation)
|
||||||
|
{
|
||||||
|
return GetRotation() + localRotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2f ChipmunkRigidBody2D::ToWorld(const Vector2f& localPosition)
|
||||||
|
{
|
||||||
|
return FromChipmunk(cpBodyLocalToWorld(m_handle, ToChipmunk(localPosition)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChipmunkRigidBody2D::UpdateVelocity(const Vector2f& gravity, float damping, float deltaTime)
|
||||||
|
{
|
||||||
|
cpBodyUpdateVelocity(m_handle, ToChipmunk(gravity), damping, deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChipmunkRigidBody2D::Wakeup()
|
void ChipmunkRigidBody2D::Wakeup()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue