Skip to content

Commit

Permalink
Fixed a crash
Browse files Browse the repository at this point in the history
[#] Fixed a crash

Related issues:
fixes misyltoad#137
fixes misyltoad#85
  • Loading branch information
RaphaelIT7 committed Oct 1, 2023
1 parent 50cef39 commit 9e0d546
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions vphysics_jolt/vjolt_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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();

for ( JoltPhysicsConstraint *pConstraint : m_pDeadConstraints )
delete pConstraint;
m_pDeadConstraints.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 ( CPhysCollide *pCollide : m_pDeadObjectCollides )
JoltPhysicsCollision::GetInstance().DestroyCollide( pCollide );
m_pDeadObjectCollides.clear();
for ( CPhysCollide *pCollide : m_pDeadObjectCollides )
JoltPhysicsCollision::GetInstance().DestroyCollide( pCollide );
m_pDeadObjectCollides.clear();
}
}

//-------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion vphysics_jolt/vjolt_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class JoltPhysicsEnvironment final : public IPhysicsEnvironment
private:

void RemoveBodyAndDeleteObject( JoltPhysicsObject* pObject );
void DeleteDeadObjects();
void DeleteDeadObjects(bool delBodies = false);

template <typename T>
void AddPhysicsSaveRestorePointer( uintp oldPtr, T* newPtr );
Expand Down

0 comments on commit 9e0d546

Please sign in to comment.