Draw sleep velocity (#49)

- draw sleep velocity for debugging
- added back shape names with B3_SHAPE_NAME_LENGTH
- zup command line option
This commit is contained in:
Erin Catto 2026-07-05 00:33:00 -07:00 • committed by GitHub
commit 540ea387b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 554 additions and 132 deletions

View file

@ -1053,6 +1053,8 @@ static int AllOps( void )
b3ShapeDef sphereShapeDef = b3DefaultShapeDef();
sphereShapeDef.density = 1.0f;
// Over-length shape name so replay exercises the clamp in the shape def reader, like the body above.
sphereShapeDef.name = "sphereNameThatExceedsTheLimit";
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3ShapeId sphereShapeId = b3CreateSphereShape( bodyId, &sphereShapeDef, &sphere );
ENSURE( b3Shape_IsValid( sphereShapeId ) );
@ -1159,7 +1161,7 @@ static int AllOps( void )
// Shape mutators: SetFriction, SetRestitution, SetDensity, SetSurfaceMaterial, SetFilter,
// EnableSensorEvents, EnableContactEvents, EnableHitEvents, EnablePreSolveEvents, ApplyWind,
// SetSphere, SetCapsule
// SetSphere, SetCapsule, SetName
b3Shape_SetFriction( boxShapeId, 0.3f );
b3Shape_SetRestitution( capsuleShapeId, 0.5f );
b3Shape_SetDensity( boxShapeId, 3.0f, true );
@ -1179,6 +1181,7 @@ static int AllOps( void )
b3Shape_SetSphere( sphereShapeId, &newSphere );
b3Capsule newCapsule = { { 0.0f, -0.3f, 0.0f }, { 0.0f, 0.3f, 0.0f }, 0.3f };
b3Shape_SetCapsule( capsuleShapeId, &newCapsule );
b3Shape_SetName( boxShapeId, "box" );
// Body mutators: SetTransform, SetLinearVelocity/AngularVelocity (Vec3), SetName,
// damping, gravity scale, sleep threshold, SetAwake, EnableSleep, SetBullet, SetMotionLocks,
@ -1849,9 +1852,97 @@ static int StagedStepCreationPose( void )
return 0;
}
// Shape names are debug only and do not feed the determinism hash, so b3ValidateReplay cannot catch a
// broken name round-trip. Replay through the player and read the names back to prove the def field and
// the ShapeSetName op survive serialization. Holds for any B3_SHAPE_NAME_LENGTH, including 0.
static int ShapeNameReplay( void )
{
// Replay must reproduce exactly what the shape stores: its source truncated to B3_SHAPE_NAME_LENGTH.
// Asserting the shape cap rather than the body cap also traps the shared string reader, which clamps
// to the body length, from silently shortening shape names if the two caps ever cross.
const char* names[3] = {
"def", // carried on the shape def, survives whole
"set", // applied after create through the setter op
"abcdefghijklmnopqrstuvwxyz", // over-length, forces the clamp
};
b3Recording* rec = b3CreateRecording( 0 );
ENSURE( rec != NULL );
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3World_StartRecording( worldId, rec );
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
// One shape per body so read-back never depends on per-body shape ordering.
for ( int i = 0; i < 3; ++i )
{
b3BodyDef bd = b3DefaultBodyDef();
bd.type = b3_dynamicBody;
bd.position = (b3Pos){ 3.0f * (float)i, 5.0f, 0.0f };
b3BodyId body = b3CreateBody( worldId, &bd );
b3ShapeDef sd = b3DefaultShapeDef();
sd.density = 1.0f;
if ( i != 1 )
{
sd.name = names[i];
}
b3ShapeId shape = b3CreateSphereShape( body, &sd, &sphere );
if ( i == 1 )
{
b3Shape_SetName( shape, names[i] );
}
}
for ( int i = 0; i < 4; ++i )
{
b3World_Step( worldId, 1.0f / 60.0f, 4 );
}
b3World_StopRecording( worldId );
b3DestroyWorld( worldId );
const uint8_t* data = b3Recording_GetData( rec );
int sz = b3Recording_GetSize( rec );
b3RecPlayer* player = b3RecPlayer_Create( data, sz, 1 );
ENSURE( player != NULL );
while ( b3RecPlayer_StepFrame( player ) )
{
}
ENSURE( b3RecPlayer_HasDiverged( player ) == false );
// Body ordinals follow creation order in the replayed world.
for ( int i = 0; i < 3; ++i )
{
b3BodyId body = b3RecPlayer_GetBodyId( player, i );
ENSURE( b3Body_IsValid( body ) );
b3ShapeId shape;
ENSURE( b3Body_GetShapes( body, &shape, 1 ) == 1 );
const char* got = b3Shape_GetName( shape );
ENSURE( got != NULL );
int srcLen = (int)strlen( names[i] );
int expectLen = srcLen < B3_SHAPE_NAME_LENGTH ? srcLen : B3_SHAPE_NAME_LENGTH;
ENSURE( (int)strlen( got ) == expectLen );
if ( expectLen > 0 )
{
ENSURE( strncmp( got, names[i], (size_t)expectLen ) == 0 );
}
}
b3RecPlayer_Destroy( player );
b3DestroyRecording( rec );
return 0;
}
int RecordingTest( void )
{
RUN_SUBTEST( GeometryHashCollision );
RUN_SUBTEST( ShapeNameReplay );
RUN_SUBTEST( SphereRoundTrip );
RUN_SUBTEST( EmptyWorldRoundTrip );
RUN_SUBTEST( HullDedup );

View file

@ -3,11 +3,13 @@
#include "test_macros.h"
#include "box3d/box3d.h"
#include "box3d/collision.h"
#include "box3d/math_functions.h"
#include <float.h>
#include <math.h>
#include <string.h>
static b3Capsule capsule = { { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, 1.0f };
static b3Sphere sphere = { { 1.0f, 0.0f, 0.0f }, 1.0f };
@ -852,12 +854,130 @@ static int RayCastShapeTest( void )
return 0;
}
// The shape stores its name inline, capped at B3_SHAPE_NAME_LENGTH with a reserved null. The stored value
// is the source truncated to the cap, so derive the expectation from the cap and stay valid at any length.
static int CheckShapeName( b3ShapeId shapeId, const char* expected )
{
const char* got = b3Shape_GetName( shapeId );
ENSURE( got != NULL );
int srcLen = expected != NULL ? (int)strlen( expected ) : 0;
int expectLen = srcLen < B3_SHAPE_NAME_LENGTH ? srcLen : B3_SHAPE_NAME_LENGTH;
ENSURE( (int)strlen( got ) == expectLen );
if ( expectLen > 0 )
{
ENSURE( strncmp( got, expected, (size_t)expectLen ) == 0 );
}
return 0;
}
// Cover the def path, the setter, over-length truncation, and the NULL clear. A zero cap collapses every
// case to the empty string, which the length-derived check handles without a special branch.
static int ShapeNameTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 0.5f };
// Default def leaves the name empty.
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
b3ShapeId shapeId = b3CreateSphereShape( bodyId, &shapeDef, &s );
if ( CheckShapeName( shapeId, "" ) )
return 1;
// Name carried on the def.
shapeDef.name = "box";
b3ShapeId namedId = b3CreateSphereShape( bodyId, &shapeDef, &s );
if ( CheckShapeName( namedId, "box" ) )
return 1;
// Setter overwrites.
b3Shape_SetName( shapeId, "wheel" );
if ( CheckShapeName( shapeId, "wheel" ) )
return 1;
// Over-length name truncates to the cap.
b3Shape_SetName( shapeId, "abcdefghijklmnopqrstuvwxyz" );
if ( CheckShapeName( shapeId, "abcdefghijklmnopqrstuvwxyz" ) )
return 1;
// NULL clears the name.
b3Shape_SetName( shapeId, NULL );
if ( CheckShapeName( shapeId, "" ) )
return 1;
b3DestroyWorld( worldId );
return 0;
}
// The event enables moved from separate bools to a shared bit field. Each accessor must touch only its own
// bit, so start with all set and flip them one at a time, checking the rest hold.
static int ShapeFlagsTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
b3Sphere s = { { 0.0f, 0.0f, 0.0f }, 0.5f };
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
shapeDef.enableSensorEvents = true;
shapeDef.enableContactEvents = true;
shapeDef.enableHitEvents = true;
shapeDef.enablePreSolveEvents = true;
b3ShapeId shapeId = b3CreateSphereShape( bodyId, &shapeDef, &s );
// The def carries every enable through to the shape.
ENSURE( b3Shape_AreSensorEventsEnabled( shapeId ) );
ENSURE( b3Shape_AreContactEventsEnabled( shapeId ) );
ENSURE( b3Shape_AreHitEventsEnabled( shapeId ) );
ENSURE( b3Shape_ArePreSolveEventsEnabled( shapeId ) );
// Clearing one leaves the rest set.
b3Shape_EnableSensorEvents( shapeId, false );
ENSURE( b3Shape_AreSensorEventsEnabled( shapeId ) == false );
ENSURE( b3Shape_AreContactEventsEnabled( shapeId ) );
ENSURE( b3Shape_AreHitEventsEnabled( shapeId ) );
ENSURE( b3Shape_ArePreSolveEventsEnabled( shapeId ) );
// Setting it back does not disturb the others.
b3Shape_EnableSensorEvents( shapeId, true );
b3Shape_EnableHitEvents( shapeId, false );
ENSURE( b3Shape_AreSensorEventsEnabled( shapeId ) );
ENSURE( b3Shape_AreContactEventsEnabled( shapeId ) );
ENSURE( b3Shape_AreHitEventsEnabled( shapeId ) == false );
ENSURE( b3Shape_ArePreSolveEventsEnabled( shapeId ) );
// Clear two more and confirm only the remaining bit stays set.
b3Shape_EnableContactEvents( shapeId, false );
b3Shape_EnablePreSolveEvents( shapeId, false );
ENSURE( b3Shape_AreSensorEventsEnabled( shapeId ) );
ENSURE( b3Shape_AreContactEventsEnabled( shapeId ) == false );
ENSURE( b3Shape_AreHitEventsEnabled( shapeId ) == false );
ENSURE( b3Shape_ArePreSolveEventsEnabled( shapeId ) == false );
b3DestroyWorld( worldId );
return 0;
}
int ShapeTest( void )
{
box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
RUN_SUBTEST( ShapeMassTest );
RUN_SUBTEST( ShapeAABBTest );
RUN_SUBTEST( ShapeNameTest );
RUN_SUBTEST( ShapeFlagsTest );
// RUN_SUBTEST( PointInShapeTest );
RUN_SUBTEST( RayCastShapeTest );