SIMD hull collision (#93)

SIMD based hull collision inspired by example code from @cairnc in #54 

<img width="1676" height="1332" alt="image"
src="https://github.com/user-attachments/assets/58b137bd-bf7b-4c99-863c-64341b973a3d"
/>

- Fixed #78
- Fixed #85 and fixed CI to error on UB.
- Updated benchmarks
- Added AMD scalar benchmarks
- Added Neon benchmarks
- More determinism unit tests
- Sample leak detection
- Added b3Body_AllowFastRotation
- Added b3Body_IsFastRotationAllowed
- Bumped b3HullData version
- Bumped b3CompoundData version
This commit is contained in:
Erin Catto 2026-07-18 22:43:03 -07:00 • committed by GitHub
commit c37cfe4cd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 3898 additions and 1151 deletions

View file

@ -869,6 +869,22 @@ void CreateJunkyard( b3WorldId worldId )
b3DestroyHull( hull );
}
void GetJunkyardCapacity(b3Capacity* capacity)
{
#ifdef NDEBUG
capacity->staticShapeCount = 16;
capacity->dynamicShapeCount = 20 * 20 * 24 + 1;
capacity->staticBodyCount = 16;
capacity->dynamicBodyCount = 20 * 20 * 24 + 1;
capacity->contactCount = 250 * 1024;
#else
capacity->staticShapeCount = 16;
capacity->dynamicShapeCount = 20 * 20 * 2 + 1;
capacity->staticBodyCount = 16;
capacity->dynamicBodyCount = 20 * 20 * 2 + 1;
#endif
}
void StepJunkyard( b3WorldId worldId, int stepCount )
{
(void)worldId;
@ -887,6 +903,15 @@ void StepJunkyard( b3WorldId worldId, int stepCount )
// Huge pile of large convexes, ported from PEEL. Each convex is the hull of 32 random points on a
// sphere. A fixed LCG seed makes the hull identical across runs so results compare directly.
void GetConvexPileCapacity( b3Capacity* capacity )
{
#ifdef NDEBUG
capacity->dynamicShapeCount = 5120;
capacity->dynamicBodyCount = 5120;
capacity->contactCount = 50 * 1024;
#endif
}
// PEEL's BasicRandom, kept verbatim so the point set matches the original
typedef struct ConvexPileRandom
{

View file

@ -26,6 +26,7 @@ void CreateTrees100( b3WorldId worldId );
void DestroyTrees( void );
void CreateJointGrid( b3WorldId worldId );
void CreateJunkyard( b3WorldId worldId );
void GetJunkyardCapacity( b3Capacity* capacity );
void StepJunkyard( b3WorldId worldId, int stepCount );
void CreateLargePyramid( b3WorldId worldId );
void CreateWidePyramid( b3WorldId worldId );
@ -40,6 +41,7 @@ void StepLargeWorld( b3WorldId worldId, int stepCount );
void GetWasherCapacity( b3Capacity* capacity );
void CreateWasher( b3WorldId worldId );
void CreateConvexPile( b3WorldId worldId );
void GetConvexPileCapacity( b3Capacity* capacity );
// void CreateSpinner( b3WorldId worldId );
// float StepSpinner( b3WorldId worldId, int stepCount );

View file

@ -4,6 +4,7 @@
#include "determinism.h"
#include "human.h"
#include "utils.h"
#include "box3d/box3d.h"
@ -122,3 +123,276 @@ void DestroyFallingRagdolls( FallingRagdollData* data )
data->gridMesh = NULL;
data->torusMesh = NULL;
}
#define WAVE_PILE_GRID 5
#define WAVE_PILE_LAYERS 4
WavePileData CreateWavePile( b3WorldId worldId )
{
WavePileData data = { 0 };
g_randomSeed = 52977;
// Height fields grow from a corner, offset the body to center the patch.
int fieldCount = 21;
b3Vec3 fieldScale = { 1.0f, 0.6f, 1.0f };
data.heightField = b3CreateWave( fieldCount, fieldCount, fieldScale, 0.08f, 0.06f, false );
{
float extent = fieldScale.x * ( fieldCount - 1 );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position.x = -0.5f * extent;
bodyDef.position.z = -0.5f * extent;
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHeightFieldShape( groundId, &shapeDef, data.heightField );
}
b3HullData* rock = b3CreateRock( 0.55f );
b3BoxHull box = b3MakeBoxHull( 0.45f, 0.3f, 0.55f );
b3Sphere sphere = { b3Vec3_zero, 0.5f };
b3Capsule capsule = { { 0.0f, -0.3f, 0.0f }, { 0.0f, 0.3f, 0.0f }, 0.35f };
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
// Rolling resistance so the pile sleeps quickly.
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.rollingResistance = 0.3f;
float spacing = 1.7f;
int index = 0;
for ( int layer = 0; layer < WAVE_PILE_LAYERS; ++layer )
{
for ( int i = 0; i < WAVE_PILE_GRID; ++i )
{
for ( int j = 0; j < WAVE_PILE_GRID; ++j )
{
b3Vec3 jitter = RandomVec3Uniform( -0.3f, 0.3f );
bodyDef.position.x = spacing * ( i - 0.5f * ( WAVE_PILE_GRID - 1 ) ) + jitter.x;
bodyDef.position.y = 2.5f + 1.6f * layer + 0.3f * jitter.y;
bodyDef.position.z = spacing * ( j - 0.5f * ( WAVE_PILE_GRID - 1 ) ) + jitter.z;
bodyDef.rotation = RandomQuat();
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
data.bodies[index] = bodyId;
switch ( index % 4 )
{
case 0:
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
break;
case 1:
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
break;
case 2:
b3CreateHullShape( bodyId, &shapeDef, &box.base );
break;
default:
b3CreateHullShape( bodyId, &shapeDef, rock );
break;
}
index += 1;
}
}
}
assert( index == WAVE_PILE_BODY_COUNT );
// The world keeps its own copy.
b3DestroyHull( rock );
return data;
}
bool UpdateWavePile( b3WorldId worldId, WavePileData* data )
{
if ( data->hash == 0 )
{
if ( b3World_GetAwakeBodyCount( worldId ) == 0 )
{
data->hash = B3_HASH_INIT;
for ( int i = 0; i < WAVE_PILE_BODY_COUNT; ++i )
{
b3WorldTransform xf = b3Body_GetTransform( data->bodies[i] );
data->hash = b3Hash( data->hash, (uint8_t*)&xf, sizeof( b3WorldTransform ) );
}
data->sleepStep = data->stepCount;
}
}
data->stepCount += 1;
return data->hash != 0;
}
void DestroyWavePile( WavePileData* data )
{
b3DestroyHeightField( data->heightField );
data->heightField = NULL;
}
typedef struct QuerySpawnOverlapContext
{
QuerySpawnData* data;
int count;
} QuerySpawnOverlapContext;
static bool QuerySpawnOverlapCallback( b3ShapeId shapeId, void* context )
{
QuerySpawnOverlapContext* overlap = context;
overlap->count += 1;
overlap->data->queryHitCount += 1;
overlap->data->queryHash = b3Hash( overlap->data->queryHash, (uint8_t*)&shapeId.index1, sizeof( shapeId.index1 ) );
return true;
}
static float QuerySpawnCastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
int triangleIndex, int childIndex, void* context )
{
(void)shapeId, (void)point, (void)normal, (void)userMaterialId, (void)triangleIndex, (void)childIndex;
float* closest = context;
*closest = fraction;
return fraction;
}
static void QuerySpawnOnce( b3WorldId worldId, QuerySpawnData* data )
{
b3QueryFilter filter = b3DefaultQueryFilter();
// Ray result decides the spawn position, a miss seeds the cloud near the origin.
b3Pos rayOrigin = RandomPos( (b3Vec3){ -12.0f, -12.0f, -12.0f }, (b3Vec3){ 12.0f, 12.0f, 12.0f } );
b3Vec3 rayTranslation = b3MulSV( 30.0f, RandomUnitVector() );
b3Pos spawnPosition;
b3RayResult ray = b3World_CastRayClosest( worldId, rayOrigin, rayTranslation, filter );
if ( ray.hit )
{
data->queryHitCount += 1;
data->queryHash = b3Hash( data->queryHash, (uint8_t*)&ray.fraction, sizeof( ray.fraction ) );
data->queryHash = b3Hash( data->queryHash, (uint8_t*)&ray.normal, sizeof( ray.normal ) );
spawnPosition = b3OffsetPos( ray.point, b3MulSV( 1.2f, ray.normal ) );
}
else
{
spawnPosition = RandomPos( (b3Vec3){ -6.0f, -6.0f, -6.0f }, (b3Vec3){ 6.0f, 6.0f, 6.0f } );
}
data->rayOrigin = rayOrigin;
data->rayTranslation = rayTranslation;
data->rayDidHit = ray.hit;
data->rayPoint = ray.hit ? ray.point : b3OffsetPos( rayOrigin, rayTranslation );
data->rayNormal = ray.hit ? ray.normal : b3Vec3_zero;
// Overlap count picks the shape type.
b3Vec3 center = RandomVec3Uniform( -10.0f, 10.0f );
float extent = RandomFloatRange( 1.0f, 4.0f );
b3AABB aabb;
aabb.lowerBound = (b3Vec3){ center.x - extent, center.y - extent, center.z - extent };
aabb.upperBound = (b3Vec3){ center.x + extent, center.y + extent, center.z + extent };
QuerySpawnOverlapContext overlap = { data, 0 };
b3World_OverlapAABB( worldId, aabb, filter, QuerySpawnOverlapCallback, &overlap );
data->overlapBounds = aabb;
data->overlapCount = overlap.count;
// Sphere cast fraction sets the spawn size.
float fraction = 1.0f;
b3Vec3 proxyPoint = b3Vec3_zero;
b3ShapeProxy proxy = { &proxyPoint, 1, QUERY_SPAWN_CAST_RADIUS };
b3World_CastShape( worldId, rayOrigin, &proxy, rayTranslation, filter, QuerySpawnCastCallback, &fraction );
if ( fraction < 1.0f )
{
data->queryHitCount += 1;
data->queryHash = b3Hash( data->queryHash, (uint8_t*)&fraction, sizeof( fraction ) );
}
data->castFraction = fraction;
float size = 0.3f + 0.2f * fraction;
// Damping guarantees everything comes to rest.
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = spawnPosition;
bodyDef.rotation = RandomQuat();
bodyDef.linearVelocity = RandomVec3Uniform( -0.2f, 0.2f );
bodyDef.angularVelocity = RandomVec3Uniform( -0.5f, 0.5f );
bodyDef.linearDamping = 1.0f;
bodyDef.angularDamping = 1.0f;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.baseMaterial.rollingResistance = 0.2f;
switch ( ( data->spawnCount + overlap.count ) % 3 )
{
case 0:
{
b3Sphere sphere = { b3Vec3_zero, size };
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
break;
}
case 1:
{
b3Capsule capsule = { { 0.0f, -size, 0.0f }, { 0.0f, size, 0.0f }, 0.7f * size };
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
break;
}
default:
{
b3BoxHull box = b3MakeBoxHull( size, 0.7f * size, 0.5f * size );
b3CreateHullShape( bodyId, &shapeDef, &box.base );
break;
}
}
data->bodies[data->spawnCount] = bodyId;
data->spawnCount += 1;
data->lastSpawnPosition = spawnPosition;
}
QuerySpawnData CreateQuerySpawn( b3WorldId worldId )
{
QuerySpawnData data = { 0 };
g_randomSeed = 71689;
// Empty space, motion comes only from spawn velocities and overlap pushes.
b3World_SetGravity( worldId, b3Vec3_zero );
return data;
}
bool UpdateQuerySpawn( b3WorldId worldId, QuerySpawnData* data )
{
if ( data->spawnCount < QUERY_SPAWN_COUNT )
{
QuerySpawnOnce( worldId, data );
}
else if ( data->hash == 0 && b3World_GetAwakeBodyCount( worldId ) == 0 )
{
data->hash = B3_HASH_INIT;
for ( int i = 0; i < QUERY_SPAWN_COUNT; ++i )
{
b3WorldTransform xf = b3Body_GetTransform( data->bodies[i] );
data->hash = b3Hash( data->hash, (uint8_t*)&xf, sizeof( b3WorldTransform ) );
}
data->sleepStep = data->stepCount;
}
data->stepCount += 1;
return data->hash != 0;
}
void DestroyQuerySpawn( QuerySpawnData* data )
{
(void)data;
}

View file

@ -36,6 +36,55 @@ FallingRagdollData CreateFallingRagdolls( b3WorldId worldId );
bool UpdateFallingRagdolls( b3WorldId worldId, FallingRagdollData* data );
void DestroyFallingRagdolls( FallingRagdollData* data );
// Convex pile dropped on a wave height field. Spheres, capsules, boxes, and rocks with
// rolling resistance so the pile sleeps quickly.
#define WAVE_PILE_BODY_COUNT 100
typedef struct WavePileData
{
b3BodyId bodies[WAVE_PILE_BODY_COUNT];
b3HeightFieldData* heightField;
int stepCount;
int sleepStep;
uint32_t hash;
} WavePileData;
WavePileData CreateWavePile( b3WorldId worldId );
bool UpdateWavePile( b3WorldId worldId, WavePileData* data );
void DestroyWavePile( WavePileData* data );
// Query driven spawning in an empty zero gravity world. Each step casts a ray, overlaps an
// AABB, and casts a sphere, then spawns a shape whose position, type, and size depend on the
// query results. Any query divergence cascades into the final state.
#define QUERY_SPAWN_COUNT 50
#define QUERY_SPAWN_CAST_RADIUS 0.5f
typedef struct QuerySpawnData
{
b3BodyId bodies[QUERY_SPAWN_COUNT];
int spawnCount;
int queryHitCount;
uint32_t queryHash;
int stepCount;
int sleepStep;
uint32_t hash;
// Last query cycle, recorded for visualization
b3Pos rayOrigin;
b3Vec3 rayTranslation;
b3Pos rayPoint;
b3Vec3 rayNormal;
bool rayDidHit;
b3AABB overlapBounds;
int overlapCount;
float castFraction;
b3Pos lastSpawnPosition;
} QuerySpawnData;
QuerySpawnData CreateQuerySpawn( b3WorldId worldId );
bool UpdateQuerySpawn( b3WorldId worldId, QuerySpawnData* data );
void DestroyQuerySpawn( QuerySpawnData* data );
#ifdef __cplusplus
}
#endif

View file

@ -10,8 +10,6 @@
#include <assert.h>
#include <stdlib.h>
#define MESH_DROP_GRID_COUNT 32
MeshDropData CreateMeshDrop( b3WorldId worldId, b3Pos origin )
{
MeshDropData data = { 0 };
@ -61,6 +59,7 @@ MeshDropData CreateMeshDrop( b3WorldId worldId, b3Pos origin )
bodyDef.linearVelocity = linearVelocity;
bodyDef.angularVelocity = angularVelocity;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
data.bodies[i * gridCount + j] = bodyId;
b3CreateHullShape( bodyId, &shapeDef, &box.base );
}
@ -70,6 +69,29 @@ MeshDropData CreateMeshDrop( b3WorldId worldId, b3Pos origin )
return data;
}
bool UpdateMeshDrop( b3WorldId worldId, MeshDropData* data )
{
if ( data->hash == 0 )
{
if ( b3World_GetAwakeBodyCount( worldId ) == 0 )
{
data->hash = B3_HASH_INIT;
int bodyCount = MESH_DROP_GRID_COUNT * MESH_DROP_GRID_COUNT;
for ( int i = 0; i < bodyCount; ++i )
{
b3WorldTransform xf = b3Body_GetTransform( data->bodies[i] );
data->hash = b3Hash( data->hash, (uint8_t*)&xf, sizeof( b3WorldTransform ) );
}
data->sleepStep = data->stepCount;
}
}
data->stepCount += 1;
return data->hash != 0;
}
void DestroyMeshDrop( MeshDropData* data )
{
b3DestroyMesh( data->mesh );

View file

@ -12,12 +12,21 @@ extern "C"
{
#endif
#define MESH_DROP_GRID_COUNT 20
// Thin fast boxes dropped on a wave mesh. Stresses continuous collision and mesh contact
// stability, and doubles as a determinism scenario via the sleep hash.
typedef struct MeshDropData
{
struct b3MeshData* mesh;
b3BodyId bodies[MESH_DROP_GRID_COUNT * MESH_DROP_GRID_COUNT];
int stepCount;
int sleepStep;
uint32_t hash;
} MeshDropData;
MeshDropData CreateMeshDrop( b3WorldId worldId, b3Pos origin );
bool UpdateMeshDrop( b3WorldId worldId, MeshDropData* data );
void DestroyMeshDrop( MeshDropData* data );
#ifdef __cplusplus

View file

@ -102,10 +102,13 @@ B3_INLINE b3Vec3 RandomUnitVector()
float sqrt1MinusU1 = sqrtf( 1.0f - u1 );
float sqrtU1 = sqrtf( u1 );
b3CosSin cs2 = b3ComputeCosSin( u2 );
b3CosSin cs3 = b3ComputeCosSin( u3 );
b3Vec3 v;
v.x = sqrt1MinusU1 * sinf( u2 );
v.y = sqrt1MinusU1 * cosf( u2 );
v.z = sqrtU1 * sinf( u3 );
v.x = sqrt1MinusU1 * cs2.sine;
v.y = sqrt1MinusU1 * cs2.cosine;
v.z = sqrtU1 * cs3.sine;
return v;
}
@ -122,11 +125,14 @@ B3_INLINE b3Quat RandomQuat()
float sqrt1MinusU1 = sqrtf( 1.0f - u1 );
float sqrtU1 = sqrtf( u1 );
b3CosSin cs2 = b3ComputeCosSin( u2 );
b3CosSin cs3 = b3ComputeCosSin( u3 );
b3Quat q;
q.v.x = sqrt1MinusU1 * sinf( u2 );
q.v.y = sqrt1MinusU1 * cosf( u2 );
q.v.z = sqrtU1 * sinf( u3 );
q.s = sqrtU1 * cosf( u3 );
q.v.x = sqrt1MinusU1 * cs2.sine;
q.v.y = sqrt1MinusU1 * cs2.cosine;
q.v.z = sqrtU1 * cs3.sine;
q.s = sqrtU1 * cs3.cosine;
return q;
}