Optimizations (#168)

- Optimize contact prepare
- Back to post restitution stage
- Optimize solver math
- Bug fixes
This commit is contained in:
Erin Catto 2026-09-24 12:21:17 -07:00 • committed by GitHub
commit 79fc55976a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
55 changed files with 3248 additions and 1519 deletions

View file

@ -17,6 +17,10 @@
#define TIME_STEP ( 1.0f / 60.0f )
#define SUB_STEP_COUNT 4
// A flat landing converges its Gauss-Seidel coupling in four stage iterations and the default is two,
// so the tests that read the coefficient or the spin of a flat landing pin the count
#define RESTITUTION_ITERATIONS 4
// Impact speed shared by every scenario that measures a coefficient. Well above the default
// restitution threshold so the bounce is always armed.
#define IMPACT_SPEED 5.0f
@ -46,6 +50,15 @@ static b3WorldId MakeWorld( float gravityY )
return b3CreateWorld( &worldDef );
}
static b3WorldId MakeWorldIterations( float gravityY, int restitutionIterations )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.gravity = (b3Vec3){ 0.0f, gravityY, 0.0f };
worldDef.enableSleep = false;
worldDef.restitutionIterations = restitutionIterations;
return b3CreateWorld( &worldDef );
}
// Ground with its top surface at y = 0
static void MakeGround( b3WorldId worldId, float restitution )
{
@ -142,9 +155,14 @@ static float MeasureGroundBounce( float restitution, float gap )
// Impactor driven onto a column of resting balls pinned against the ground. The support balls are
// dead so only the top contact can bounce. A supported target has infinite effective mass along the
// normal, so the coefficient must not depend on the column height.
static float MeasureSupportedBounce( float restitution, int supportCount )
static float MeasureSupportedBounce( float restitution, int supportCount, bool propagate )
{
b3WorldId worldId = MakeWorld( 0.0f );
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.gravity = b3Vec3_zero;
worldDef.enableSleep = false;
worldDef.enableRestitutionPropagation = propagate;
worldDef.restitutionIterations = RESTITUTION_ITERATIONS;
b3WorldId worldId = b3CreateWorld( &worldDef );
MakeGround( worldId, 0.0f );
for ( int i = 0; i < supportCount; ++i )
@ -168,7 +186,7 @@ static float MeasureSupportedBounce( float restitution, int supportCount )
// Flat box landing on all four corners at once with no gravity
static float MeasureFlatBounce( float restitution, int subStepCount, float* spin )
{
b3WorldId worldId = MakeWorld( 0.0f );
b3WorldId worldId = MakeWorldIterations( 0.0f, RESTITUTION_ITERATIONS );
MakeGround( worldId, 0.0f );
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -307,32 +325,35 @@ static int PhaseTest( void )
return failed;
}
// The bounce is solved as a constraint alongside the support contacts, so the column can supply the
// reaction. A terminal restitution pass has nothing after it to do that and the coefficient decays
// with the column height, which the tolerance is chosen to reject.
// By default the restitution stage sweeps only the contacts that carry restitution, so the ball
// pushes the column down and the ground under it gets no say until the next step: e = 0.9 gives
// about 0.52 on one support. With propagation enabled the stage sweeps every contact, the ground
// contact supplies the reaction within the stage and e = 0.9 gives 0.85 on one support and 0.72 on
// three at the pinned four iterations, decaying with depth because the column needs an iteration per
// level. Propagation costs a normal pass over every contact per iteration, hence the option. Both
// modes are gated: never above the free coefficient, which would be energy from nowhere, and never
// collapsed.
static int SupportedTest( void )
{
static const float restitutions[] = { 0.5f, 0.9f };
const float tolerance = 0.2f;
int failed = 0;
for ( int j = 0; j < ARRAY_COUNT( restitutions ); ++j )
for ( int propagate = 0; propagate <= 1; ++propagate )
{
for ( int n = 0; n <= 3; ++n )
float floor = propagate ? 0.75f : 0.5f;
for ( int j = 0; j < ARRAY_COUNT( restitutions ); ++j )
{
float measured = MeasureSupportedBounce( restitutions[j], n );
printf( " supported e %.2f supports %d -> %.4f\n", restitutions[j], n, measured );
if ( b3AbsFloat( measured - restitutions[j] ) > tolerance )
for ( int n = 0; n <= 3; ++n )
{
failed = 1;
}
float measured = MeasureSupportedBounce( restitutions[j], n, propagate == 1 );
printf( " supported e %.2f supports %d propagation %d -> %.4f\n", restitutions[j], n, propagate, measured );
// Rebounding faster than the impact is energy from nowhere, whatever the coefficient
if ( measured > 1.01f )
{
failed = 1;
if ( measured > restitutions[j] + 0.01f || measured < floor * restitutions[j] )
{
failed = 1;
}
}
}
}
@ -340,15 +361,16 @@ static int SupportedTest( void )
return failed;
}
// A symmetric four point landing. The points are solved in sequence within a relax pass and the
// bounce retires once they all separate, so a small residual spin is expected. The tolerance admits
// that residual and rejects the gross asymmetry of one point taking the whole bounce.
// A symmetric four point landing. The stage solves the points in sequence, so a single sweep leaves
// the box spinning: the first impulse tilts it and the next point sees a larger closing speed. Four
// stage iterations converge the coupling and leave a small residual. The tolerance admits that
// residual and rejects the single sweep behavior.
static int FlatLandingTest( void )
{
static const float restitutions[] = { 0.5f, 0.9f };
static const int subStepCounts[] = { 4, 8 };
const float speedTolerance = 0.1f;
const float spinTolerance = 0.25f;
const float speedTolerance = 0.01f;
const float spinTolerance = 0.03f;
int failed = 0;
@ -379,8 +401,7 @@ static int FlatLandingTest( void )
// A flat box carrying spin about a horizontal axis. All four corners stay in contact and the normal
// constraints are linear in the body velocity, so perfect restitution reverses linear and angular
// velocity. Sweeping the normal solve once per manifold point makes it exact to float precision. One
// sweep left up to 0.03 of residual in each.
// velocity. Four stage iterations bring both within a few thousandths.
static int SpinTest( void )
{
static const float spins[] = { 0.0f, 0.5f, 1.0f, 2.0f };
@ -389,7 +410,7 @@ static int SpinTest( void )
for ( int j = 0; j < ARRAY_COUNT( spins ); ++j )
{
b3WorldId worldId = MakeWorld( 0.0f );
b3WorldId worldId = MakeWorldIterations( 0.0f, RESTITUTION_ITERATIONS );
MakeGround( worldId, 0.0f );
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -418,7 +439,7 @@ static int SpinTest( void )
printf( " spin in %+.2f -> vy %+.4f (want %+.4f) wz %+.4f (want %+.4f)\n", spins[j], speed, IMPACT_SPEED, spin,
-spins[j] );
if ( b3AbsFloat( speed - IMPACT_SPEED ) > 0.25f || b3AbsFloat( spin + spins[j] ) > 0.25f )
if ( b3AbsFloat( speed - IMPACT_SPEED ) > 0.05f || b3AbsFloat( spin + spins[j] ) > 0.05f )
{
failed = 1;
}
@ -430,10 +451,10 @@ static int SpinTest( void )
// Perfectly elastic ball under gravity. Only heights where continuous collision engages are used.
// Continuous collision lands the ball on the surface, so the bounce is armed from the true impact
// speed and the apex holds. Slower drops resolve the impact inside the overlap and the penetration
// recovery adds height, by design.
// recovery adds height, by design. The 10 m case is the long horizon read.
static int DropTest( void )
{
static const float heights[] = { 40.0f, 20.0f };
static const float heights[] = { 40.0f, 20.0f, 10.0f };
int failed = 0;
@ -461,7 +482,8 @@ static int DropTest( void )
highest = b3MaxFloat( highest, apexes[i] );
}
if ( highest > 1.02f * apexes[0] || apexes[apexCount - 1] < 0.8f * apexes[0] )
// A later apex above the first, or a first apex above the drop, is energy from nowhere
if ( highest > 1.001f * apexes[0] || apexes[0] > 1.001f * heights[j] || apexes[apexCount - 1] < 0.8f * apexes[0] )
{
failed = 1;
}
@ -471,21 +493,14 @@ static int DropTest( void )
}
// A cube dropped flat with an aggressive continuous safety factor so it lands square on the surface.
// Perfectly elastic and frictionless, so it should come back to the drop height with no rotation and
// keep doing that. It now does: the four apexes sit within a centimetre of each other and the spin
// after the landing is zero to float precision.
//
// This used to be the worst subtest in the file and its history is worth keeping. The four points of
// a flat landing are solved in sequence, so the first impulse tilts the cube, the next point sees a
// larger closing speed and delivers more, and the sum overshot the rigid body answer. One sweep gave
// 0.89 rad/s of spin and a 6 percent high first apex, after which the cube tumbled and either shed
// almost all its energy (the deferred scheme collapsed to 0.71, the height of a cube rolling on its
// edges) or gained it. The normal solve is now swept once per manifold point when a bounce is armed,
// which converges the coupling: spin fell to 0.13 rad/s at two sweeps and to zero at three. The
// bounds below reject every one of those earlier behaviors.
// Perfectly elastic and frictionless, so it should come back to the drop height with no rotation. A
// flat landing solves four coplanar points in sequence and one sweep over-delivers: the first impulse
// tilts the cube, the next point sees a larger closing speed and the sum overshoots the rigid body
// answer, leaving spin that tilts the cube for the next landing. Four stage iterations converge the
// coupling. The bounds below reject the single sweep behavior and any apex above the drop.
static int SingleBoxTest( void )
{
b3WorldId worldId = MakeWorld( -10.0f );
b3WorldId worldId = MakeWorldIterations( -10.0f, RESTITUTION_ITERATIONS );
MakeGround( worldId, 0.0f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
@ -543,14 +558,14 @@ static int SingleBoxTest( void )
int failed = 0;
if ( firstSpin > 0.1f )
if ( firstSpin > 0.01f )
{
failed = 1;
}
for ( int i = 0; i < apexCount; ++i )
{
if ( apexes[i] < 0.95f * dropHeight || apexes[i] > 1.02f * dropHeight )
if ( apexes[i] < 0.95f * dropHeight || apexes[i] > 1.001f * dropHeight )
{
failed = 1;
}
@ -680,7 +695,6 @@ static uint64_t RunWorkerScene( int workerCount )
return hash;
}
// The armed bounce is per manifold point state, so it must survive the split across workers
// The manifold point approach speed is published only for contacts that opted into hit events. It
// is not a stale value otherwise, it is zero, so a reader can tell "not measured" from "measured
// zero" by whether the shape enables the events. Restitution no longer reads it at all, so this is
@ -745,33 +759,27 @@ static int NormalVelocityTest( void )
return failed;
}
// Mirrors the Restitution Overshoot sample in samples/sample_issues.cpp: a unit cube dropped flat on
// a floor narrower than itself, perfectly elastic, so the four contact points sit inboard of the box
// corners. A perfectly elastic bounce cannot come back higher than it was dropped from.
//
// Run twice, because two different effects used to be tangled together here. The impulse itself must
// never return more speed than it received, which is the restitution invariant and is checked in both
// cases. The apex is a weaker statement: without continuous collision the cube moves 0.23 m in the
// step before contact, just under the 0.25 m that would trigger it, so the first manifold appears
// with the cube already 0.10 m deep. It then bounces from down there and the soft position correction
// lifts it back to the surface for free, which is worth about a tenth of a metre of extra height and
// has nothing to do with restitution. With continuous collision the cube lands on the surface and the
// apex comes in under the drop height.
// Total mechanical energy of a body. Perfect restitution and no friction means this may only
// decrease, which is a stronger statement than any height bound: it also catches a bounce that
// manufactures spin rather than height.
// Mechanical energy of a body as symplectic Euler conserves it. The integrator updates velocity
// before position, so the quantity that is exactly constant in free flight is not kinetic plus
// potential but that plus half a sub-step of the gravity power, m * dot(v, g) * h / 2. Plain
// kinetic plus potential sampled at step boundaries jumps by about g * v * h at every bounce and
// drifts down by g^2 h^2 / 2 each sub-step, which would hide a real gain or fake one. With this
// measure a contact that never does positive work leaves the energy flat to round-off.
static float MeasureEnergy( b3BodyId bodyId, b3WorldId worldId )
{
b3MassData massData = b3Body_GetMassData( bodyId );
b3Vec3 v = b3Body_GetLinearVelocity( bodyId );
b3Vec3 gravity = b3MulSV( b3Body_GetGravityScale( bodyId ), b3World_GetGravity( worldId ) );
float h = TIME_STEP / SUB_STEP_COUNT;
// The inertia tensor is in the body frame, so bring the angular velocity back to it
b3Vec3 wLocal = b3InvRotateVector( b3Body_GetRotation( bodyId ), b3Body_GetAngularVelocity( bodyId ) );
float kinetic = 0.5f * massData.mass * b3Dot( v, v ) + 0.5f * b3Dot( wLocal, b3MulMV( massData.inertia, wLocal ) );
float potential = -massData.mass * b3Dot( b3World_GetGravity( worldId ), b3ToVec3( b3Body_GetWorldCenter( bodyId ) ) );
float potential = -massData.mass * b3Dot( gravity, b3ToVec3( b3Body_GetWorldCenter( bodyId ) ) );
float phase = 0.5f * h * massData.mass * b3Dot( v, gravity );
return kinetic + potential;
return kinetic + potential + phase;
}
typedef struct OvershootResult
@ -789,7 +797,7 @@ typedef struct OvershootResult
// gain energy.
static OvershootResult MeasureOvershoot( bool continuous )
{
b3WorldId worldId = MakeWorld( -10.0f );
b3WorldId worldId = MakeWorldIterations( -10.0f, RESTITUTION_ITERATIONS );
b3BodyDef floorDef = b3DefaultBodyDef();
floorDef.position = (b3Pos){ 0.0f, -0.25f, 0.0f };
@ -901,9 +909,13 @@ static int OvershootTest( void )
OvershootResult discrete = MeasureOvershoot( false );
OvershootResult continuous = MeasureOvershoot( true );
// Perfectly elastic returns exactly the speed it received, so any excess is round-off. The
// overflow and colored solvers round differently and either can land an ulp over one.
const float speedTolerance = 1.0e-5f;
int failed = 0;
if ( discrete.speedRatio > 1.0f || continuous.speedRatio > 1.0f )
if ( discrete.speedRatio > 1.0f + speedTolerance || continuous.speedRatio > 1.0f + speedTolerance )
{
failed = 1;
}
@ -918,7 +930,10 @@ static int OvershootTest( void )
failed = 1;
}
if ( discrete.bounceCount < 2 || continuous.bounceCount < 2 )
// The continuous landing leaves the cube sliding sideways a few tenths of a metre per second, from
// friction during the sub-steps, and a floor narrower than the cube does not catch it on the way
// back down. That is sub-step friction, not restitution, so only the first bounce is required.
if ( discrete.bounceCount < 2 || continuous.bounceCount < 1 )
{
failed = 1;
}
@ -981,7 +996,7 @@ static ImpulseResult MeasureDropImpulse( float restitution, float dropHeight )
}
}
// The sim flag is the one written this step, the body flag lags a step behind
// Transient body flags are cleared and rewritten every step
b3Body* ball = b3GetBodyFullId( world, ballId );
b3BodySim* ballSim = b3GetBodySim( world, ball );
if ( ballSim->flags & b3_hadTimeOfImpact )
@ -1029,8 +1044,8 @@ static ImpulseResult MeasureDropImpulse( float restitution, float dropHeight )
//
// The step balance is the real gate. The bounce total is bracketed as well so the restitution sweep
// means something: the contact has to reverse the approach at the coefficient and may carry the
// weight for at most as long as it lasted. The bounce retires inside the step once the point
// separates, so the ball can lose up to a step of gravity below the reversal.
// weight for at most as long as it lasted. The stage runs after the sub-steps, so the reversal
// lands a step late and the ball can come in up to a step of gravity under it.
static int ImpulseTest( void )
{
static const float restitutions[] = { 0.0f, 0.25f, 0.5f, 0.75f, 1.0f };
@ -1099,6 +1114,157 @@ static int ImpulseTest( void )
return failed;
}
typedef struct EnergyScene
{
const char* name;
float safetyFactor;
float friction;
float angle;
float spin;
float height;
float velocityY;
float halfWidth;
float halfHeight;
bool bullet;
bool stack;
int stepCount;
} EnergyScene;
// Peak energy over the run relative to the start, for every dynamic body in the scene. Perfectly
// elastic and the energy may still only go down: friction and the soft contact position correction
// dissipate, and a bounce may return at most what the contact absorbed.
static float MeasurePeakEnergy( const EnergyScene* scene )
{
b3WorldId worldId = MakeWorld( -10.0f );
b3BodyDef groundDef = b3DefaultBodyDef();
groundDef.position = (b3Pos){ 0.0f, -1.0f, 0.0f };
b3BodyId groundId = b3CreateBody( worldId, &groundDef );
b3ShapeDef groundShape = b3DefaultShapeDef();
groundShape.baseMaterial.friction = scene->friction;
b3BoxHull ground = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
b3CreateHullShape( groundId, &groundShape, &ground.base );
b3ShapeDef shapeDef = b3DefaultShapeDef();
shapeDef.density = 1.0f;
shapeDef.baseMaterial.friction = scene->friction;
shapeDef.baseMaterial.restitution = 1.0f;
b3BoxHull box = b3MakeBoxHull( scene->halfWidth, scene->halfHeight, scene->halfWidth );
b3BodyId bodyIds[4];
int bodyCount = 0;
if ( scene->stack )
{
// Resting box under the dropped one, like the TwoBoxRestitution sample
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = (b3Pos){ 0.0f, scene->halfHeight, 0.0f };
bodyDef.safetyFactor = scene->safetyFactor;
bodyIds[bodyCount] = b3CreateBody( worldId, &bodyDef );
b3CreateHullShape( bodyIds[bodyCount], &shapeDef, &box.base );
bodyCount += 1;
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.position = (b3Pos){ 0.0f, scene->height, 0.0f };
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, scene->angle );
bodyDef.angularVelocity = (b3Vec3){ 0.0f, 0.0f, scene->spin };
bodyDef.linearVelocity = (b3Vec3){ 0.0f, scene->velocityY, 0.0f };
bodyDef.safetyFactor = scene->safetyFactor;
bodyDef.isBullet = scene->bullet;
bodyIds[bodyCount] = b3CreateBody( worldId, &bodyDef );
b3CreateHullShape( bodyIds[bodyCount], &shapeDef, &box.base );
bodyCount += 1;
}
float start = 0.0f;
for ( int k = 0; k < bodyCount; ++k )
{
start += MeasureEnergy( bodyIds[k], worldId );
}
float peak = start;
int peakStep = -1;
for ( int i = 0; i < scene->stepCount; ++i )
{
b3World_Step( worldId, TIME_STEP, SUB_STEP_COUNT );
float energy = 0.0f;
for ( int k = 0; k < bodyCount; ++k )
{
energy += MeasureEnergy( bodyIds[k], worldId );
}
if ( energy > peak )
{
peak = energy;
peakStep = i;
}
}
b3DestroyWorld( worldId );
float ratio = peak / start;
printf( " energy %-24s peak %.5f at step %d\n", scene->name, ratio, peakStep );
return ratio;
}
// Perfectly elastic material with friction, dropped every way that used to gain energy: a spinning
// edge landing, tumbling, a flat landing with spin, a box falling on a resting box, and a tall box
// landing flat on its narrow end. Energy must never rise above the start. This is the load bearing
// property of restitution. Accuracy of the coefficient and freedom from spin are secondary.
//
// The tall boxes guard the stage iteration count. Their contact points sit close together while the
// inertia is long, so the off diagonal of the multi point normal mass is large and one sweep
// overshoots.
static int EnergyTest( void )
{
static const EnergyScene scenes[] = {
{ "spinning edge", 0.01f, 0.6f, -1.43f, 7.9f, 0.7f, -1.9f, 0.5f, 0.5f, false, false, 300 },
{ "spinning edge fast", 0.01f, 0.6f, -0.8f, 12.0f, 3.0f, -8.0f, 0.5f, 0.5f, false, false, 300 },
{ "tumble", 0.01f, 0.6f, -0.3f, 3.0f, 8.0f, 0.0f, 0.5f, 0.5f, false, false, 600 },
{ "tumble discrete", 0.5f, 0.6f, -0.3f, 3.0f, 8.0f, 0.0f, 0.5f, 0.5f, false, false, 600 },
{ "flat with spin", 0.01f, 0.6f, 0.0f, 2.0f, 2.0f, -5.0f, 0.5f, 0.5f, false, false, 300 },
{ "flat frictionless", 0.01f, 0.0f, 0.0f, 0.0f, 10.0f, 0.0f, 0.5f, 0.5f, false, false, 1200 },
{ "edge", 0.01f, 0.6f, -0.2f, 0.0f, 5.0f, 0.0f, 0.5f, 0.5f, false, false, 600 },
{ "two box", 0.02f, 0.6f, 0.0f, 0.0f, 4.0f, 0.0f, 0.5f, 0.5f, true, true, 1500 },
{ "two box discrete", 0.5f, 0.6f, 0.0f, 0.0f, 4.0f, 0.0f, 0.5f, 0.5f, false, true, 1500 },
{ "tall flat frictionless", 0.01f, 0.0f, 0.0f, 0.0f, 10.0f, 0.0f, 0.5f, 1.0f, false, false, 1200 },
{ "tall flat friction", 0.01f, 0.6f, 0.0f, 0.0f, 10.0f, 0.0f, 0.5f, 1.0f, false, false, 1200 },
{ "narrow flat frictionless", 0.01f, 0.0f, 0.0f, 0.0f, 10.0f, 0.0f, 0.25f, 1.0f, false, false, 1200 },
{ "narrow flat friction", 0.01f, 0.6f, 0.0f, 0.0f, 10.0f, 0.0f, 0.25f, 1.0f, false, false, 1200 },
};
// The soft contact position correction lifts a body that lands inside the ground, which is
// potential energy from nowhere at any restitution. Continuous collision keeps landings on the
// surface, so the tolerance only has to cover the stage. A four point flat landing does not
// converge at the default two stage iterations the way the two point landing does in 2D: the cube
// comes back with 0.2% extra. The in-relax scheme this replaced gained 2.5% to 29% on these scenes,
// so the tolerance still separates the two.
const float tolerance = 5.0e-3f;
const float discreteTolerance = 2.0e-2f;
int failed = 0;
for ( int i = 0; i < ARRAY_COUNT( scenes ); ++i )
{
float ratio = MeasurePeakEnergy( scenes + i );
float limit = scenes[i].safetyFactor < 0.1f ? tolerance : discreteTolerance;
if ( ratio > 1.0f + limit )
{
failed = 1;
}
}
return failed;
}
// The sampled approach speed and the Poisson accumulator are per manifold point state, so they must
// survive the split across workers
static int WorkerParityTest( void )
{
uint64_t hash1 = RunWorkerScene( 1 );
@ -1128,6 +1294,7 @@ int RestitutionTest( void )
RUN_MEASUREMENT( RestingTest );
RUN_MEASUREMENT( NormalVelocityTest );
RUN_MEASUREMENT( OvershootTest );
RUN_MEASUREMENT( EnergyTest );
RUN_MEASUREMENT( ImpulseTest );
RUN_MEASUREMENT( WorkerParityTest );