Optimizations (#161)

- cache body sim indices and split out fat aabb
- deferred restitution
- the manifold point normal velocity is only computed for restitution or
hit events, otherwise it is zero
This commit is contained in:
Erin Catto 2026-09-20 15:54:52 -07:00 • committed by GitHub
commit 9e5a4cde86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
49 changed files with 3395 additions and 864 deletions

View file

@ -62,7 +62,8 @@ void b3WakeSolverSet( b3World* world, int setIndex )
*state = b3_identityBodyState;
state->flags = body->flags;
// move non-touching contacts from disabled set to awake set
// Move non-touching contacts from disabled set to awake set.
int encodedBodySimIndex = body->localIndex;
int contactKey = body->headContactKey;
while ( contactKey != B3_NULL_INDEX )
{
@ -75,6 +76,21 @@ void b3WakeSolverSet( b3World* world, int setIndex )
if ( contact->setIndex != b3_disabledSet )
{
B3_ASSERT( contact->setIndex == b3_awakeSet || contact->setIndex == setIndex );
// If this contact is awake but not touching I need to fixup the body sim index here.
if ( contact->setIndex == b3_awakeSet )
{
B3_ASSERT( contact->colorIndex == B3_NULL_INDEX );
if ( edgeIndex == 0 )
{
contact->encodedBodySimA = encodedBodySimIndex;
}
else
{
contact->encodedBodySimB = encodedBodySimIndex;
}
}
continue;
}
@ -89,6 +105,15 @@ void b3WakeSolverSet( b3World* world, int setIndex )
contact->localIndex = awakeSet->contactIndices.count;
b3Array_Push( awakeSet->contactIndices, contactId );
if ( edgeIndex == 0 )
{
contact->encodedBodySimA = encodedBodySimIndex;
}
else
{
contact->encodedBodySimB = encodedBodySimIndex;
}
int movedLocalIndex = b3Array_RemoveSwap( disabledSet->contactIndices, localIndex );
if ( movedLocalIndex != B3_NULL_INDEX )
{
@ -227,6 +252,11 @@ void b3TrySleepIsland( b3World* world, int islandId )
b3Body* movedBody = b3Array_Get( world->bodies, movedId );
B3_ASSERT( movedBody->localIndex == movedIndex );
movedBody->localIndex = awakeBodyIndex;
if ( movedBody->islandId != islandId )
{
b3RefreshBodyContactIndices( world, movedBody );
}
}
// destroy state, no need to clone
@ -245,14 +275,9 @@ void b3TrySleepIsland( b3World* world, int islandId )
b3Contact* contact = b3Array_Get( world->contacts, contactId );
B3_ASSERT( contact->setIndex == b3_awakeSet || contact->setIndex == b3_disabledSet );
contactKey = contact->edges[edgeIndex].nextKey;
B3_ASSERT( contact->setIndex == b3_awakeSet );
if ( contact->setIndex == b3_disabledSet )
{
// already moved to disabled set by another body in the island
continue;
}
contactKey = contact->edges[edgeIndex].nextKey;
if ( contact->colorIndex != B3_NULL_INDEX )
{
@ -261,6 +286,15 @@ void b3TrySleepIsland( b3World* world, int islandId )
continue;
}
if ( edgeIndex == 0 )
{
contact->encodedBodySimA = B3_NULL_INDEX;
}
else
{
contact->encodedBodySimB = B3_NULL_INDEX;
}
// the other body may still be awake, it still may go to sleep and then it will be responsible
// for moving this contact to the disabled set.
int otherEdgeIndex = edgeIndex ^ 1;
@ -352,6 +386,8 @@ void b3TrySleepIsland( b3World* world, int islandId )
contact->setIndex = sleepSetId;
contact->colorIndex = B3_NULL_INDEX;
contact->localIndex = sleepContactIndex;
contact->encodedBodySimA = b3SleepBodySimIndex( contact->encodedBodySimA );
contact->encodedBodySimB = b3SleepBodySimIndex( contact->encodedBodySimB );
}
}
@ -530,6 +566,8 @@ void b3MergeSolverSets( b3World* world, int setId1, int setId2 )
void b3TransferBody( b3World* world, b3SolverSet* targetSet, b3SolverSet* sourceSet, b3Body* body )
{
B3_ASSERT( body->headContactKey == B3_NULL_INDEX );
if ( targetSet == sourceSet )
{
return;
@ -543,7 +581,8 @@ void b3TransferBody( b3World* world, b3SolverSet* targetSet, b3SolverSet* source
memcpy( targetSim, sourceSim, sizeof( b3BodySim ) );
// Clear transient body flags
targetSim->flags &= ~( b3_isFast | b3_isSpeedCapped | b3_hadTimeOfImpact );
body->flags &= ~b3_bodyTransientFlags;
targetSim->flags &= ~(b3_isFast | b3_bodyTransientFlags);
// Remove body sim from solver set that owns it
int movedIndex = b3Array_RemoveSwap( sourceSet->bodySims, sourceIndex );
@ -555,6 +594,7 @@ void b3TransferBody( b3World* world, b3SolverSet* targetSet, b3SolverSet* source
b3Body* movedBody = b3Array_Get( world->bodies, movedId );
B3_ASSERT( movedBody->localIndex == movedIndex );
movedBody->localIndex = sourceIndex;
b3RefreshBodyContactIndices( world, movedBody );
}
if ( sourceSet->setIndex == b3_awakeSet )