Optimizations (#168)
- Optimize contact prepare - Back to post restitution stage - Optimize solver math - Bug fixes
This commit is contained in:
parent
9e5a4cde86
commit
79fc55976a
55 changed files with 3248 additions and 1519 deletions
|
|
@ -6,6 +6,7 @@ set(BOX3D_TEST_FILES
|
|||
test_bitset.c
|
||||
test_body.c
|
||||
test_body_query.c
|
||||
test_broad_phase.c
|
||||
test_collision.c
|
||||
test_compound.c
|
||||
test_container.c
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ extern int AllocatorTest( void );
|
|||
extern int BitTest( void );
|
||||
extern int BodyTest( void );
|
||||
extern int BodyQueryTest( void );
|
||||
extern int BroadPhaseTest( void );
|
||||
extern int CollisionTest( void );
|
||||
extern int CompoundTest( void );
|
||||
extern int ContainerTest( void );
|
||||
|
|
@ -106,6 +107,7 @@ int main( int argc, char** argv )
|
|||
MAYBE_RUN_TEST( BitTest );
|
||||
MAYBE_RUN_TEST( BodyTest );
|
||||
MAYBE_RUN_TEST( BodyQueryTest );
|
||||
MAYBE_RUN_TEST( BroadPhaseTest );
|
||||
MAYBE_RUN_TEST( CollisionTest );
|
||||
MAYBE_RUN_TEST( CompoundTest );
|
||||
MAYBE_RUN_TEST( ContainerTest );
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ static int TestBlockAlignment( void )
|
|||
{
|
||||
elements[j] = b3AllocateElement( &allocator );
|
||||
ENSURE( elements[j] != NULL );
|
||||
ENSURE( ( (uintptr_t)elements[j] & ( B3_ALIGNMENT - 1 ) ) == 0 );
|
||||
ENSURE( ( (uintptr_t)elements[j] & ( B3_BLOCK_ALIGNMENT - 1 ) ) == 0 );
|
||||
}
|
||||
|
||||
for ( int j = 0; j < count; ++j )
|
||||
|
|
@ -75,7 +75,7 @@ static int TestBlockAlignment( void )
|
|||
{
|
||||
elements[j] = b3AllocateElement( &allocator );
|
||||
ENSURE( elements[j] != NULL );
|
||||
ENSURE( ( (uintptr_t)elements[j] & ( B3_ALIGNMENT - 1 ) ) == 0 );
|
||||
ENSURE( ( (uintptr_t)elements[j] & ( B3_BLOCK_ALIGNMENT - 1 ) ) == 0 );
|
||||
}
|
||||
|
||||
for ( int j = 0; j < count; ++j )
|
||||
|
|
|
|||
488
test/test_broad_phase.c
Normal file
488
test/test_broad_phase.c
Normal file
|
|
@ -0,0 +1,488 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "body.h"
|
||||
#include "broad_phase.h"
|
||||
#include "physics_world.h"
|
||||
#include "shape.h"
|
||||
#include "table.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
// Brute force check of the broad-phase pair set against every live shape pair. Must run right
|
||||
// after a zero time step so the pair update and the disjoint contact purge have both happened
|
||||
// against the current fat boxes.
|
||||
static int CheckPairSet( b3WorldId worldId )
|
||||
{
|
||||
b3World* world = b3GetWorldFromId( worldId );
|
||||
b3BroadPhase* bp = &world->broadPhase;
|
||||
const b3Shape* shapes = world->shapes.data;
|
||||
const b3AABB* fatAABBs = world->fatAABBs.data;
|
||||
int shapeCount = world->shapes.count;
|
||||
|
||||
for ( int i = 0; i < shapeCount; ++i )
|
||||
{
|
||||
const b3Shape* shapeA = shapes + i;
|
||||
if ( shapeA->id == B3_NULL_INDEX || shapeA->proxyKey == B3_NULL_INDEX || shapeA->sensorIndex != B3_NULL_INDEX )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
b3Body* bodyA = b3Array_Get( world->bodies, shapeA->bodyId );
|
||||
|
||||
for ( int j = i + 1; j < shapeCount; ++j )
|
||||
{
|
||||
const b3Shape* shapeB = shapes + j;
|
||||
if ( shapeB->id == B3_NULL_INDEX || shapeB->proxyKey == B3_NULL_INDEX || shapeB->sensorIndex != B3_NULL_INDEX )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( shapeA->bodyId == shapeB->bodyId )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
b3Body* bodyB = b3Array_Get( world->bodies, shapeB->bodyId );
|
||||
|
||||
bool expected = b3ShouldShapesCollide( shapeA->filter, shapeB->filter ) &&
|
||||
b3ShouldBodiesCollide( world, bodyA, bodyB ) && b3AABB_Overlaps( fatAABBs[i], fatAABBs[j] );
|
||||
|
||||
bool actual = b3ContainsKey( &bp->pairSet, b3ShapePairKey( i, j, 0 ) );
|
||||
|
||||
// A sleeping contact is never refreshed, so a stale pair can outlive the overlap when
|
||||
// the other body is teleported, and a set woken by the narrow phase carries its stale
|
||||
// contacts into the awake set one collide late. With sleeping on only a missing pair
|
||||
// is a bug.
|
||||
if ( world->enableSleep && actual && expected == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( expected != actual )
|
||||
{
|
||||
printf( " pair mismatch: shapes %d (body %d type %d set %d) and %d (body %d type %d set %d), expected %d, "
|
||||
"actual %d\n",
|
||||
i, shapeA->bodyId, bodyA->type, bodyA->setIndex, j, shapeB->bodyId, bodyB->type, bodyB->setIndex,
|
||||
expected, actual );
|
||||
fflush( stdout );
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct RandomState
|
||||
{
|
||||
uint32_t seed;
|
||||
} RandomState;
|
||||
|
||||
static uint32_t NextRandom( RandomState* state )
|
||||
{
|
||||
// xorshift32
|
||||
uint32_t x = state->seed;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
state->seed = x;
|
||||
return x;
|
||||
}
|
||||
|
||||
static float RandomFloat( RandomState* state, float lower, float upper )
|
||||
{
|
||||
float u = (float)( NextRandom( state ) & 0xFFFFFF ) / (float)0x1000000;
|
||||
return lower + ( upper - lower ) * u;
|
||||
}
|
||||
|
||||
static int RandomInt( RandomState* state, int count )
|
||||
{
|
||||
return (int)( NextRandom( state ) % (uint32_t)count );
|
||||
}
|
||||
|
||||
#define MAX_BODIES 64
|
||||
#define STATIC_COUNT 60
|
||||
#define TILE_COUNT 9
|
||||
|
||||
typedef struct BodySlot
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
bool active;
|
||||
} BodySlot;
|
||||
|
||||
typedef struct StressContext
|
||||
{
|
||||
b3WorldId worldId;
|
||||
RandomState random;
|
||||
b3Pos origin;
|
||||
float extent;
|
||||
BodySlot slots[MAX_BODIES];
|
||||
b3JointId joints[4];
|
||||
int jointCount;
|
||||
b3BoxHull box;
|
||||
} StressContext;
|
||||
|
||||
static b3Pos RandomPosition( StressContext* context )
|
||||
{
|
||||
float e = context->extent;
|
||||
b3Vec3 offset = { RandomFloat( &context->random, -e, e ), RandomFloat( &context->random, -e, e ),
|
||||
RandomFloat( &context->random, -e, e ) };
|
||||
return b3OffsetPos( context->origin, offset );
|
||||
}
|
||||
|
||||
static b3Vec3 RandomVelocity( StressContext* context )
|
||||
{
|
||||
// Some bodies outrun their margin every step and go through continuous collision
|
||||
float s = RandomInt( &context->random, 4 ) == 0 ? 40.0f : 12.0f;
|
||||
return (b3Vec3){ RandomFloat( &context->random, -s, s ), RandomFloat( &context->random, -s, s ),
|
||||
RandomFloat( &context->random, -s, s ) };
|
||||
}
|
||||
|
||||
static void CreateSlotBody( StressContext* context, int slot )
|
||||
{
|
||||
RandomState* random = &context->random;
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = RandomInt( random, 10 ) == 0 ? b3_kinematicBody : b3_dynamicBody;
|
||||
bodyDef.position = RandomPosition( context );
|
||||
bodyDef.linearVelocity = RandomVelocity( context );
|
||||
bodyDef.isBullet = RandomInt( random, 3 ) == 0;
|
||||
bodyDef.gravityScale = 0.0f;
|
||||
b3BodyId bodyId = b3CreateBody( context->worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.filter.categoryBits = 1ull << RandomInt( random, 3 );
|
||||
shapeDef.filter.maskBits = RandomInt( random, 4 ) == 0 ? 0x5ull : B3_DEFAULT_MASK_BITS;
|
||||
|
||||
int shapeCount = 1 + RandomInt( random, 2 );
|
||||
for ( int i = 0; i < shapeCount; ++i )
|
||||
{
|
||||
if ( RandomInt( random, 2 ) == 0 )
|
||||
{
|
||||
b3Sphere sphere = { { RandomFloat( random, -1.0f, 1.0f ), 0.0f, 0.0f }, RandomFloat( random, 0.2f, 1.5f ) };
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
}
|
||||
else
|
||||
{
|
||||
b3CreateHullShape( bodyId, &shapeDef, &context->box.base );
|
||||
}
|
||||
}
|
||||
|
||||
context->slots[slot].bodyId = bodyId;
|
||||
context->slots[slot].active = true;
|
||||
}
|
||||
|
||||
// A swarm of bodies churning through every proxy mutation the world API offers, with the pair
|
||||
// set checked against a brute force scan after every step.
|
||||
static int RunBroadPhaseStress( int workerCount, uint32_t seed, bool enableSleep )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.workerCount = workerCount;
|
||||
worldDef.enableSleep = enableSleep;
|
||||
worldDef.gravity = b3Vec3_zero;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
StressContext context = { 0 };
|
||||
context.worldId = worldId;
|
||||
context.random.seed = seed;
|
||||
context.origin = (b3Pos){ -1900.0, 0.0, 1330.0 };
|
||||
context.extent = 12.0f;
|
||||
context.box = b3MakeBoxHull( 0.95f, 0.6f, 2.2f );
|
||||
|
||||
// Kilometre tiles far from the origin under the swarm, like a streamed terrain
|
||||
b3HeightFieldData* tile = b3CreateGrid( 65, 65, (b3Vec3){ 16.0f, 1.0f, 16.0f }, false );
|
||||
for ( int i = 0; i < TILE_COUNT; ++i )
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = (b3Pos){ -3072.0 + 1024.0 * ( i % 3 ), -30.0, 1024.0 * ( i / 3 ) };
|
||||
b3BodyId tileId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.enableSpeculativeContact = false;
|
||||
b3CreateHeightFieldShape( tileId, &shapeDef, tile );
|
||||
}
|
||||
|
||||
// Static clutter inside the swarm. Some pieces ask for contacts on creation.
|
||||
b3BodyId staticIds[STATIC_COUNT];
|
||||
b3HeightFieldData* grid = b3CreateGrid( 5, 5, (b3Vec3){ 2.0f, 1.0f, 2.0f }, false );
|
||||
b3BoxHull staticBox = b3MakeBoxHull( 3.0f, 0.5f, 3.0f );
|
||||
for ( int i = 0; i < STATIC_COUNT; ++i )
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = RandomPosition( &context );
|
||||
staticIds[i] = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.invokeContactCreation = ( i % 4 ) == 0;
|
||||
shapeDef.enableSpeculativeContact = ( i % 2 ) == 0;
|
||||
|
||||
if ( i % 3 == 0 )
|
||||
{
|
||||
b3CreateHeightFieldShape( staticIds[i], &shapeDef, grid );
|
||||
}
|
||||
else
|
||||
{
|
||||
b3CreateHullShape( staticIds[i], &shapeDef, &staticBox.base );
|
||||
}
|
||||
}
|
||||
|
||||
for ( int i = 0; i < MAX_BODIES; ++i )
|
||||
{
|
||||
if ( RandomInt( &context.random, 4 ) != 0 )
|
||||
{
|
||||
CreateSlotBody( &context, i );
|
||||
}
|
||||
}
|
||||
|
||||
float timeStep = 1.0f / 60.0f;
|
||||
int stepCount = 400;
|
||||
|
||||
for ( int step = 0; step < stepCount; ++step )
|
||||
{
|
||||
RandomState* random = &context.random;
|
||||
|
||||
int mutationCount = RandomInt( random, 4 );
|
||||
for ( int m = 0; m < mutationCount; ++m )
|
||||
{
|
||||
int slot = RandomInt( random, MAX_BODIES );
|
||||
BodySlot* body = context.slots + slot;
|
||||
int action = RandomInt( random, 12 );
|
||||
|
||||
if ( body->active == false )
|
||||
{
|
||||
if ( action < 6 )
|
||||
{
|
||||
CreateSlotBody( &context, slot );
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ( action )
|
||||
{
|
||||
case 0:
|
||||
b3DestroyBody( body->bodyId );
|
||||
body->active = false;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
case 2:
|
||||
b3Body_SetTransform( body->bodyId, RandomPosition( &context ), b3Quat_identity );
|
||||
break;
|
||||
|
||||
case 3:
|
||||
b3Body_SetLinearVelocity( body->bodyId, RandomVelocity( &context ) );
|
||||
break;
|
||||
|
||||
case 4:
|
||||
b3Body_Disable( body->bodyId );
|
||||
break;
|
||||
|
||||
case 5:
|
||||
b3Body_Enable( body->bodyId );
|
||||
break;
|
||||
|
||||
case 6:
|
||||
b3Body_SetBullet( body->bodyId, RandomInt( random, 2 ) == 0 );
|
||||
break;
|
||||
|
||||
case 7:
|
||||
{
|
||||
b3BodyType types[3] = { b3_staticBody, b3_kinematicBody, b3_dynamicBody };
|
||||
b3Body_SetType( body->bodyId, types[RandomInt( random, 3 )] );
|
||||
if ( b3Body_GetType( body->bodyId ) != b3_staticBody )
|
||||
{
|
||||
b3Body_SetLinearVelocity( body->bodyId, RandomVelocity( &context ) );
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 8:
|
||||
{
|
||||
// Add a joint that blocks collision, or toggle one
|
||||
if ( context.jointCount < 4 )
|
||||
{
|
||||
int otherSlot = RandomInt( random, MAX_BODIES );
|
||||
BodySlot* other = context.slots + otherSlot;
|
||||
if ( otherSlot != slot && other->active && b3Body_GetType( body->bodyId ) == b3_dynamicBody &&
|
||||
b3Body_GetType( other->bodyId ) == b3_dynamicBody )
|
||||
{
|
||||
b3WeldJointDef jointDef = b3DefaultWeldJointDef();
|
||||
jointDef.base.bodyIdA = body->bodyId;
|
||||
jointDef.base.bodyIdB = other->bodyId;
|
||||
jointDef.base.collideConnected = false;
|
||||
context.joints[context.jointCount++] = b3CreateWeldJoint( worldId, &jointDef );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
b3JointId jointId = context.joints[RandomInt( random, context.jointCount )];
|
||||
if ( b3Joint_IsValid( jointId ) )
|
||||
{
|
||||
b3Joint_SetCollideConnected( jointId, RandomInt( random, 2 ) == 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 9:
|
||||
{
|
||||
int staticIndex = RandomInt( random, STATIC_COUNT );
|
||||
b3Body_SetTransform( staticIds[staticIndex], RandomPosition( &context ), b3Quat_identity );
|
||||
}
|
||||
break;
|
||||
|
||||
case 10:
|
||||
b3World_RebuildStaticTree( worldId );
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Joints whose bodies were destroyed are gone too
|
||||
for ( int j = 0; j < context.jointCount; )
|
||||
{
|
||||
if ( b3Joint_IsValid( context.joints[j] ) == false )
|
||||
{
|
||||
context.joints[j] = context.joints[context.jointCount - 1];
|
||||
context.jointCount -= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Keep the swarm inside the clutter
|
||||
for ( int i = 0; i < MAX_BODIES; ++i )
|
||||
{
|
||||
BodySlot* body = context.slots + i;
|
||||
if ( body->active == false || b3Body_IsEnabled( body->bodyId ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
b3Pos p = b3Body_GetPosition( body->bodyId );
|
||||
b3Vec3 d = b3SubPos( p, context.origin );
|
||||
if ( b3AbsFloat( d.x ) > 2.0f * context.extent || b3AbsFloat( d.y ) > 2.0f * context.extent ||
|
||||
b3AbsFloat( d.z ) > 2.0f * context.extent )
|
||||
{
|
||||
b3Body_SetTransform( body->bodyId, RandomPosition( &context ), b3Quat_identity );
|
||||
b3Body_SetLinearVelocity( body->bodyId, RandomVelocity( &context ) );
|
||||
}
|
||||
}
|
||||
|
||||
b3World_Step( worldId, timeStep, 4 );
|
||||
|
||||
// A zero step finds pairs and purges disjoint contacts without moving anything
|
||||
b3World_Step( worldId, 0.0f, 1 );
|
||||
|
||||
if ( CheckPairSet( worldId ) != 0 )
|
||||
{
|
||||
printf( " workers=%d seed=%u sleep=%d step=%d\n", workerCount, seed, enableSleep, step );
|
||||
fflush( stdout );
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyHeightField( grid );
|
||||
b3DestroyHeightField( tile );
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
b3DestroyHeightField( grid );
|
||||
b3DestroyHeightField( tile );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int BroadPhaseStressTest( void )
|
||||
{
|
||||
uint32_t seeds[3] = { 12345u, 0xC0FFEEu, 777u };
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
ENSURE( RunBroadPhaseStress( 1, seeds[i], false ) == 0 );
|
||||
ENSURE( RunBroadPhaseStress( 4, seeds[i], false ) == 0 );
|
||||
ENSURE( RunBroadPhaseStress( 3, seeds[i], true ) == 0 );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The broad-phase keeps its pending pair marks inside the static tree, so a full rebuild between
|
||||
// a static mutation and the next step used to wipe them and the contact never appeared. Covers
|
||||
// both ways a static proxy gets marked: a teleport and a shape created with invokeContactCreation.
|
||||
static int RunStaticRebuildKeepsMarks( bool useInvoke )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.gravity = b3Vec3_zero;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Pos){ 0.0, 1.0, 0.0 };
|
||||
b3BodyId sphereId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
|
||||
b3CreateSphereShape( sphereId, &shapeDef, &sphere );
|
||||
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = (b3Pos){ 100.0, 0.0, 0.0 };
|
||||
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull box = b3MakeBoxHull( 2.0f, 0.5f, 2.0f );
|
||||
if ( useInvoke == false )
|
||||
{
|
||||
b3CreateHullShape( groundId, &shapeDef, &box.base );
|
||||
}
|
||||
|
||||
// Settle so the resting sphere carries no mark of its own
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
b3Body_SetTransform( groundId, b3Pos_zero, b3Quat_identity );
|
||||
if ( useInvoke )
|
||||
{
|
||||
b3ShapeDef invokeDef = b3DefaultShapeDef();
|
||||
invokeDef.invokeContactCreation = true;
|
||||
b3CreateHullShape( groundId, &invokeDef, &box.base );
|
||||
}
|
||||
|
||||
b3World_RebuildStaticTree( worldId );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
|
||||
ENSURE( b3Body_GetContactCapacity( sphereId ) == 1 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int StaticRebuildKeepsMarks( void )
|
||||
{
|
||||
ENSURE( RunStaticRebuildKeepsMarks( false ) == 0 );
|
||||
ENSURE( RunStaticRebuildKeepsMarks( true ) == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int FlushingAssert( const char* condition, const char* fileName, int lineNumber )
|
||||
{
|
||||
printf( "BOX3D ASSERTION: %s, %s, line %d\n", condition, fileName, lineNumber );
|
||||
fflush( stdout );
|
||||
return 1;
|
||||
}
|
||||
|
||||
int BroadPhaseTest( void )
|
||||
{
|
||||
// A crash through a pipe loses buffered output, so flush the assertion first
|
||||
b3SetAssertFcn( FlushingAssert );
|
||||
|
||||
RUN_SUBTEST( StaticRebuildKeepsMarks );
|
||||
RUN_SUBTEST( BroadPhaseStressTest );
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -18,23 +18,23 @@
|
|||
// 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 272
|
||||
#define RAGDOLL_HASH 0xC049067A
|
||||
#define WAVE_PILE_SLEEP_STEP 323
|
||||
#define WAVE_PILE_HASH 0x2200A547
|
||||
#define RAGDOLL_SLEEP_STEP 273
|
||||
#define RAGDOLL_HASH 0x0F4DD1EB
|
||||
#define WAVE_PILE_SLEEP_STEP 314
|
||||
#define WAVE_PILE_HASH 0xD9B88124
|
||||
#define QUERY_SPAWN_SLEEP_STEP 242
|
||||
#define QUERY_SPAWN_HASH 0x1737F5BC
|
||||
#define QUERY_SPAWN_HASH 0x9B96A098
|
||||
#define QUERY_SPAWN_HIT_COUNT 59
|
||||
#define QUERY_SPAWN_QUERY_HASH 0x5B4429DC
|
||||
#define MESH_DROP_SLEEP_STEP 217
|
||||
#define MESH_DROP_HASH 0xCF946437
|
||||
#else
|
||||
#define RAGDOLL_SLEEP_STEP 271
|
||||
#define RAGDOLL_HASH 0x805719D3
|
||||
#define WAVE_PILE_SLEEP_STEP 316
|
||||
#define WAVE_PILE_HASH 0xAA7D15A7
|
||||
#define RAGDOLL_SLEEP_STEP 274
|
||||
#define RAGDOLL_HASH 0x773AB8ED
|
||||
#define WAVE_PILE_SLEEP_STEP 330
|
||||
#define WAVE_PILE_HASH 0x205D6055
|
||||
#define QUERY_SPAWN_SLEEP_STEP 242
|
||||
#define QUERY_SPAWN_HASH 0xB9F993A5
|
||||
#define QUERY_SPAWN_HASH 0xF1EDEF47
|
||||
#define QUERY_SPAWN_HIT_COUNT 59
|
||||
#define QUERY_SPAWN_QUERY_HASH 0xE3271F3D
|
||||
#define MESH_DROP_SLEEP_STEP 217
|
||||
|
|
|
|||
|
|
@ -608,6 +608,65 @@ static int TestCreateJointClearsContacts( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Issue #164: a joint with no dynamic body lives in the static set even when one body is an awake
|
||||
// kinematic. Disabling and enabling that body has to respect this, whether the joint was created
|
||||
// that way or got there through body type changes.
|
||||
static int TestNonDynamicJointDisable( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.gravity = b3Vec3_zero;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
b3BodyId bodyIdA = b3CreateBody( worldId, &bodyDef );
|
||||
bodyDef.position = (b3Pos){ 2.0f, 0.0f, 0.0f };
|
||||
b3BodyId bodyIdB = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3BoxHull box = b3MakeCubeHull( 0.5f );
|
||||
b3CreateHullShape( bodyIdA, &shapeDef, &box.base );
|
||||
b3CreateHullShape( bodyIdB, &shapeDef, &box.base );
|
||||
|
||||
b3WeldJointDef jointDef = b3DefaultWeldJointDef();
|
||||
jointDef.base.bodyIdA = bodyIdA;
|
||||
jointDef.base.bodyIdB = bodyIdB;
|
||||
b3JointId weldId = b3CreateWeldJoint( worldId, &jointDef );
|
||||
|
||||
b3Body_SetType( bodyIdA, b3_staticBody );
|
||||
b3Body_SetType( bodyIdB, b3_kinematicBody );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
|
||||
b3Body_Disable( bodyIdB );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
b3Body_Enable( bodyIdB );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
ENSURE( b3Joint_IsValid( weldId ) );
|
||||
|
||||
b3Body_SetType( bodyIdB, b3_dynamicBody );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
ENSURE( b3Joint_IsAwake( weldId ) );
|
||||
|
||||
// Created directly between a static and a kinematic body
|
||||
bodyDef.type = b3_kinematicBody;
|
||||
bodyDef.position = (b3Pos){ 4.0f, 0.0f, 0.0f };
|
||||
b3BodyId bodyIdC = b3CreateBody( worldId, &bodyDef );
|
||||
b3CreateHullShape( bodyIdC, &shapeDef, &box.base );
|
||||
|
||||
b3RevoluteJointDef revoluteDef = b3DefaultRevoluteJointDef();
|
||||
revoluteDef.base.bodyIdA = bodyIdA;
|
||||
revoluteDef.base.bodyIdB = bodyIdC;
|
||||
b3JointId revoluteId = b3CreateRevoluteJoint( worldId, &revoluteDef );
|
||||
|
||||
b3Body_Disable( bodyIdC );
|
||||
b3Body_Enable( bodyIdC );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
ENSURE( b3Joint_IsAwake( revoluteId ) == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int JointTest( void )
|
||||
{
|
||||
RUN_SUBTEST( TestParallelJoint );
|
||||
|
|
@ -620,6 +679,7 @@ int JointTest( void )
|
|||
RUN_SUBTEST( TestWeldJoint );
|
||||
RUN_SUBTEST( TestWheelJoint );
|
||||
RUN_SUBTEST( TestCreateJointClearsContacts );
|
||||
RUN_SUBTEST( TestNonDynamicJointDisable );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1028,8 +1028,7 @@ static float ReplaySafetyFactor( b3RecPlayer* player, const char* name )
|
|||
}
|
||||
|
||||
// Exercise every recorded op in a single session, then validate replay at two worker
|
||||
// counts, round-trip through a file, and drive the incremental player. Mirrors the
|
||||
// comprehensive RecordingTest in Box2D's test suite (box2d/test/test_recording.c).
|
||||
// counts, round-trip through a file, and drive the incremental player.
|
||||
static int AllOps( void )
|
||||
{
|
||||
b3Recording* rec = b3CreateRecording( 0 );
|
||||
|
|
@ -1440,6 +1439,8 @@ static int AllOps( void )
|
|||
b3World_EnableWarmStarting( worldId, true );
|
||||
b3World_EnableSpeculative( worldId, true );
|
||||
b3World_SetRestitutionThreshold( worldId, 1.5f );
|
||||
b3World_SetRestitutionIterations( worldId, 3 );
|
||||
b3World_EnableRestitutionPropagation( worldId, true );
|
||||
b3World_SetHitEventThreshold( worldId, 2.0f );
|
||||
b3World_SetContactTuning( worldId, 30.0f, 10.0f, 3.0f );
|
||||
b3World_SetContactRecycleDistance( worldId, 0.05f );
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue