From e6f26f04a9f886da1531f75e61d4f954e2487685 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Fri, 4 Aug 2023 12:27:26 +0200 Subject: [PATCH] JoltPhysics3D: Fix destruction of just created bodies --- src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp b/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp index 4e87100d5..3a1a15293 100644 --- a/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp +++ b/src/Nazara/JoltPhysics3D/JoltPhysWorld3D.cpp @@ -496,6 +496,13 @@ namespace Nz void JoltPhysWorld3D::UnregisterBody(const JPH::BodyID& bodyID, bool destroy, bool removeFromActivationList) { + // Remove from list first as bodyID may be invalidated by destruction + if (removeFromActivationList) + { + m_world->pendingAdditionActivate.erase(bodyID); + m_world->pendingAdditionNoActivate.erase(bodyID); + } + if (destroy) { auto& bodyInterface = m_world->physicsSystem.GetBodyInterface(); @@ -511,17 +518,10 @@ namespace Nz bodyInterface.RemoveBody(bodyID); } - bodyInterface.DestroyBody(bodyID); - + bodyInterface.DestroyBody(bodyID); //< this invalidate the bodyID reference! } else m_world->pendingDeactivations.insert(bodyID); - - if (removeFromActivationList) - { - m_world->pendingAdditionActivate.erase(bodyID); - m_world->pendingAdditionNoActivate.erase(bodyID); - } } std::shared_ptr JoltPhysWorld3D::GetDefaultCharacterImpl()