Fixed a crash

[#] Fixed a crash

Related issues:
fixes #137
fixes #85
This commit is contained in:
RaphaelIT7 2023-10-02 00:40:34 +02:00
parent 50cef39716
commit 9e0d54616f
2 changed files with 21 additions and 13 deletions

View File

@ -771,6 +771,10 @@ void JoltPhysicsEnvironment::Simulate( float deltaTime )
// doing the simulation...
m_ContactListener.PostSimulationFrame();
// RaphaelIT7: We need to delete all dead objects after m_ContactListener.PostSimulationFrame, or else Jolt freaks out for some reason.
// This also needs to be before pController->OnPreSimulate or else we get some crashes.
DeleteDeadObjects(true);
// Run pre-simulation controllers
for ( IJoltPhysicsController *pController : m_pPhysicsControllers )
pController->OnPreSimulate( deltaTime );
@ -818,8 +822,10 @@ void JoltPhysicsEnvironment::Simulate( float deltaTime )
// If the delete queue is disabled, we only added to it during the simulation
// ie. callbacks etc. So flush that now.
if ( !m_bEnableDeleteQueue )
if ( !m_bEnableDeleteQueue ) {
DeleteDeadObjects();
DeleteDeadObjects(true); // Also delete all bodies
}
#ifdef JPH_DEBUG_RENDERER
JoltPhysicsDebugRenderer::GetInstance().RenderPhysicsSystem( m_PhysicsSystem );
@ -1430,19 +1436,21 @@ void JoltPhysicsEnvironment::RemoveBodyAndDeleteObject( JoltPhysicsObject *pObje
delete pObject;
}
void JoltPhysicsEnvironment::DeleteDeadObjects()
void JoltPhysicsEnvironment::DeleteDeadObjects( bool delBodies )
{
for ( JoltPhysicsObject *pObject : m_pDeadObjects )
RemoveBodyAndDeleteObject( pObject );
m_pDeadObjects.clear();
if (delBodies) {
for ( JoltPhysicsObject *pObject : m_pDeadObjects )
RemoveBodyAndDeleteObject( pObject );
m_pDeadObjects.clear();
} else {
for ( JoltPhysicsConstraint *pConstraint : m_pDeadConstraints )
delete pConstraint;
m_pDeadConstraints.clear();
for ( JoltPhysicsConstraint *pConstraint : m_pDeadConstraints )
delete pConstraint;
m_pDeadConstraints.clear();
for ( CPhysCollide *pCollide : m_pDeadObjectCollides )
JoltPhysicsCollision::GetInstance().DestroyCollide( pCollide );
m_pDeadObjectCollides.clear();
for ( CPhysCollide *pCollide : m_pDeadObjectCollides )
JoltPhysicsCollision::GetInstance().DestroyCollide( pCollide );
m_pDeadObjectCollides.clear();
}
}
//-------------------------------------------------------------------------------------------------

View File

@ -172,7 +172,7 @@ public:
private:
void RemoveBodyAndDeleteObject( JoltPhysicsObject* pObject );
void DeleteDeadObjects();
void DeleteDeadObjects(bool delBodies = false);
template <typename T>
void AddPhysicsSaveRestorePointer( uintp oldPtr, T* newPtr );