- #86
- #88 
- #87 
- #89
This commit is contained in:
Erin Catto 2026-07-24 21:48:23 -07:00 • committed by GitHub
commit 781673be80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 705 additions and 152 deletions

View file

@ -355,6 +355,114 @@ static int OverlapFilter( void )
return 0;
}
static bool CountOverlapCallback( b3ShapeId shapeId, void* context )
{
(void)shapeId;
*(int*)context += 1;
return true;
}
// A box hull proxy built around a world target with a zero origin must hit the same shapes as the
// same box built at the local origin and queried with the target as origin. This is the origin
// relative equivalence the world query promises, and the pattern users reach for when they bake a
// query box with b3MakeTransformedBoxHull.
static int OverlapHullProxyEquivalence( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_staticBody;
bodyDef.position = (b3Pos){ 10.0f, 0.0f, 0.0f };
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHullShape( bodyId, &shapeDef, &box.base );
b3World_Step( worldId, 1.0f / 60.0f, 1 );
b3QueryFilter filter = b3DefaultQueryFilter();
// Overlapping target: a 10 wide query box centered on the body.
{
b3Vec3 offset = { 10.0f, 0.0f, 0.0f };
b3BoxHull baked = b3MakeTransformedBoxHull( 5.0f, 5.0f, 5.0f, (b3Transform){ offset, b3Quat_identity } );
b3ShapeProxy bakedProxy = { baked.boxPoints, baked.base.vertexCount, 0.0f };
int bakedHits = 0;
b3World_OverlapShape( worldId, b3Pos_zero, &bakedProxy, filter, CountOverlapCallback, &bakedHits );
b3Pos origin = b3OffsetPos( b3Pos_zero, offset );
b3BoxHull local = b3MakeBoxHull( 5.0f, 5.0f, 5.0f );
b3ShapeProxy localProxy = { local.boxPoints, local.base.vertexCount, 0.0f };
int localHits = 0;
b3World_OverlapShape( worldId, origin, &localProxy, filter, CountOverlapCallback, &localHits );
ENSURE( bakedHits == 1 );
ENSURE( localHits == bakedHits );
}
// Clearing target: same box far from the body, both formulations agree on the miss.
{
b3Vec3 offset = { 100.0f, 0.0f, 0.0f };
b3BoxHull baked = b3MakeTransformedBoxHull( 5.0f, 5.0f, 5.0f, (b3Transform){ offset, b3Quat_identity } );
b3ShapeProxy bakedProxy = { baked.boxPoints, baked.base.vertexCount, 0.0f };
int bakedHits = 0;
b3World_OverlapShape( worldId, b3Pos_zero, &bakedProxy, filter, CountOverlapCallback, &bakedHits );
b3Pos origin = b3OffsetPos( b3Pos_zero, offset );
b3BoxHull local = b3MakeBoxHull( 5.0f, 5.0f, 5.0f );
b3ShapeProxy localProxy = { local.boxPoints, local.base.vertexCount, 0.0f };
int localHits = 0;
b3World_OverlapShape( worldId, origin, &localProxy, filter, CountOverlapCallback, &localHits );
ENSURE( bakedHits == 0 );
ENSURE( localHits == bakedHits );
}
b3DestroyWorld( worldId );
return 0;
}
// A quarter turn baked into the query hull must reach the overlap test. A long thin bar hits a body
// off the origin when aligned along X and clears it once rotated to lie along Z.
static int OverlapHullProxyRotation( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_staticBody;
bodyDef.position = (b3Pos){ 3.0f, 0.0f, 0.0f };
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
b3CreateHullShape( bodyId, &shapeDef, &box.base );
b3World_Step( worldId, 1.0f / 60.0f, 1 );
b3QueryFilter filter = b3DefaultQueryFilter();
// Bar long in local X, centered at the origin, reaches the body at x = 3.
b3BoxHull aligned = b3MakeTransformedBoxHull( 4.0f, 0.3f, 0.3f, b3Transform_identity );
b3ShapeProxy alignedProxy = { aligned.boxPoints, aligned.base.vertexCount, 0.0f };
int alignedHits = 0;
b3World_OverlapShape( worldId, b3Pos_zero, &alignedProxy, filter, CountOverlapCallback, &alignedHits );
ENSURE( alignedHits == 1 );
// Rotated a quarter turn about Y the long axis points along Z, so the bar no longer reaches x = 3.
b3Quat q = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 1.0f, 0.0f }, 0.5f * B3_PI );
b3BoxHull turned = b3MakeTransformedBoxHull( 4.0f, 0.3f, 0.3f, (b3Transform){ b3Vec3_zero, q } );
b3ShapeProxy turnedProxy = { turned.boxPoints, turned.base.vertexCount, 0.0f };
int turnedHits = 0;
b3World_OverlapShape( worldId, b3Pos_zero, &turnedProxy, filter, CountOverlapCallback, &turnedHits );
ENSURE( turnedHits == 0 );
b3DestroyWorld( worldId );
return 0;
}
// CollideMover -----------------------------------------------------------------------------
static int MoverTouchesBox( void )
@ -474,6 +582,8 @@ int BodyQueryTest( void )
RUN_SUBTEST( OverlapFalse );
RUN_SUBTEST( OverlapRespectsBodyTransform );
RUN_SUBTEST( OverlapFilter );
RUN_SUBTEST( OverlapHullProxyEquivalence );
RUN_SUBTEST( OverlapHullProxyRotation );
RUN_SUBTEST( MoverTouchesBox );
RUN_SUBTEST( MoverSeparated );

