Physics2D/RigidBody2D: Fix SetGeom attribute copy
This commit is contained in:
parent
377dd992b9
commit
72bcb1fda7
|
|
@ -74,10 +74,11 @@ namespace Nz
|
||||||
static constexpr std::size_t InvalidShapeIndex = std::numeric_limits<std::size_t>::max();
|
static constexpr std::size_t InvalidShapeIndex = std::numeric_limits<std::size_t>::max();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CopyBodyData(cpBody* body);
|
|
||||||
cpBody* Create(float mass = 1.f, float moment = 1.f);
|
cpBody* Create(float mass = 1.f, float moment = 1.f);
|
||||||
void Destroy();
|
void Destroy();
|
||||||
|
|
||||||
|
static void CopyBodyData(cpBody* from, cpBody* to);
|
||||||
|
|
||||||
std::vector<cpShape*> m_shapes;
|
std::vector<cpShape*> m_shapes;
|
||||||
Collider2DRef m_geom;
|
Collider2DRef m_geom;
|
||||||
cpBody* m_handle;
|
cpBody* m_handle;
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ namespace Nz
|
||||||
m_handle = Create(m_mass, object.GetMomentOfInertia());
|
m_handle = Create(m_mass, object.GetMomentOfInertia());
|
||||||
SetGeom(object.GetGeom(), false);
|
SetGeom(object.GetGeom(), false);
|
||||||
|
|
||||||
CopyBodyData(object.GetHandle());
|
CopyBodyData(object.GetHandle(), m_handle);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < m_shapes.size(); ++i)
|
for (std::size_t i = 0; i < m_shapes.size(); ++i)
|
||||||
m_shapes[i]->bb = cpShapeCacheBB(object.m_shapes[i]);
|
m_shapes[i]->bb = cpShapeCacheBB(object.m_shapes[i]);
|
||||||
|
|
@ -243,7 +243,7 @@ namespace Nz
|
||||||
|
|
||||||
cpBody* newHandle = Create(static_cast<float>(mass), static_cast<float>(moment));
|
cpBody* newHandle = Create(static_cast<float>(mass), static_cast<float>(moment));
|
||||||
|
|
||||||
CopyBodyData(m_handle);
|
CopyBodyData(m_handle, newHandle);
|
||||||
Destroy();
|
Destroy();
|
||||||
|
|
||||||
m_handle = newHandle;
|
m_handle = newHandle;
|
||||||
|
|
@ -403,25 +403,20 @@ namespace Nz
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RigidBody2D::CopyBodyData(cpBody* body)
|
|
||||||
{
|
|
||||||
cpBodySetAngle(m_handle, cpBodyGetAngle(body));
|
|
||||||
cpBodySetAngularVelocity(m_handle, cpBodyGetAngularVelocity(body));
|
|
||||||
cpBodySetCenterOfGravity(m_handle, cpBodyGetCenterOfGravity(body));
|
|
||||||
cpBodySetForce(m_handle, cpBodyGetForce(body));
|
|
||||||
cpBodySetPosition(m_handle, cpBodyGetPosition(body));
|
|
||||||
cpBodySetTorque(m_handle, cpBodyGetTorque(body));
|
|
||||||
cpBodySetVelocity(m_handle, cpBodyGetVelocity(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
cpBody* RigidBody2D::Create(float mass, float moment)
|
cpBody* RigidBody2D::Create(float mass, float moment)
|
||||||
{
|
{
|
||||||
cpBody* handle = cpBodyNew(mass, moment);
|
cpBody* handle;
|
||||||
|
if (IsKinematic())
|
||||||
|
{
|
||||||
|
if (IsStatic())
|
||||||
|
handle = cpBodyNewStatic();
|
||||||
|
else
|
||||||
|
handle = cpBodyNewKinematic();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
handle = cpBodyNew(mass, moment);
|
||||||
|
|
||||||
cpBodySetUserData(handle, this);
|
cpBodySetUserData(handle, this);
|
||||||
|
|
||||||
if (mass <= 0.f)
|
|
||||||
cpBodySetType(handle, CP_BODY_TYPE_KINEMATIC);
|
|
||||||
|
|
||||||
cpSpaceAddBody(m_world->GetHandle(), handle);
|
cpSpaceAddBody(m_world->GetHandle(), handle);
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
|
|
@ -443,4 +438,16 @@ namespace Nz
|
||||||
}
|
}
|
||||||
m_shapes.clear();
|
m_shapes.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RigidBody2D::CopyBodyData(cpBody* from, cpBody* to)
|
||||||
|
{
|
||||||
|
cpBodySetAngle(to, cpBodyGetAngle(from));
|
||||||
|
cpBodySetAngularVelocity(to, cpBodyGetAngularVelocity(from));
|
||||||
|
cpBodySetCenterOfGravity(to, cpBodyGetCenterOfGravity(from));
|
||||||
|
cpBodySetForce(to, cpBodyGetForce(from));
|
||||||
|
cpBodySetPosition(to, cpBodyGetPosition(from));
|
||||||
|
cpBodySetTorque(to, cpBodyGetTorque(from));
|
||||||
|
cpBodySetVelocity(to, cpBodyGetVelocity(from));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue