From cdf7c11ef854d5c004e8830db04836f2d6c28c22 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 9 Mar 2017 18:20:18 +0100 Subject: [PATCH] Fix crash when RigidBody2D is moved while having poststeps --- SDK/src/NDK/Components/PhysicsComponent3D.cpp | 1 - include/Nazara/Physics2D/PhysWorld2D.hpp | 2 +- src/Nazara/Physics2D/PhysWorld2D.cpp | 2 +- src/Nazara/Physics2D/RigidBody2D.cpp | 18 +++++++++--------- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/SDK/src/NDK/Components/PhysicsComponent3D.cpp b/SDK/src/NDK/Components/PhysicsComponent3D.cpp index e0a22d08f..31b3fb6ac 100644 --- a/SDK/src/NDK/Components/PhysicsComponent3D.cpp +++ b/SDK/src/NDK/Components/PhysicsComponent3D.cpp @@ -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 m_object.reset(); - } ComponentIndex PhysicsComponent3D::componentIndex; diff --git a/include/Nazara/Physics2D/PhysWorld2D.hpp b/include/Nazara/Physics2D/PhysWorld2D.hpp index c3858dc1a..7b31d9743 100644 --- a/include/Nazara/Physics2D/PhysWorld2D.hpp +++ b/include/Nazara/Physics2D/PhysWorld2D.hpp @@ -91,7 +91,7 @@ namespace Nz private: void InitCallbacks(cpCollisionHandler* handler, const Callback& callbacks); - using PostStep = std::function; + using PostStep = std::function; void OnRigidBodyMoved(RigidBody2D* oldPointer, RigidBody2D* newPointer); void OnRigidBodyRelease(RigidBody2D* rigidBody); diff --git a/src/Nazara/Physics2D/PhysWorld2D.cpp b/src/Nazara/Physics2D/PhysWorld2D.cpp index f3dcba77d..f77c509ed 100644 --- a/src/Nazara/Physics2D/PhysWorld2D.cpp +++ b/src/Nazara/Physics2D/PhysWorld2D.cpp @@ -169,7 +169,7 @@ namespace Nz for (const auto& pair : m_rigidPostSteps) { for (const auto& step : pair.second.funcs) - step(); + step(pair.first); } m_rigidPostSteps.clear(); diff --git a/src/Nazara/Physics2D/RigidBody2D.cpp b/src/Nazara/Physics2D/RigidBody2D.cpp index 8bf713402..f31e9ef54 100644 --- a/src/Nazara/Physics2D/RigidBody2D.cpp +++ b/src/Nazara/Physics2D/RigidBody2D.cpp @@ -245,24 +245,24 @@ namespace Nz { if (mass > 0.f) { - m_world->RegisterPostStep(this, [this, mass]() + m_world->RegisterPostStep(this, [mass](Nz::RigidBody2D* body) { - cpBodySetMass(m_handle, mass); - cpBodySetMoment(m_handle, m_geom->ComputeInertialMatrix(m_mass)); + cpBodySetMass(body->GetHandle(), mass); + cpBodySetMoment(body->GetHandle(), body->GetGeom()->ComputeInertialMatrix(mass)); }); } 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) { - 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); - cpBodySetMass(m_handle, mass); - cpBodySetMoment(m_handle, m_geom->ComputeInertialMatrix(m_mass)); + cpBodySetType(body->GetHandle(), CP_BODY_TYPE_DYNAMIC); + cpBodySetMass(body->GetHandle(), mass); + cpBodySetMoment(body->GetHandle(), body->GetGeom()->ComputeInertialMatrix(mass)); } }); }