View file

@ -409,6 +409,107 @@ static int CreateHullMergeChurnStressTest( void )
return 0;
}
// b3ComputeCosSin is a coarse approximation, so build rotations from libm to keep the
// analytic corner and plane positions exact.
static b3Quat ExactQuat( b3Vec3 axis, float radians )
{
float half = 0.5f * radians;
float s = sinf( half );
return (b3Quat){ { s * axis.x, s * axis.y, s * axis.z }, cosf( half ) };
}
// Corner sign pattern baked by b3MakeTransformedBoxHull, in order.
static const b3Vec3 s_boxCornerSigns[8] = {
{ 1.0f, 1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, -1.0f }, { -1.0f, 1.0f, -1.0f }, { -1.0f, -1.0f, -1.0f }, { 1.0f, -1.0f, -1.0f },
};
// The baked box hull is only exercised elsewhere through mass properties, which are analytic
// and never read boxPoints or boxPlanes. Pin the geometry itself against the transform so an
// axis swap in the point or plane bake cannot pass silently.
static int CheckTransformedBox( b3Vec3 h, b3Transform xf )
{
b3BoxHull box = b3MakeTransformedBoxHull( h.x, h.y, h.z, xf );
const float tol = 1e-5f;
// Each corner is the transform of the signed half extent.
for ( int i = 0; i < 8; ++i )
{
b3Vec3 local = { s_boxCornerSigns[i].x * h.x, s_boxCornerSigns[i].y * h.y, s_boxCornerSigns[i].z * h.z };
b3Vec3 expected = b3TransformPoint( xf, local );
ENSURE_SMALL( box.boxPoints[i].x - expected.x, tol );
ENSURE_SMALL( box.boxPoints[i].y - expected.y, tol );
ENSURE_SMALL( box.boxPoints[i].z - expected.z, tol );
}
// Face normals rotate with the box, offsets carry the rotated normal through the translation.
b3Vec3 localNormals[6] = {
b3Neg( b3Vec3_axisX ), b3Vec3_axisX, b3Neg( b3Vec3_axisY ), b3Vec3_axisY, b3Neg( b3Vec3_axisZ ), b3Vec3_axisZ,
};
float localOffsets[6] = { h.x, h.x, h.y, h.y, h.z, h.z };
for ( int i = 0; i < 6; ++i )
{
b3Vec3 n = b3RotateVector( xf.q, localNormals[i] );
float offset = localOffsets[i] + b3Dot( n, xf.p );
ENSURE_SMALL( box.boxPlanes[i].normal.x - n.x, tol );
ENSURE_SMALL( box.boxPlanes[i].normal.y - n.y, tol );
ENSURE_SMALL( box.boxPlanes[i].normal.z - n.z, tol );
ENSURE_SMALL( box.boxPlanes[i].offset - offset, tol );
}
// SoA vertex mirrors match the AoS points on all eight lanes.
for ( int i = 0; i < 8; ++i )
{
ENSURE_SMALL( box.vx[i] - box.boxPoints[i].x, tol );
ENSURE_SMALL( box.vy[i] - box.boxPoints[i].y, tol );
ENSURE_SMALL( box.vz[i] - box.boxPoints[i].z, tol );
}
// SoA normal mirrors carry the six real faces and zero the two pad lanes.
for ( int i = 0; i < 6; ++i )
{
ENSURE_SMALL( box.nx[i] - box.boxPlanes[i].normal.x, tol );
ENSURE_SMALL( box.ny[i] - box.boxPlanes[i].normal.y, tol );
ENSURE_SMALL( box.nz[i] - box.boxPlanes[i].normal.z, tol );
}
ENSURE( box.nx[6] == 0.0f && box.nx[7] == 0.0f );
ENSURE( box.ny[6] == 0.0f && box.ny[7] == 0.0f );
ENSURE( box.nz[6] == 0.0f && box.nz[7] == 0.0f );
// The stored AABB bounds every baked corner.
b3AABB pointBox = b3MakeAABB( box.boxPoints, 8, 0.0f );
ENSURE( b3AABB_Contains( box.base.aabb, pointBox ) );
return 0;
}
static int TransformedBoxHullTest( void )
{
b3Vec3 h = { 0.25f, 0.5f, 0.3f };
// Identity.
if ( CheckTransformedBox( h, b3Transform_identity ) )
return 1;
// Translation only.
b3Transform translated = { { 0.4f, -0.7f, 0.1f }, b3Quat_identity };
if ( CheckTransformedBox( h, translated ) )
return 1;
// Rotation only. A quarter turn about Y swaps the world X and Z extents.
b3Transform rotated = { b3Vec3_zero, ExactQuat( b3Vec3_axisY, 0.25f * B3_PI ) };
if ( CheckTransformedBox( h, rotated ) )
return 1;
// Translation and rotation together.
b3Transform transformed = { { 3.0f, -2.0f, 1.5f }, ExactQuat( b3Vec3_axisZ, 0.25f * B3_PI ) };
if ( CheckTransformedBox( h, transformed ) )
return 1;
return 0;
}
static int CreateHullDegenerateTest( void )
{
// Real (non-null) buffer; pointCount < 4 cases are guarded inside Construct().
@ -456,6 +557,7 @@ int HullTest( void )
RUN_SUBTEST( CreateHullSphereStressTest );
RUN_SUBTEST( CreateHullMergeChurnStressTest );
RUN_SUBTEST( CreateHullDegenerateTest );
RUN_SUBTEST( TransformedBoxHullTest );
return 0;
}

View file

@ -1117,6 +1117,133 @@ static int CapsuleHullSeamTest( void )
return 0;
}
// A capsule core straddling a triangle face inside the interior. The core pierces the plane so the
// deep path runs, and with the tilt kept small the face stays the axis of minimum penetration, so the
// clip must return two points on the triangle face. This is the branch that had no coverage.
static int CapsuleTriangleFaceDeepTest( void )
{
// Triangle in the y = 0 plane, normal +y, centroid at the origin
b3Vec3 v1 = { -3.0f, 0.0f, -2.0f };
b3Vec3 v2 = { 0.0f, 0.0f, 4.0f };
b3Vec3 v3 = { 3.0f, 0.0f, -2.0f };
b3Vec3 triangle[] = { v1, v2, v3 };
float yaws[] = { 0.0f, 0.6f, 1.2f, 1.8f, 2.4f };
float tilts[] = { 0.06f, 0.1f, 0.15f };
float radii[] = { 0.05f, 0.1f, 0.2f };
// Bias the center just above the plane so the back side cull passes while the lower endpoint dips through
float bias = 0.01f;
float halfLength = 1.0f;
int faceContacts = 0;
for ( int i = 0; i < ARRAY_COUNT( yaws ); ++i )
{
for ( int j = 0; j < ARRAY_COUNT( tilts ); ++j )
{
for ( int r = 0; r < ARRAY_COUNT( radii ); ++r )
{
// Axis is the in-plane heading tipped up so the segment straddles the plane
b3Vec3 axis = { cosf( tilts[j] ) * cosf( yaws[i] ), sinf( tilts[j] ), cosf( tilts[j] ) * sinf( yaws[i] ) };
b3Vec3 center = { 0.0f, bias, 0.0f };
b3Vec3 c1 = b3MulAdd( center, -halfLength, axis );
b3Vec3 c2 = b3MulAdd( center, halfLength, axis );
b3Capsule capsule = { c1, c2, radii[r] };
b3LocalManifoldPoint points[8];
b3LocalManifold manifold = { 0 };
manifold.points = points;
b3SimplexCache cache = { 0 };
b3CollideTriangleAndCapsule( &manifold, 8, triangle, &capsule, &cache );
// Two points on the triangle face with the plane normal
ENSURE( manifold.pointCount == 2 );
ENSURE( manifold.feature == b3_featureTriangleFace );
ENSURE_SMALL( manifold.normal.x, 1e-5f );
ENSURE_SMALL( manifold.normal.y - 1.0f, 1e-5f );
ENSURE_SMALL( manifold.normal.z, 1e-5f );
// Separations are the endpoint heights pulled in by the radius. The lower endpoint is below
// the plane, so the deepest separation is negative.
float lower = b3MinFloat( c1.y, c2.y ) - radii[r];
float upper = b3MaxFloat( c1.y, c2.y ) - radii[r];
float minSep = b3MinFloat( manifold.points[0].separation, manifold.points[1].separation );
float maxSep = b3MaxFloat( manifold.points[0].separation, manifold.points[1].separation );
ENSURE_SMALL( minSep - lower, 1e-5f );
ENSURE_SMALL( maxSep - upper, 1e-5f );
ENSURE( minSep < 0.0f );
++faceContacts;
}
}
}
// Every configuration must reach the face path
ENSURE( faceContacts == ARRAY_COUNT( yaws ) * ARRAY_COUNT( tilts ) * ARRAY_COUNT( radii ) );
return 0;
}
// A capsule laid flat with its core below a box top face. The core sits inside the box so the deep
// path runs, and across a sweep of depths, headings and radii the face clip must return two points.
static int HullCapsuleFaceDeepTest( void )
{
b3BoxHull hull = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
float depths[] = { 0.1f, 0.2f, 0.3f, 0.4f, 0.45f };
float yaws[] = { 0.0f, 0.4f, 0.8f, 1.2f };
float radii[] = { 0.1f, 0.15f, 0.2f };
float offsets[] = { -0.1f, 0.0f, 0.1f };
float halfLength = 0.3f;
int faceContacts = 0;
for ( int i = 0; i < ARRAY_COUNT( depths ); ++i )
{
for ( int j = 0; j < ARRAY_COUNT( yaws ); ++j )
{
for ( int r = 0; r < ARRAY_COUNT( radii ); ++r )
{
for ( int o = 0; o < ARRAY_COUNT( offsets ); ++o )
{
float y = depths[i];
b3Vec3 dir = { cosf( yaws[j] ), 0.0f, sinf( yaws[j] ) };
b3Vec3 center = { offsets[o], y, 0.0f };
b3Vec3 c1 = b3MulAdd( center, -halfLength, dir );
b3Vec3 c2 = b3MulAdd( center, halfLength, dir );
b3Capsule capsule = { c1, c2, radii[r] };
b3LocalManifoldPoint points[8];
b3LocalManifold manifold = { 0 };
manifold.points = points;
b3SimplexCache cache = { 0 };
b3CollideHullAndCapsule( &manifold, 8, &hull.base, &capsule, b3Transform_identity, &cache );
// Two points on the top face. The hull path does not tag a feature, so the face is
// identified by the normal and the point count.
ENSURE( manifold.pointCount == 2 );
ENSURE_SMALL( manifold.normal.x, 1e-5f );
ENSURE_SMALL( manifold.normal.y - 1.0f, 1e-5f );
ENSURE_SMALL( manifold.normal.z, 1e-5f );
// Flat capsule, so both points sit at the same analytic gap
float expected = ( y - 0.5f ) - radii[r];
ENSURE_SMALL( manifold.points[0].separation - expected, 1e-5f );
ENSURE_SMALL( manifold.points[1].separation - expected, 1e-5f );
ENSURE( expected < 0.0f );
++faceContacts;
}
}
}
}
ENSURE( faceContacts == ARRAY_COUNT( depths ) * ARRAY_COUNT( yaws ) * ARRAY_COUNT( radii ) * ARRAY_COUNT( offsets ) );
return 0;
}
int ManifoldTest( void )
{
RUN_SUBTEST( CrossedEdgeTest );
@ -1134,6 +1261,8 @@ int ManifoldTest( void )
RUN_SUBTEST( HullCapsuleEdgeDeepTest );
RUN_SUBTEST( TriangleHullEdgeSweepTest );
RUN_SUBTEST( CapsuleTriangleEdgeDeepTest );
RUN_SUBTEST( CapsuleTriangleFaceDeepTest );
RUN_SUBTEST( HullCapsuleFaceDeepTest );
RUN_SUBTEST( SphereHullSeamTest );
RUN_SUBTEST( CapsuleHullSeamTest );

View file

@ -1117,8 +1117,11 @@ static int AllOps( void )
b3BodyId meshBodyId = b3CreateBody( worldId, &meshBodyDef );
b3MeshData* meshData = b3CreateGridMesh( 3, 3, 2.0f, 0, false );
ENSURE( meshData != NULL );
b3MeshData* swapMeshData = b3CreateGridMesh( 4, 4, 1.5f, 0, false );
ENSURE( swapMeshData != NULL );
b3ShapeDef meshShapeDef = b3DefaultShapeDef();
b3CreateMeshShape( meshBodyId, &meshShapeDef, meshData, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
b3ShapeId meshShapeId = b3CreateMeshShape( meshBodyId, &meshShapeDef, meshData, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
ENSURE( b3Shape_IsValid( meshShapeId ) );
b3BodyDef hfBodyDef = b3DefaultBodyDef();
hfBodyDef.type = b3_staticBody;
@ -1150,9 +1153,9 @@ static int AllOps( void )
b3ShapeId tmpShapeId = b3CreateSphereShape( capsuleBodyId, &capsuleShapeDef, &tmpSphere );
b3DestroyShape( tmpShapeId, true );
// Shape mutators: SetFriction, SetRestitution, SetDensity, SetSurfaceMaterial, SetFilter,
// EnableSensorEvents, EnableContactEvents, EnableHitEvents, EnablePreSolveEvents, ApplyWind,
// SetSphere, SetCapsule, SetName
// Shape mutators: SetFriction, SetRestitution, SetDensity, SetSurfaceMaterial, SetMeshMaterial,
// SetFilter, EnableSensorEvents, EnableContactEvents, EnableHitEvents, EnablePreSolveEvents,
// ApplyWind, SetSphere, SetCapsule, SetHull, SetMesh, SetName
b3Shape_SetFriction( boxShapeId, 0.3f );
b3Shape_SetRestitution( capsuleShapeId, 0.5f );
b3Shape_SetDensity( boxShapeId, 3.0f, true );
@ -1174,6 +1177,14 @@ static int AllOps( void )
b3Shape_SetCapsule( capsuleShapeId, &newCapsule );
b3Shape_SetName( boxShapeId, "box" );
// Geometry swaps intern into the registry at the record site. The repeated SetHull takes the
// shared hull short circuit, which changes nothing and so must leave the stream alone.
b3BoxHull swapHull = b3MakeBoxHull( 0.3f, 0.7f, 0.4f );
b3Shape_SetHull( boxShapeId, &swapHull.base );
b3Shape_SetHull( boxShapeId, &swapHull.base );
b3Shape_SetMesh( meshShapeId, swapMeshData, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
b3Shape_SetMeshMaterial( meshShapeId, surfMat, 0 );
// Body mutators: SetTransform, SetLinearVelocity/AngularVelocity (Vec3), SetName,
// damping, gravity scale, sleep threshold, SetAwake, EnableSleep, SetBullet, SetMotionLocks,
// SetMassData, ApplyMassFromShapes, SetType, SetTargetTransform, Disable/Enable, EnableContactRecycling,
@ -1453,6 +1464,7 @@ static int AllOps( void )
// Free geometry allocated for this subtest
b3DestroyHull( customHull );
b3DestroyMesh( meshData );
b3DestroyMesh( swapMeshData );
b3DestroyHeightField( hf );
b3DestroyCompound( compound );
@ -1679,7 +1691,7 @@ static int GeometryHashCollision( void )
uint32_t idA = b3InternGeometry( &reg, b3_geometryHull, sharedHash, blobA, n );
uint32_t idB = b3InternGeometry( &reg, b3_geometryHull, sharedHash, blobB, n );
ENSURE( idA != idB );
ENSURE( reg.count == 2 );
ENSURE( reg.entries.count == 2 );
// Re-interning either blob must find it through the hash chain and never grow the registry,
// including the one shadowed behind the bucket head. The old single-entry lookup missed the
@ -1687,12 +1699,12 @@ static int GeometryHashCollision( void )
uint8_t* blobA2 = (uint8_t*)b3Alloc( (size_t)n );
memset( blobA2, 0xAA, (size_t)n );
ENSURE( b3InternGeometry( &reg, b3_geometryHull, sharedHash, blobA2, n ) == idA );
ENSURE( reg.count == 2 );
ENSURE( reg.entries.count == 2 );
uint8_t* blobB2 = (uint8_t*)b3Alloc( (size_t)n );
memset( blobB2, 0xBB, (size_t)n );
ENSURE( b3InternGeometry( &reg, b3_geometryHull, sharedHash, blobB2, n ) == idB );
ENSURE( reg.count == 2 );
ENSURE( reg.entries.count == 2 );
b3FreeRegistry( &reg );
@ -1712,10 +1724,10 @@ static int GeometryHashCollision( void )
uint8_t* live = (uint8_t*)b3Alloc( (size_t)n );
memset( live, 0xAA, (size_t)n );
uint32_t resolved = b3InternGeometry( &seeded, b3_geometryHull, sharedHash, live, n );
ENSURE( seeded.count == 3 ); // no growth
ENSURE( seeded.entries.count == 3 ); // no growth
ENSURE( resolved == 0 || resolved == 2 ); // a valid slot index for that content
ENSURE( seeded.entries[resolved].byteCount == n );
ENSURE( memcmp( seeded.entries[resolved].bytes, slot0, (size_t)n ) == 0 );
ENSURE( seeded.entries.data[resolved].byteCount == n );
ENSURE( memcmp( seeded.entries.data[resolved].bytes, slot0, (size_t)n ) == 0 );
b3FreeRegistry( &seeded );
return 0;
@ -1924,6 +1936,147 @@ static int ShapeNameReplay( void )
return 0;
}
// A box sliding across a mesh floor, with the option to swap both geometries and retune a
// per-triangle material part way through. Returns the final state hash so the caller can prove the
// mutations move the simulation. Recording is optional so the same scene serves as the control.
static uint64_t RunGeometryMutatorScene( b3Recording* rec, bool mutate, const b3MeshData* meshA, const b3MeshData* meshB,
const b3HullData* swapHull, float swapFriction )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.workerCount = 1;
b3WorldId worldId = b3CreateWorld( &worldDef );
if ( rec != NULL )
{
b3World_StartRecording( worldId, rec );
}
// The mesh body is created first so its ordinal is stable for the read back after replay.
b3SurfaceMaterial meshMaterials[2] = { b3DefaultSurfaceMaterial(), b3DefaultSurfaceMaterial() };
meshMaterials[1].friction = 0.05f;
b3BodyDef meshBodyDef = b3DefaultBodyDef();
meshBodyDef.type = b3_staticBody;
b3BodyId meshBodyId = b3CreateBody( worldId, &meshBodyDef );
b3ShapeDef meshShapeDef = b3DefaultShapeDef();
meshShapeDef.materials = meshMaterials;
meshShapeDef.materialCount = 2;
b3ShapeId meshShapeId = b3CreateMeshShape( meshBodyId, &meshShapeDef, meshA, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
b3BodyDef boxBodyDef = b3DefaultBodyDef();
boxBodyDef.type = b3_dynamicBody;
boxBodyDef.position = (b3Pos){ -2.0f, 2.0f, 0.0f };
boxBodyDef.linearVelocity = (b3Vec3){ 4.0f, 0.0f, 0.0f };
b3BodyId boxBodyId = b3CreateBody( worldId, &boxBodyDef );
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
b3ShapeDef boxShapeDef = b3DefaultShapeDef();
boxShapeDef.density = 1.0f;
b3ShapeId boxShapeId = b3CreateHullShape( boxBodyId, &boxShapeDef, &box.base );
float timeStep = 1.0f / 60.0f;
for ( int i = 0; i < 10; ++i )
{
b3World_Step( worldId, timeStep, 4 );
}
if ( mutate )
{
b3Shape_SetHull( boxShapeId, swapHull );
b3Shape_SetMesh( meshShapeId, meshB, (b3Vec3){ 1.0f, 1.0f, 1.0f } );
b3SurfaceMaterial grippy = b3DefaultSurfaceMaterial();
grippy.friction = swapFriction;
b3Shape_SetMeshMaterial( meshShapeId, grippy, 1 );
}
for ( int i = 0; i < 30; ++i )
{
b3World_Step( worldId, timeStep, 4 );
}
uint64_t hash = b3HashWorldState( b3GetWorldFromId( worldId ) );
if ( rec != NULL )
{
b3World_StopRecording( worldId );
}
b3DestroyWorld( worldId );
return hash;
}
// Swapping a shape's hull or mesh, or retuning one of its per-triangle materials, is a world
// mutation like any other and has to ride the stream. The geometry pair interns into the registry
// at the record site so replay rebuilds the same shape instead of running on the geometry it was
// created with. The control run proves the mutations move the simulation, so the state hash gate
// has teeth, and the read back covers each op on its own where dynamics alone would not.
static int GeometryMutatorReplay( void )
{
// Two flat floors with different triangulations, both carrying two material slots so the
// material index stays live across the swap.
b3MeshData* meshA = b3CreateGridMesh( 8, 8, 2.0f, 2, false );
ENSURE( meshA != NULL );
b3MeshData* meshB = b3CreateGridMesh( 12, 12, 1.5f, 2, false );
ENSURE( meshB != NULL );
ENSURE( meshA->triangleCount != meshB->triangleCount );
b3BoxHull swapHull = b3MakeBoxHull( 0.25f, 1.5f, 0.25f );
const float swapFriction = 0.95f;
uint64_t controlHash = RunGeometryMutatorScene( NULL, false, meshA, meshB, &swapHull.base, swapFriction );
b3Recording* rec = b3CreateRecording( 0 );
ENSURE( rec != NULL );
uint64_t mutatedHash = RunGeometryMutatorScene( rec, true, meshA, meshB, &swapHull.base, swapFriction );
// Without this the replay gate below could pass on a recording that never carried the ops.
ENSURE( mutatedHash != controlHash );
const uint8_t* data = b3Recording_GetData( rec );
int size = b3Recording_GetSize( rec );
ENSURE( size > 0 );
ENSURE( b3ValidateReplay( data, size, 1 ) );
ENSURE( b3ValidateReplay( data, size, 4 ) );
b3RecPlayer* player = b3RecPlayer_Create( data, size, 1 );
ENSURE( player != NULL );
while ( b3RecPlayer_StepFrame( player ) )
{
}
ENSURE( b3RecPlayer_HasDiverged( player ) == false );
// Body ordinals follow creation order in the replayed world.
b3BodyId replayMeshBody = b3RecPlayer_GetBodyId( player, 0 );
b3BodyId replayBoxBody = b3RecPlayer_GetBodyId( player, 1 );
ENSURE( b3Body_IsValid( replayMeshBody ) && b3Body_IsValid( replayBoxBody ) );
b3ShapeId replayMeshShape;
ENSURE( b3Body_GetShapes( replayMeshBody, &replayMeshShape, 1 ) == 1 );
b3ShapeId replayBoxShape;
ENSURE( b3Body_GetShapes( replayBoxBody, &replayBoxShape, 1 ) == 1 );
const b3HullData* replayHull = b3Shape_GetHull( replayBoxShape );
ENSURE( replayHull != NULL );
ENSURE( replayHull->hash == swapHull.base.hash );
b3Mesh replayMesh = b3Shape_GetMesh( replayMeshShape );
ENSURE( replayMesh.data != NULL );
ENSURE( replayMesh.data->hash == meshB->hash );
ENSURE( replayMesh.data->triangleCount == meshB->triangleCount );
b3SurfaceMaterial replayMaterial = b3Shape_GetMeshSurfaceMaterial( replayMeshShape, 1 );
ENSURE( replayMaterial.friction == swapFriction );
b3RecPlayer_Destroy( player );
b3DestroyRecording( rec );
b3DestroyMesh( meshA );
b3DestroyMesh( meshB );
return 0;
}
int RecordingTest( void )
{
RUN_SUBTEST( GeometryHashCollision );
@ -1942,6 +2095,7 @@ int RecordingTest( void )
RUN_SUBTEST( QueryReplay );
RUN_SUBTEST( TaggedQuery );
RUN_SUBTEST( TransformedHullRoundTrip );
RUN_SUBTEST( GeometryMutatorReplay );
RUN_SUBTEST( AllOps );
RUN_SUBTEST( ReservedHeaderBytes );
return 0;