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

@ -69,18 +69,41 @@ b3BodyState* b3GetBodyState( b3World* world, b3Body* body )
return NULL;
}
void b3RefreshBodyContactIndices( b3World* world, b3Body* body )
{
int encodedBodySimIndex = b3EncodeBodySimIndex( body );
int contactKey = body->headContactKey;
while ( contactKey != B3_NULL_INDEX )
{
int edgeIndex = contactKey & 1;
int contactId = contactKey >> 1;
b3Contact* contact = b3Array_Get( world->contacts, contactId );
contactKey = contact->edges[edgeIndex].nextKey;
if ( edgeIndex == 0 )
{
contact->encodedBodySimA = encodedBodySimIndex;
}
else
{
contact->encodedBodySimB = encodedBodySimIndex;
}
}
}
void b3SyncBodyFlags( b3World* world, b3Body* body )
{
// Never sync transient flags
uint32_t flags = body->flags & ~b3_bodyTransientFlags;
b3BodySim* bodySim = b3GetBodySim( world, body );
bodySim->flags = flags;
// Preserve the fast flag for contact recycling.
bodySim->flags = ( bodySim->flags & b3_isFast ) | ( body->flags & ~b3_bodyTransientFlags );
b3BodyState* bodyState = b3GetBodyState( world, body );
if ( bodyState != NULL )
{
bodyState->flags = flags;
bodyState->flags = body->flags & ~b3_bodyTransientFlags;
}
}
@ -410,6 +433,7 @@ void b3DestroyBody( b3BodyId bodyId )
b3Body* movedBody = b3Array_Get( world->bodies, movedId );
B3_ASSERT( movedBody->localIndex == movedIndex );
movedBody->localIndex = body->localIndex;
b3RefreshBodyContactIndices( world, movedBody );
}
// Remove body state from awake set
@ -1120,17 +1144,11 @@ void b3Body_SetTransform( b3BodyId bodyId, b3Pos position, b3Quat rotation )
b3AABB aabb = b3ComputeFatShapeAABB( shape, transform, speculativeDistance );
shape->aabb = aabb;
if ( b3AABB_Contains( shape->fatAABB, aabb ) == false )
b3AABB* shapeFatAABB = world->fatAABBs.data + shapeId;
if ( b3AABB_Contains( *shapeFatAABB, aabb ) == false )
{
float margin = shape->aabbMargin;
b3AABB fatAABB;
fatAABB.lowerBound.x = aabb.lowerBound.x - margin;
fatAABB.lowerBound.y = aabb.lowerBound.y - margin;
fatAABB.lowerBound.z = aabb.lowerBound.z - margin;
fatAABB.upperBound.x = aabb.upperBound.x + margin;
fatAABB.upperBound.y = aabb.upperBound.y + margin;
fatAABB.upperBound.z = aabb.upperBound.z + margin;
shape->fatAABB = fatAABB;
b3AABB fatAABB = b3AABB_Inflate( aabb, shape->aabbMargin );
*shapeFatAABB = fatAABB;
// The body could be disabled
if ( shape->proxyKey != B3_NULL_INDEX )
@ -1717,7 +1735,7 @@ void b3Body_SetType( b3BodyId bodyId, b3BodyType type )
shapeId = shape->nextShapeId;
b3DestroyShapeProxy( shape, &world->broadPhase );
bool forcePairCreation = true;
b3CreateShapeProxy( shape, &world->broadPhase, type, transform, forcePairCreation );
b3CreateShapeProxy( world, shape, type, transform, forcePairCreation );
}
// Relink all joints
@ -2263,7 +2281,7 @@ void b3Body_Enable( b3BodyId bodyId )
b3Shape* shape = b3Array_Get( world->shapes, shapeId );
shapeId = shape->nextShapeId;
b3CreateShapeProxy( shape, &world->broadPhase, proxyType, transform, forcePairCreation );
b3CreateShapeProxy( world, shape, proxyType, transform, forcePairCreation );
}
if ( setId != b3_staticSet )