Skip to content

Commit

Permalink
Fixed a crash with physics springs on object destruction (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelIT7 authored Aug 16, 2024
1 parent 77c2131 commit 9f3bff4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion vphysics_jolt/vjolt_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

//-------------------------------------------------------------------------------------------------
Expand All @@ -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();

Expand All @@ -535,6 +539,9 @@ void JoltPhysicsSpring::SetSpringConstant( float flSpringConstant )

void JoltPhysicsSpring::SetSpringDamping( float flSpringDamping )
{
if ( !m_pConstraint )
return;

m_pObjectStart->Wake();
m_pObjectEnd->Wake();

Expand All @@ -544,6 +551,9 @@ void JoltPhysicsSpring::SetSpringDamping( float flSpringDamping )

void JoltPhysicsSpring::SetSpringLength( float flSpringLength )
{
if ( !m_pConstraint )
return;

m_pObjectStart->Wake();
m_pObjectEnd->Wake();

Expand Down Expand Up @@ -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;
}

//-------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 9f3bff4

Please sign in to comment.