Physics2D/RigidBody2D: Made SetGeom public
This commit is contained in:
parent
7f445def13
commit
9772b43542
|
|
@ -49,6 +49,7 @@ namespace Nz
|
||||||
bool IsSleeping() const;
|
bool IsSleeping() const;
|
||||||
|
|
||||||
void SetAngularVelocity(float angularVelocity);
|
void SetAngularVelocity(float angularVelocity);
|
||||||
|
void SetGeom(Collider2DRef geom);
|
||||||
void SetMass(float mass);
|
void SetMass(float mass);
|
||||||
void SetMassCenter(const Vector2f& center);
|
void SetMassCenter(const Vector2f& center);
|
||||||
void SetPosition(const Vector2f& position);
|
void SetPosition(const Vector2f& position);
|
||||||
|
|
@ -59,8 +60,8 @@ namespace Nz
|
||||||
RigidBody2D& operator=(RigidBody2D&& object);
|
RigidBody2D& operator=(RigidBody2D&& object);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void Create(float mass = 0.f, float moment = 0.f);
|
||||||
void Destroy();
|
void Destroy();
|
||||||
void SetGeom(Collider2DRef geom);
|
|
||||||
|
|
||||||
std::vector<cpShape*> m_shapes;
|
std::vector<cpShape*> m_shapes;
|
||||||
Collider2DRef m_geom;
|
Collider2DRef m_geom;
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,7 @@ namespace Nz
|
||||||
{
|
{
|
||||||
NazaraAssert(m_world, "Invalid world");
|
NazaraAssert(m_world, "Invalid world");
|
||||||
|
|
||||||
m_handle = cpBodyNew(0.f, 0.f);
|
Create();
|
||||||
cpBodySetUserData(m_handle, this);
|
|
||||||
cpSpaceAddBody(m_world->GetHandle(), m_handle);
|
|
||||||
|
|
||||||
SetGeom(geom);
|
SetGeom(geom);
|
||||||
SetMass(mass);
|
SetMass(mass);
|
||||||
|
|
@ -43,9 +41,7 @@ namespace Nz
|
||||||
NazaraAssert(m_world, "Invalid world");
|
NazaraAssert(m_world, "Invalid world");
|
||||||
NazaraAssert(m_geom, "Invalid geometry");
|
NazaraAssert(m_geom, "Invalid geometry");
|
||||||
|
|
||||||
m_handle = cpBodyNew(0.f, 0.f);
|
Create();
|
||||||
cpBodySetUserData(m_handle, this);
|
|
||||||
cpSpaceAddBody(m_world->GetHandle(), m_handle);
|
|
||||||
|
|
||||||
SetGeom(object.GetGeom());
|
SetGeom(object.GetGeom());
|
||||||
SetMass(object.GetMass());
|
SetMass(object.GetMass());
|
||||||
|
|
@ -169,6 +165,34 @@ namespace Nz
|
||||||
cpBodySetAngularVelocity(m_handle, angularVelocity);
|
cpBodySetAngularVelocity(m_handle, angularVelocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RigidBody2D::SetGeom(Collider2DRef geom)
|
||||||
|
{
|
||||||
|
// We have no public way of getting rid of an existing geom without removing the whole body
|
||||||
|
// So let's save some attributes of the body, destroy it and rebuild it
|
||||||
|
if (m_geom)
|
||||||
|
{
|
||||||
|
cpVect pos = cpBodyGetPosition(m_handle);
|
||||||
|
cpFloat mass = cpBodyGetMass(m_handle);
|
||||||
|
cpFloat moment = cpBodyGetMoment(m_handle);
|
||||||
|
cpFloat rot = cpBodyGetAngle(m_handle);
|
||||||
|
cpVect vel = cpBodyGetVelocity(m_handle);
|
||||||
|
|
||||||
|
Destroy();
|
||||||
|
Create(mass, moment);
|
||||||
|
|
||||||
|
cpBodySetAngle(m_handle, rot);
|
||||||
|
cpBodySetPosition(m_handle, pos);
|
||||||
|
cpBodySetVelocity(m_handle, vel);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (geom)
|
||||||
|
m_geom = geom;
|
||||||
|
else
|
||||||
|
m_geom = NullCollider2D::New();
|
||||||
|
|
||||||
|
m_shapes = m_geom->CreateShapes(this);
|
||||||
|
}
|
||||||
|
|
||||||
void RigidBody2D::SetMass(float mass)
|
void RigidBody2D::SetMass(float mass)
|
||||||
{
|
{
|
||||||
if (m_mass > 0.f)
|
if (m_mass > 0.f)
|
||||||
|
|
@ -230,6 +254,13 @@ namespace Nz
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RigidBody2D::Create(float mass, float moment)
|
||||||
|
{
|
||||||
|
m_handle = cpBodyNew(mass, moment);
|
||||||
|
cpBodySetUserData(m_handle, this);
|
||||||
|
cpSpaceAddBody(m_world->GetHandle(), m_handle);
|
||||||
|
}
|
||||||
|
|
||||||
void RigidBody2D::Destroy()
|
void RigidBody2D::Destroy()
|
||||||
{
|
{
|
||||||
for (cpShape* shape : m_shapes)
|
for (cpShape* shape : m_shapes)
|
||||||
|
|
@ -238,14 +269,4 @@ namespace Nz
|
||||||
if (m_handle)
|
if (m_handle)
|
||||||
cpBodyFree(m_handle);
|
cpBodyFree(m_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigidBody2D::SetGeom(Collider2DRef geom)
|
|
||||||
{
|
|
||||||
if (geom)
|
|
||||||
m_geom = geom;
|
|
||||||
else
|
|
||||||
m_geom = NullCollider2D::New();
|
|
||||||
|
|
||||||
m_shapes = m_geom->CreateShapes(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue