From 9f3bff409fdf1c5e997f4619d42c95703e4c12bd Mon Sep 17 00:00:00 2001 From: Raphael <64648134+RaphaelIT7@users.noreply.github.com> Date: Fri, 16 Aug 2024 23:47:40 +0200 Subject: [PATCH] Fixed a crash with physics springs on object destruction (#194) --- vphysics_jolt/vjolt_environment.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/vphysics_jolt/vjolt_environment.cpp b/vphysics_jolt/vjolt_environment.cpp index c3617fab..48380b8a 100644 --- a/vphysics_jolt/vjolt_environment.cpp +++ b/vphysics_jolt/vjolt_environment.cpp @@ -507,7 +507,8 @@ JoltPhysicsSpring::~JoltPhysicsSpring() if ( m_pObjectEnd ) m_pObjectEnd->RemoveDestroyedListener( this ); - m_pPhysicsSystem->RemoveConstraint( m_pConstraint ); + if ( m_pConstraint ) + m_pPhysicsSystem->RemoveConstraint( m_pConstraint ); } //------------------------------------------------------------------------------------------------- @@ -526,6 +527,9 @@ void JoltPhysicsSpring::GetEndpoints( Vector *worldPositionStart, Vector *worldP void JoltPhysicsSpring::SetSpringConstant( float flSpringConstant ) { + if ( !m_pConstraint ) + return; + m_pObjectStart->Wake(); m_pObjectEnd->Wake(); @@ -535,6 +539,9 @@ void JoltPhysicsSpring::SetSpringConstant( float flSpringConstant ) void JoltPhysicsSpring::SetSpringDamping( float flSpringDamping ) { + if ( !m_pConstraint ) + return; + m_pObjectStart->Wake(); m_pObjectEnd->Wake(); @@ -544,6 +551,9 @@ void JoltPhysicsSpring::SetSpringDamping( float flSpringDamping ) void JoltPhysicsSpring::SetSpringLength( float flSpringLength ) { + if ( !m_pConstraint ) + return; + m_pObjectStart->Wake(); m_pObjectEnd->Wake(); @@ -572,6 +582,11 @@ void JoltPhysicsSpring::OnJoltPhysicsObjectDestroyed( JoltPhysicsObject *pObject if ( pObject == m_pObjectEnd ) m_pObjectEnd = nullptr; + + // Raphael: As soon as one of the physics objects / bodies get destroyed, + // we need to remove the constraint or else it will crash on the next simulation. + m_pPhysicsSystem->RemoveConstraint( m_pConstraint ); + m_pConstraint = nullptr; } //-------------------------------------------------------------------------------------------------