testing and tolerance refinement

This commit is contained in:
Erin Catto 2026-10-15 17:54:15 -07:00
commit 9b593c2d72
10 changed files with 168 additions and 5 deletions

View file

@ -214,6 +214,12 @@ B3_API void b3World_SetMaximumLinearSpeed( b3WorldId worldId, float maximumLinea
/// Get the maximum linear speed. Usually in m/s.
B3_API float b3World_GetMaximumLinearSpeed( b3WorldId worldId );
// Set the tolerance for gyroscopic torque. [0, 1]
B3_API void b3World_SetIsotropicInertiaTolerance( b3WorldId worldId, float tolerance );
// Get the tolerance for gyroscopic torque. [0, 1]
B3_API float b3World_GetIsotropicInertiaTolerance( b3WorldId worldId );
/// Enable/disable constraint warm starting. Advanced feature for testing. Disabling
/// warm starting greatly reduces stability and provides no performance gain.
B3_API void b3World_EnableWarmStarting( b3WorldId worldId, bool flag );

View file

@ -174,8 +174,7 @@ typedef struct b3WorldDef
float maximumLinearSpeed;
/// Use this to disable gyroscopic torque on sphere-like objects automatically.
/// A value of 0.2 means up to 20% non-isotropic inertia values are treated as
/// isotropic.
/// Range is [0, 1]. A sphere is 0 and a 1x1x2 box is 0.5.
float isotropicInertiaTolerance;
/// Optional mixing callback for friction. The default uses sqrt(frictionA * frictionB).

View file

@ -583,6 +583,109 @@ public:
static int sampleGyroscopicPrecession = RegisterSample( "Bodies", "Gyroscopic Precession", GyroscopicPrecession::Create );
// This show that gyroscopic torque is necessary for plausible behavior on long skinny objects.
class SpinningCapsule : public Sample
{
public:
explicit SpinningCapsule( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 30.0f, 25.0f, 15.0f, { 0.0f, 1.0f, 0.0f } );
}
m_length = 4.0f;
m_tolerance = b3World_GetIsotropicInertiaTolerance( m_worldId );
CreateScene();
}
void CreateScene()
{
// The tolerance only applies to bodies created after it is set
b3Capacity capacity = {};
CreateWorld( &capacity );
b3World_SetIsotropicInertiaTolerance( m_worldId, m_tolerance );
AddGroundBox( 20.0f );
float radius = 0.2f;
float halfLength = 0.5f * m_length;
b3Capsule capsule = { { 0.0f, -halfLength, 0.0f }, { 0.0f, halfLength, 0.0f }, radius };
// Stand on the lower cap, tilted about it
b3Quat rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 15.0f * B3_PI / 180.0f );
b3Vec3 axis = b3RotateVector( rotation, b3Vec3_axisY );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = b3OffsetPos( { 0.0f, radius, 0.0f }, halfLength * axis );
bodyDef.rotation = rotation;
bodyDef.angularVelocity = 20.0f * axis;
m_capsuleId = b3CreateBody( m_worldId, &bodyDef );
b3ShapeDef shapeDef = b3DefaultShapeDef();
//shapeDef.baseMaterial.rollingResistance = 0.0f;
//shapeDef.baseMaterial.friction = 0.2f;
b3CreateCapsuleShape( m_capsuleId, &shapeDef, &capsule );
}
bool DrawControls() override
{
bool changed = false;
ImGui::PushItemWidth( 10.0f * ImGui::GetFontSize() );
changed |= ImGui::SliderFloat( "Length", &m_length, 0.0f, 8.0f, "%.1f" );
changed |= ImGui::SliderFloat( "Tolerance", &m_tolerance, 0.0f, 1.0f, "%.3f" );
ImGui::PopItemWidth();
if ( ImGui::Button( "Reset" ) || changed )
{
CreateScene();
}
return true;
}
void Step() override
{
Sample::Step();
// Same measure the world uses to skip the gyroscopic solve: the deviatoric part of the inertia
// relative to its mean.
b3Matrix3 inertia = b3Body_GetLocalRotationalInertia( m_capsuleId );
float mean = ( inertia.cx.x + inertia.cy.y + inertia.cz.z ) / 3.0f;
float dx = inertia.cx.x - mean;
float dy = inertia.cy.y - mean;
float dz = inertia.cz.z - mean;
float offDiagonal = inertia.cy.x * inertia.cy.x + inertia.cz.x * inertia.cz.x + inertia.cz.y * inertia.cz.y;
float deviation = dx * dx + dy * dy + dz * dz + 2.0f * offDiagonal;
float anisotropy = mean > 0.0f ? sqrtf( 2.0f * deviation / 3.0f ) / mean : 0.0f;
bool isGyroscopic = anisotropy > m_tolerance;
b3Pos center = b3Body_GetWorldCenter( m_capsuleId );
b3Vec3 axis = b3RotateVector( b3Body_GetRotation( m_capsuleId ), b3Vec3_axisY );
b3Vec3 omega = b3Body_GetAngularVelocity( m_capsuleId );
float tilt = acosf( b3ClampFloat( b3AbsFloat( axis.y ), 0.0f, 1.0f ) );
DrawLine( center, b3OffsetPos( center, 0.2f * omega ), MakeColor( b3_colorYellow ) );
DrawTextLine( "anisotropy %.2f, gyroscopic torque %s", anisotropy, isGyroscopic ? "on" : "off" );
DrawTextLine( "tilt %.0f deg, spin %.1f rad/s", ( 180.0f / B3_PI ) * tilt, b3Dot( omega, axis ) );
}
static Sample* Create( SampleContext* context )
{
return new SpinningCapsule( context );
}
b3BodyId m_capsuleId;
float m_length;
float m_tolerance;
};
static int sampleSpinningCapsule = RegisterSample( "Bodies", "Spinning Capsule", SpinningCapsule::Create );
class Weeble : public Sample
{
public:

View file

@ -901,10 +901,14 @@ static void b3UpdateIsotropicInertia( b3World* world, b3Body* body )
float dy = inertia.cy.y - mean;
float dz = inertia.cz.z - mean;
float offDiagonal = inertia.cy.x * inertia.cy.x + inertia.cz.x * inertia.cz.x + inertia.cz.y * inertia.cz.y;
// This is the square of the Frobenius matrix norm.
// https://en.wikipedia.org/wiki/Matrix_norm
float deviation = dx * dx + dy * dy + dz * dz + 2.0f * offDiagonal;
float tolerance = world->isotropicInertiaTolerance;
if ( 2.0f * deviation <= tolerance * tolerance * mean * mean )
if ( 2.0f * deviation <= 3.0f * tolerance * tolerance * mean * mean )
{
body->flags |= b3_isotropicInertia;
}

View file

@ -332,7 +332,7 @@ b3WorldId b3CreateWorld( const b3WorldDef* def )
world->restitutionIterations = b3ClampInt( def->restitutionIterations, 0, B3_MAX_RESTITUTION_ITERATIONS );
world->enableRestitutionPropagation = def->enableRestitutionPropagation;
world->maxLinearSpeed = def->maximumLinearSpeed;
world->isotropicInertiaTolerance = b3MaxFloat( def->isotropicInertiaTolerance, 0.0f );
world->isotropicInertiaTolerance = b3ClampFloat( def->isotropicInertiaTolerance, 0.0f, 1.0f );
world->contactSpeed = def->contactSpeed;
world->contactHertz = def->contactHertz;
world->contactDampingRatio = def->contactDampingRatio;
@ -2216,6 +2216,27 @@ float b3World_GetMaximumLinearSpeed( b3WorldId worldId )
return world->maxLinearSpeed;
}
void b3World_SetIsotropicInertiaTolerance( b3WorldId worldId, float tolerance )
{
B3_ASSERT( b3IsValidFloat( tolerance ) );
b3World* world = b3GetUnlockedWorldFromId( worldId );
if ( world == NULL )
{
return;
}
B3_REC( world, WorldSetIsotropicInertiaTolerance, worldId, tolerance );
world->isotropicInertiaTolerance = b3ClampFloat( tolerance, 0.0f, 1.0f );
}
float b3World_GetIsotropicInertiaTolerance( b3WorldId worldId )
{
b3World* world = b3GetWorldFromId( worldId );
return world->isotropicInertiaTolerance;
}
b3Profile b3World_GetProfile( b3WorldId worldId )
{
b3World* world = b3GetUnlockedWorldFromId( worldId );

View file

@ -52,7 +52,7 @@ typedef struct b3World b3World;
// Minor tracks op-stream additions that keep the 48 byte header shape.
// Minor version 7 made the contact body sim locators persistent
#define B3_REC_VERSION_MINOR 8
#define B3_REC_VERSION_MINOR 9
// File header, fixed 48 bytes. Contains the registry locator so the player
// can load geometry before replaying any ops.

View file

@ -40,6 +40,7 @@ B3_REC_OP( 0x0C, WorldRebuildStaticTree, RET_NONE, ARG( WORLDID, world ) )
B3_REC_OP( 0x0D, WorldEnableSpeculative, RET_NONE, ARG( WORLDID, world ) ARG( BOOL, flag ) )
B3_REC_OP( 0x0E, WorldSetRestitutionIterations, RET_NONE, ARG( WORLDID, world ) ARG( I32, iterations ) )
B3_REC_OP( 0x0F, WorldEnableRestitutionPropagation, RET_NONE, ARG( WORLDID, world ) ARG( BOOL, flag ) )
B3_REC_OP( 0x70, WorldSetIsotropicInertiaTolerance, RET_NONE, ARG( WORLDID, world ) ARG( F32, tolerance ) )
// Body
B3_REC_OP( 0x10, CreateBody, RET_BODYID, ARG( WORLDID, world ) ARG( BODYDEF, def ) )

View file

@ -806,6 +806,12 @@ static void b3RecDispatch_WorldEnableRestitutionPropagation( const b3RecArgs_Wor
b3World_EnableRestitutionPropagation( rdr->replayWorldId, a->flag );
}
static void b3RecDispatch_WorldSetIsotropicInertiaTolerance( const b3RecArgs_WorldSetIsotropicInertiaTolerance* a,
b3RecReader* rdr )
{
b3World_SetIsotropicInertiaTolerance( rdr->replayWorldId, a->tolerance );
}
static void b3RecDispatch_CreateBody( const b3RecArgs_CreateBody* a, b3RecReader* rdr )
{
b3BodyId recId = b3RecR_BODYID( rdr );

View file

@ -1584,6 +1584,21 @@ static int AllOps( void )
b3World_SetMaximumLinearSpeed( worldId, 100.0f );
b3World_RebuildStaticTree( worldId );
// The tolerance only reaches bodies whose mass is computed afterwards. A free spinning rod made
// after the change loses its gyroscopic torque, so replay diverges if the op goes missing.
b3World_SetIsotropicInertiaTolerance( worldId, 1.0f );
{
b3BodyDef rodDef = b3DefaultBodyDef();
rodDef.type = b3_dynamicBody;
rodDef.position = (b3Pos){ 40.0f, 10.0f, 0.0f };
rodDef.gravityScale = 0.0f;
rodDef.angularVelocity = (b3Vec3){ 1.0f, 5.0f, 0.5f };
b3BodyId rodId = b3CreateBody( worldId, &rodDef );
b3BoxHull rod = b3MakeBoxHull( 0.1f, 1.0f, 0.1f );
b3ShapeDef rodShapeDef = b3DefaultShapeDef();
b3CreateHullShape( rodId, &rodShapeDef, &rod.base );
}
b3ExplosionDef explosion = b3DefaultExplosionDef();
explosion.position = (b3Pos){ 0.0f, 5.0f, 0.0f };
explosion.radius = 3.0f;

View file

@ -315,6 +315,14 @@ int TestWorldCoverage( void )
value = b3World_GetMaximumLinearSpeed( worldId );
ENSURE( value == 10.0f );
b3World_SetIsotropicInertiaTolerance( worldId, 0.5f );
value = b3World_GetIsotropicInertiaTolerance( worldId );
ENSURE( value == 0.5f );
b3World_SetIsotropicInertiaTolerance( worldId, 2.0f );
value = b3World_GetIsotropicInertiaTolerance( worldId );
ENSURE( value == 1.0f );
b3World_EnableWarmStarting( worldId, true );
flag = b3World_IsWarmStartingEnabled( worldId );
ENSURE( flag == true );