Fix crash when RigidBody2D is moved while having poststeps

This commit is contained in:
Lynix 2017-03-09 18:20:18 +01:00
parent 4ea43e0b0b
commit cdf7c11ef8
4 changed files with 11 additions and 12 deletions

View File

@ -92,7 +92,6 @@ namespace Ndk
{ {
// Kill rigid body before entity destruction to force contact callbacks to be called while the entity is still valid // Kill rigid body before entity destruction to force contact callbacks to be called while the entity is still valid
m_object.reset(); m_object.reset();
} }
ComponentIndex PhysicsComponent3D::componentIndex; ComponentIndex PhysicsComponent3D::componentIndex;

View File

@ -91,7 +91,7 @@ namespace Nz
private: private:
void InitCallbacks(cpCollisionHandler* handler, const Callback& callbacks); void InitCallbacks(cpCollisionHandler* handler, const Callback& callbacks);
using PostStep = std::function<void()>; using PostStep = std::function<void(Nz::RigidBody2D* body)>;
void OnRigidBodyMoved(RigidBody2D* oldPointer, RigidBody2D* newPointer); void OnRigidBodyMoved(RigidBody2D* oldPointer, RigidBody2D* newPointer);
void OnRigidBodyRelease(RigidBody2D* rigidBody); void OnRigidBodyRelease(RigidBody2D* rigidBody);

View File

@ -169,7 +169,7 @@ namespace Nz
for (const auto& pair : m_rigidPostSteps) for (const auto& pair : m_rigidPostSteps)
{ {
for (const auto& step : pair.second.funcs) for (const auto& step : pair.second.funcs)
step(); step(pair.first);
} }
m_rigidPostSteps.clear(); m_rigidPostSteps.clear();

View File

@ -245,24 +245,24 @@ namespace Nz
{ {
if (mass > 0.f) if (mass > 0.f)
{ {
m_world->RegisterPostStep(this, [this, mass]() m_world->RegisterPostStep(this, [mass](Nz::RigidBody2D* body)
{ {
cpBodySetMass(m_handle, mass); cpBodySetMass(body->GetHandle(), mass);
cpBodySetMoment(m_handle, m_geom->ComputeInertialMatrix(m_mass)); cpBodySetMoment(body->GetHandle(), body->GetGeom()->ComputeInertialMatrix(mass));
}); });
} }
else else
m_world->RegisterPostStep(this, [this]() { cpBodySetType(m_handle, CP_BODY_TYPE_STATIC); } ); m_world->RegisterPostStep(this, [](Nz::RigidBody2D* body) { cpBodySetType(body->GetHandle(), CP_BODY_TYPE_STATIC); } );
} }
else if (mass > 0.f) else if (mass > 0.f)
{ {
m_world->RegisterPostStep(this, [this, mass]() m_world->RegisterPostStep(this, [mass](Nz::RigidBody2D* body)
{ {
if (cpBodyGetType(m_handle) == CP_BODY_TYPE_STATIC) if (cpBodyGetType(body->GetHandle()) == CP_BODY_TYPE_STATIC)
{ {
cpBodySetType(m_handle, CP_BODY_TYPE_DYNAMIC); cpBodySetType(body->GetHandle(), CP_BODY_TYPE_DYNAMIC);
cpBodySetMass(m_handle, mass); cpBodySetMass(body->GetHandle(), mass);
cpBodySetMoment(m_handle, m_geom->ComputeInertialMatrix(m_mass)); cpBodySetMoment(body->GetHandle(), body->GetGeom()->ComputeInertialMatrix(mass));
} }
}); });
} }