box3d/test/test_determinism.c
Erin Catto dfa5e6add0
Fixing issues (#94)
- option to draw kinematic shapes with transparency
- slight change to triangle vs hull to reduce ghost collisions
- changed return of b3DebugDraw.DrawShapeFcn to void
#66 
#48 
#69 
#75 
#76
2026-07-19 21:43:43 -07:00

295 lines
7.1 KiB
C

// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#include "box3d/box3d.h"
#include "determinism.h"
#include "stability.h"
#include "test_macros.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef BOX3D_PROFILE
#include <tracy/TracyC.h>
#else
#define TracyCFrameMark
#endif
// Double precision accumulates body positions in double, so the settle/sleep step and the
// state hash differ from the float build. Both modes are internally deterministic.
#if defined( BOX3D_DOUBLE_PRECISION )
#define RAGDOLL_SLEEP_STEP 297
#define RAGDOLL_HASH 0x27FF38C1
#define WAVE_PILE_SLEEP_STEP 258
#define WAVE_PILE_HASH 0xA3CDC61B
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0x1737F5BC
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0x31F090DC
#define MESH_DROP_SLEEP_STEP 251
#define MESH_DROP_HASH 0x465381C5
#else
#define RAGDOLL_SLEEP_STEP 308
#define RAGDOLL_HASH 0x1E5EDD79
#define WAVE_PILE_SLEEP_STEP 239
#define WAVE_PILE_HASH 0xA2F4D472
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0xB9F993A5
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0xB9D3863D
#define MESH_DROP_SLEEP_STEP 251
#define MESH_DROP_HASH 0xE58C7240
#endif
static int SingleMultithreadingTest( int workerCount )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.workerCount = workerCount;
b3WorldId worldId = b3CreateWorld( &worldDef );
FallingRagdollData data = CreateFallingRagdolls( worldId );
float timeStep = 1.0f / 60.0f;
int stepLimit = 500;
for ( int i = 0; i < stepLimit; ++i )
{
int subStepCount = 4;
b3World_Step( worldId, timeStep, subStepCount );
TracyCFrameMark;
bool done = UpdateFallingRagdolls( worldId, &data );
if ( done )
{
break;
}
}
b3DestroyWorld( worldId );
if ( data.sleepStep != RAGDOLL_SLEEP_STEP || data.hash != RAGDOLL_HASH )
{
printf( " workers=%d sleepStep=%d hash=0x%08X\n", workerCount, data.sleepStep, data.hash );
}
ENSURE( data.sleepStep == RAGDOLL_SLEEP_STEP );
ENSURE( data.hash == RAGDOLL_HASH );
DestroyFallingRagdolls( &data );
return 0;
}
// Test multithreaded determinism.
static int MultithreadingTest( void )
{
for ( int workerCount = 1; workerCount < 6; ++workerCount )
{
int result = SingleMultithreadingTest( workerCount );
ENSURE( result == 0 );
}
return 0;
}
// Test cross platform determinism.
static int CrossPlatformTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
FallingRagdollData data = CreateFallingRagdolls( worldId );
float timeStep = 1.0f / 60.0f;
bool done = false;
while ( done == false )
{
int subStepCount = 4;
b3World_Step( worldId, timeStep, subStepCount );
TracyCFrameMark;
done = UpdateFallingRagdolls( worldId, &data );
}
if ( data.sleepStep != RAGDOLL_SLEEP_STEP || data.hash != RAGDOLL_HASH )
{
printf( " cross-platform sleepStep=%d hash=0x%08X\n", data.sleepStep, data.hash );
}
ENSURE( data.sleepStep == RAGDOLL_SLEEP_STEP );
ENSURE( data.hash == RAGDOLL_HASH );
DestroyFallingRagdolls( &data );
b3DestroyWorld( worldId );
return 0;
}
static int SingleWavePileTest( int workerCount )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.workerCount = workerCount;
b3WorldId worldId = b3CreateWorld( &worldDef );
WavePileData data = CreateWavePile( worldId );
float timeStep = 1.0f / 60.0f;
// Rolling resistance must put the pile to sleep within 500 steps
bool done = false;
for ( int i = 0; i < 500 && done == false; ++i )
{
int subStepCount = 4;
b3World_Step( worldId, timeStep, subStepCount );
TracyCFrameMark;
done = UpdateWavePile( worldId, &data );
}
b3DestroyWorld( worldId );
if ( data.sleepStep != WAVE_PILE_SLEEP_STEP || data.hash != WAVE_PILE_HASH )
{
printf( " wave pile workers=%d sleepStep=%d hash=0x%08X\n", workerCount, data.sleepStep, data.hash );
}
ENSURE( done == true );
ENSURE( data.sleepStep == WAVE_PILE_SLEEP_STEP );
ENSURE( data.hash == WAVE_PILE_HASH );
DestroyWavePile( &data );
return 0;
}
// Test multithreaded determinism of a mixed convex pile on a wave height field.
static int WavePileTest( void )
{
for ( int workerCount = 1; workerCount <= 4; ++workerCount )
{
int result = SingleWavePileTest( workerCount );
ENSURE( result == 0 );
}
return 0;
}
static int SingleQuerySpawnTest( int workerCount )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.workerCount = workerCount;
b3WorldId worldId = b3CreateWorld( &worldDef );
QuerySpawnData data = CreateQuerySpawn( worldId );
float timeStep = 1.0f / 60.0f;
bool done = false;
for ( int i = 0; i < 1000 && done == false; ++i )
{
int subStepCount = 4;
b3World_Step( worldId, timeStep, subStepCount );
TracyCFrameMark;
done = UpdateQuerySpawn( worldId, &data );
}
b3DestroyWorld( worldId );
if ( data.sleepStep != QUERY_SPAWN_SLEEP_STEP || data.hash != QUERY_SPAWN_HASH || data.queryHitCount != QUERY_SPAWN_HIT_COUNT ||
data.queryHash != QUERY_SPAWN_QUERY_HASH )
{
printf( " query spawn workers=%d sleepStep=%d hash=0x%08X hits=%d queryHash=0x%08X\n", workerCount, data.sleepStep,
data.hash, data.queryHitCount, data.queryHash );
}
ENSURE( done == true );
ENSURE( data.spawnCount == QUERY_SPAWN_COUNT );
ENSURE( data.sleepStep == QUERY_SPAWN_SLEEP_STEP );
ENSURE( data.hash == QUERY_SPAWN_HASH );
ENSURE( data.queryHitCount == QUERY_SPAWN_HIT_COUNT );
ENSURE( data.queryHash == QUERY_SPAWN_QUERY_HASH );
DestroyQuerySpawn( &data );
return 0;
}
// Test determinism of world queries by feeding their results back into the simulation.
static int QuerySpawnTest( void )
{
for ( int workerCount = 1; workerCount <= 4; ++workerCount )
{
int result = SingleQuerySpawnTest( workerCount );
ENSURE( result == 0 );
}
return 0;
}
static int SingleMeshDropTest( int workerCount )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.workerCount = workerCount;
b3WorldId worldId = b3CreateWorld( &worldDef );
MeshDropData data = CreateMeshDrop( worldId, b3Pos_zero );
float timeStep = 1.0f / 60.0f;
bool done = false;
for ( int i = 0; i < 400 && done == false; ++i )
{
int subStepCount = 4;
b3World_Step( worldId, timeStep, subStepCount );
TracyCFrameMark;
done = UpdateMeshDrop( worldId, &data );
}
b3DestroyWorld( worldId );
if ( data.sleepStep != MESH_DROP_SLEEP_STEP || data.hash != MESH_DROP_HASH )
{
printf( " mesh drop workers=%d sleepStep=%d hash=0x%08X\n", workerCount, data.sleepStep, data.hash );
}
ENSURE( done == true );
ENSURE( data.sleepStep == MESH_DROP_SLEEP_STEP );
ENSURE( data.hash == MESH_DROP_HASH );
DestroyMeshDrop( &data );
return 0;
}
// Test continuous collision determinism. Thin fast boxes need CCD against the wave mesh.
// The scene is large, so only the single threaded and widest schedules run.
static int MeshDropTest( void )
{
int workerCounts[2] = { 1, 4 };
for ( int i = 0; i < 2; ++i )
{
int result = SingleMeshDropTest( workerCounts[i] );
ENSURE( result == 0 );
}
return 0;
}
int DeterminismTest( void )
{
RUN_SUBTEST( MultithreadingTest );
RUN_SUBTEST( CrossPlatformTest );
RUN_SUBTEST( WavePileTest );
RUN_SUBTEST( QuerySpawnTest );
RUN_SUBTEST( MeshDropTest );
return 0;
}