More static queries (#216)

- and a data race fix for it
- compare mesh versus heightfield
This commit is contained in:
Erin Catto 2026-11-11 17:41:42 -08:00 • committed by GitHub
commit a8ac09f364
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 486 additions and 183 deletions

View file

@ -264,7 +264,7 @@ b3WorldId b3CreateWorld( const b3WorldDef* def )
int bodyCapacity = b3MaxInt( 16, def->capacity.staticBodyCount + def->capacity.dynamicBodyCount );
b3Array_Reserve( world->bodies, bodyCapacity );
b3Array_Reserve( world->solverSets, 8 );
b3Array_Reserve( world->sleepingSets, 8 );
// add empty static, active, and disabled body sets
world->solverSetIdPool = b3CreateIdPool();
@ -272,22 +272,22 @@ b3WorldId b3CreateWorld( const b3WorldDef* def )
// static set
set.setIndex = b3AllocId( &world->solverSetIdPool );
b3Array_Push( world->solverSets, set );
b3Array_Reserve( world->solverSets.data[b3_staticSet].bodySims, b3MaxInt( 16, def->capacity.staticBodyCount ) );
B3_ASSERT( world->solverSets.data[b3_staticSet].setIndex == b3_staticSet );
world->fixedSets[set.setIndex] = set;
b3Array_Reserve( b3GetSolverSet( world, b3_staticSet )->bodySims, b3MaxInt( 16, def->capacity.staticBodyCount ) );
B3_ASSERT( b3GetSolverSet( world, b3_staticSet )->setIndex == b3_staticSet );
// disabled set
set.setIndex = b3AllocId( &world->solverSetIdPool );
b3Array_Push( world->solverSets, set );
B3_ASSERT( world->solverSets.data[b3_disabledSet].setIndex == b3_disabledSet );
world->fixedSets[set.setIndex] = set;
B3_ASSERT( b3GetSolverSet( world, b3_disabledSet )->setIndex == b3_disabledSet );
// awake set
set.setIndex = b3AllocId( &world->solverSetIdPool );
b3Array_Push( world->solverSets, set );
b3Array_Reserve( world->solverSets.data[b3_awakeSet].bodySims, b3MaxInt( 16, def->capacity.dynamicBodyCount ) );
b3Array_Reserve( world->solverSets.data[b3_awakeSet].bodyStates, b3MaxInt( 16, def->capacity.dynamicBodyCount ) );
b3Array_Reserve( world->solverSets.data[b3_awakeSet].contactIndices, b3MaxInt( 16, def->capacity.contactCount ) );
B3_ASSERT( world->solverSets.data[b3_awakeSet].setIndex == b3_awakeSet );
world->fixedSets[set.setIndex] = set;
b3Array_Reserve( b3GetSolverSet( world, b3_awakeSet )->bodySims, b3MaxInt( 16, def->capacity.dynamicBodyCount ) );
b3Array_Reserve( b3GetSolverSet( world, b3_awakeSet )->bodyStates, b3MaxInt( 16, def->capacity.dynamicBodyCount ) );
b3Array_Reserve( b3GetSolverSet( world, b3_awakeSet )->contactIndices, b3MaxInt( 16, def->capacity.contactCount ) );
B3_ASSERT( b3GetSolverSet( world, b3_awakeSet )->setIndex == b3_awakeSet );
world->shapeIdPool = b3CreateIdPool();
@ -502,17 +502,17 @@ void b3DestroyWorld( b3WorldId worldId )
b3Array_Destroy( world->islands );
// Destroy solver sets
int setCapacity = world->solverSets.count;
int setCapacity = b3GetSolverSetCount( world );
for ( int i = 0; i < setCapacity; ++i )
{
b3SolverSet* set = world->solverSets.data + i;
b3SolverSet* set = b3GetSolverSet( world, i );
if ( set->setIndex != B3_NULL_INDEX )
{
b3DestroySolverSet( world, i );
}
}
b3Array_Destroy( world->solverSets );
b3Array_Destroy( world->sleepingSets );
b3DestroyGraph( &world->constraintGraph );
b3DestroyBroadPhase( &world->broadPhase );
@ -577,7 +577,7 @@ static inline b3BodySim* b3ResolveContactBodySim( b3World* world, b3BodySim* awa
}
b3Body* body = b3Array_Get( world->bodies, bodyId );
b3SolverSet* set = b3Array_Get( world->solverSets, body->setIndex );
b3SolverSet* set = b3GetSolverSet( world, body->setIndex );
return b3Array_Get( set->bodySims, body->localIndex );
}
@ -593,8 +593,8 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
b3Contact* contacts = world->contacts.data;
b3Shape* shapes = world->shapes.data;
const b3AABB* fatAABBs = world->fatAABBs.data;
b3BodySim* awakeSims = world->solverSets.data[b3_awakeSet].bodySims.data;
b3BodySim* staticSims = world->solverSets.data[b3_staticSet].bodySims.data;
b3BodySim* awakeSims = b3GetSolverSet( world, b3_awakeSet )->bodySims.data;
b3BodySim* staticSims = b3GetSolverSet( world, b3_staticSet )->bodySims.data;
B3_ASSERT( startIndex < endIndex );
@ -799,7 +799,7 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
static void b3AddNonTouchingContact( b3World* world, b3Contact* contact )
{
B3_ASSERT( contact->setIndex == b3_awakeSet );
b3SolverSet* set = b3Array_Get( world->solverSets, b3_awakeSet );
b3SolverSet* set = b3GetSolverSet( world, b3_awakeSet );
contact->colorIndex = B3_NULL_INDEX;
contact->localIndex = set->contactIndices.count;
b3Array_Push( set->contactIndices, contact->contactId );
@ -807,7 +807,7 @@ static void b3AddNonTouchingContact( b3World* world, b3Contact* contact )
static void b3RemoveNonTouchingContact( b3World* world, int setIndex, int localIndex )
{
b3SolverSet* set = b3Array_Get( world->solverSets, setIndex );
b3SolverSet* set = b3GetSolverSet( world, setIndex );
int movedIndex = b3Array_RemoveSwap( set->contactIndices, localIndex );
if ( movedIndex != B3_NULL_INDEX )
{
@ -838,7 +838,7 @@ static void b3Collide( b3StepContext* context )
touchingCount += graphColors[i].convexContacts.count + graphColors[i].contacts.count;
}
b3SolverSet* awakeSet = b3Array_Get( world->solverSets, b3_awakeSet );
b3SolverSet* awakeSet = b3GetSolverSet( world, b3_awakeSet );
int nonTouchingCount = awakeSet->contactIndices.count;
int contactCount = touchingCount + nonTouchingCount;
@ -1083,7 +1083,7 @@ void b3World_Step( b3WorldId worldId, float timeStep, int subStepCount )
c->staticShapeCount = b3MaxInt( c->staticShapeCount, world->broadPhase.trees[b3_staticBody].proxyCount );
c->dynamicShapeCount = b3MaxInt( c->dynamicShapeCount, world->broadPhase.trees[b3_dynamicBody].proxyCount );
int staticBodyCount = world->solverSets.data[b3_staticSet].bodySims.count;
int staticBodyCount = b3GetSolverSet( world, b3_staticSet )->bodySims.count;
c->staticBodyCount = b3MaxInt( c->staticBodyCount, staticBodyCount );
// this includes kinematic bodies
@ -1101,7 +1101,7 @@ void b3World_Step( b3WorldId worldId, float timeStep, int subStepCount )
world->profile.pairs = b3GetMilliseconds( pairTicks );
}
b3SolverSet* awakeSet = b3Array_Get( world->solverSets, b3_awakeSet );
b3SolverSet* awakeSet = b3GetSolverSet( world, b3_awakeSet );
b3StepContext context = { 0 };
context.world = world;
@ -1997,10 +1997,10 @@ void b3World_EnableSleeping( b3WorldId worldId, bool flag )
if ( flag == false )
{
int setCount = world->solverSets.count;
int setCount = b3GetSolverSetCount( world );
for ( int i = b3_firstSleepingSet; i < setCount; ++i )
{
b3SolverSet* set = b3Array_Get( world->solverSets, i );
b3SolverSet* set = b3GetSolverSet( world, i );
if ( set->bodySims.count > 0 )
{
b3WakeSolverSet( world, i );
@ -2052,7 +2052,7 @@ int b3World_GetAwakeBodyCount( b3WorldId worldId )
{
return 0;
}
b3SolverSet* awakeSet = b3Array_Get( world->solverSets, b3_awakeSet );
b3SolverSet* awakeSet = b3GetSolverSet( world, b3_awakeSet );
return awakeSet->bodySims.count;
}
@ -2382,7 +2382,7 @@ static uint64_t b3GetWorldByteCount( b3World* world, bool log )
// world arrays
int bodyArrayBytes = b3Array_ByteCount( world->bodies );
int solverSetArrayBytes = b3Array_ByteCount( world->solverSets );
int solverSetArrayBytes = b3Array_ByteCount( world->sleepingSets );
int jointArrayBytes = b3Array_ByteCount( world->joints );
int contactArrayBytes = b3Array_ByteCount( world->contacts );
int islandArrayBytes = b3Array_ByteCount( world->islands );
@ -2482,10 +2482,10 @@ static uint64_t b3GetWorldByteCount( b3World* world, bool log )
int jointSimCapacity = 0;
int contactIndexCapacity = 0;
int islandSimCapacity = 0;
int solverSetCapacity = world->solverSets.count;
int solverSetCapacity = b3GetSolverSetCount( world );
for ( int i = 0; i < solverSetCapacity; ++i )
{
b3SolverSet* set = world->solverSets.data + i;
b3SolverSet* set = b3GetSolverSet( world, i );
if ( set->setIndex == B3_NULL_INDEX )
{
continue;
@ -2657,7 +2657,7 @@ b3Counters b3World_GetCounters( b3WorldId worldId )
s.colorCounts[i] = colorContactCount + color->jointSims.count;
s.awakeContactCount += colorContactCount;
}
s.awakeContactCount += world->solverSets.data[b3_awakeSet].contactIndices.count;
s.awakeContactCount += b3GetSolverSet( world, b3_awakeSet )->contactIndices.count;
s.recycledContactCount = 0;
s.arenaCapacity = 0;
@ -2865,6 +2865,21 @@ b3TreeStats b3World_OverlapShape( b3WorldId worldId, b3Pos origin, const b3Shape
return treeStats;
}
b3TreeStats b3World_OverlapShape_Static( b3WorldId worldId, b3Pos origin, const b3ShapeProxy* proxy,
b3QueryFilter filter, b3OverlapResultFcn* fcn, void* context )
{
b3World* world = b3GetWorldFromId( worldId );
B3_ASSERT( b3IsValidPosition( origin ) );
b3AABB aabb = b3OffsetAABB( b3MakeAABB( proxy->points, proxy->count, proxy->radius ), origin );
WorldOverlapContext worldContext = {
world, fcn, filter, *proxy, origin, context,
};
return b3DynamicTree_QueryRelaxed( world->broadPhase.trees + b3_staticBody, aabb, filter.maskBits, false,
b3TreeOverlapCallback, &worldContext );
}
typedef struct WorldMoverContext
{
b3World* world;
@ -3080,6 +3095,20 @@ b3TreeStats b3World_CastRay( b3WorldId worldId, b3Pos origin, b3Vec3 translation
return treeStats;
}
b3TreeStats b3World_CastRay_Static( b3WorldId worldId, b3Pos origin, b3Vec3 translation, b3QueryFilter filter,
b3CastResultFcn* fcn, void* context )
{
b3World* world = b3GetWorldFromId( worldId );
B3_ASSERT( b3IsValidPosition( origin ) );
B3_ASSERT( b3IsValidVec3( translation ) );
b3RayCastInput input = { b3ToVec3( origin ), translation, 1.0f };
WorldRayCastContext worldContext = { world, fcn, filter, 1.0f, origin, context };
return b3DynamicTree_RayCastRelaxed( world->broadPhase.trees + b3_staticBody, &input, filter.maskBits, false,
RayCastCallback, &worldContext );
}
// This callback finds the closest hit. This is the most common callback used in games.
static float b3RayCastClosestFcn( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
int triangleIndex, int childIndex, void* context )
@ -3293,6 +3322,32 @@ b3TreeStats b3World_CastShape( b3WorldId worldId, b3Pos origin, const b3ShapePro
return treeStats;
}
b3TreeStats b3World_CastShape_Static( b3WorldId worldId, b3Pos origin, const b3ShapeProxy* proxy, b3Vec3 translation,
b3QueryFilter filter, b3CastResultFcn* fcn, void* context )
{
b3World* world = b3GetWorldFromId( worldId );
B3_ASSERT( b3IsValidPosition( origin ) );
B3_ASSERT( b3IsValidVec3( translation ) );
WorldShapeCastContext worldContext = { 0 };
worldContext.world = world;
worldContext.fcn = fcn;
worldContext.filter = filter;
worldContext.fraction = 1.0f;
worldContext.origin = origin;
worldContext.input.proxy = *proxy;
worldContext.input.translation = translation;
worldContext.input.maxFraction = 1.0f;
worldContext.input.canEncroach = false;
worldContext.userContext = context;
b3AABB localBox = b3MakeAABB( proxy->points, proxy->count, proxy->radius );
b3BoxCastInput treeInput = { b3OffsetAABB( localBox, origin ), translation, 1.0f };
return b3DynamicTree_BoxCastRelaxed( world->broadPhase.trees + b3_staticBody, &treeInput, filter.maskBits, false,
b3ShapeCastCallback, &worldContext );
}
typedef struct WorldMoverCastContext
{
b3World* world;
@ -3543,7 +3598,7 @@ static bool ExplosionCallback( int proxyId, uint64_t userData, void* context )
b3Vec3 impulse = b3MulSV( magnitude, b3RotateVector( xf.q, direction ) );
int localIndex = body->localIndex;
b3SolverSet* set = b3Array_Get( world->solverSets, b3_awakeSet );
b3SolverSet* set = b3GetSolverSet( world, b3_awakeSet );
b3BodyState* state = b3Array_Get( set->bodyStates, localIndex );
b3BodySim* bodySim = b3Array_Get( set->bodySims, localIndex );
state->linearVelocity = b3MulAdd( state->linearVelocity, bodySim->invMass, impulse );
@ -3711,7 +3766,7 @@ void b3ValidateSolverSets( b3World* world )
B3_ASSERT( b3GetIdCapacity( &world->contactIdPool ) == world->contacts.count );
B3_ASSERT( b3GetIdCapacity( &world->jointIdPool ) == world->joints.count );
B3_ASSERT( b3GetIdCapacity( &world->islandIdPool ) == world->islands.count );
B3_ASSERT( b3GetIdCapacity( &world->solverSetIdPool ) == world->solverSets.count );
B3_ASSERT( b3GetIdCapacity( &world->solverSetIdPool ) == b3GetSolverSetCount( world ) );
// Ensure the encoded body sim indices are correct.
for ( int contactIndex = 0; contactIndex < world->contacts.count; ++contactIndex )
@ -3735,10 +3790,10 @@ void b3ValidateSolverSets( b3World* world )
int totalIslandCount = 0;
// Validate all solver sets
int setCount = world->solverSets.count;
int setCount = b3GetSolverSetCount( world );
for ( int setIndex = 0; setIndex < setCount; ++setIndex )
{
b3SolverSet* set = world->solverSets.data + setIndex;
b3SolverSet* set = b3GetSolverSet( world, setIndex );
if ( set->setIndex != B3_NULL_INDEX )
{
activeSetCount += 1;
@ -4111,7 +4166,7 @@ void b3ValidateContacts( b3World* world )
bool touching = ( contact->flags & b3_contactTouchingFlag ) != 0;
int setId = contact->setIndex;
b3SolverSet* set = b3Array_Get( world->solverSets, setId );
b3SolverSet* set = b3GetSolverSet( world, setId );
if ( setId == b3_awakeSet )
{