Gyro tolerance (#191)
This commit is contained in:
parent
0cfc475b88
commit
efca0c184d
23 changed files with 420 additions and 63 deletions
170
test/test_body.c
170
test/test_body.c
|
|
@ -475,6 +475,174 @@ static int ShapeExtents( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int CompoundInertiaConsistency( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Normalize( (b3Vec3){ 0.3f, -0.8f, 0.5f } ), 1.3f );
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3BoxHull boxA = b3MakeOffsetBoxHull( 0.5f, 0.25f, 0.5f, (b3Vec3){ 1.0f, 1.0f, 0.0f } );
|
||||
b3BoxHull boxB = b3MakeOffsetBoxHull( 0.3f, 0.6f, 0.2f, (b3Vec3){ -0.8f, -0.4f, 0.3f } );
|
||||
b3CreateHullShape( bodyId, &shapeDef, &boxA.base );
|
||||
b3CreateHullShape( bodyId, &shapeDef, &boxB.base );
|
||||
b3Body_ApplyMassFromShapes( bodyId );
|
||||
|
||||
b3Matrix3 local = b3Body_GetLocalRotationalInertia( bodyId );
|
||||
ENSURE( b3AbsFloat( local.cy.x ) > 0.1f );
|
||||
|
||||
b3Matrix3 r = b3MakeMatrixFromQuat( bodyDef.rotation );
|
||||
b3Matrix3 world = b3MulMM( b3MulMM( r, local ), b3Transpose( r ) );
|
||||
b3Matrix3 invWorld = b3Body_GetWorldInverseRotationalInertia( bodyId );
|
||||
b3Matrix3 product = b3MulMM( invWorld, world );
|
||||
|
||||
ENSURE_SMALL( product.cx.x - 1.0f, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cy.y - 1.0f, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cz.z - 1.0f, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cx.y, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cx.z, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cy.x, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cy.z, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cz.x, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cz.y, 1.0e-4f );
|
||||
|
||||
b3Quat q2 = b3MakeQuatFromAxisAngle( b3Normalize( (b3Vec3){ -1.0f, 0.2f, 0.4f } ), -2.1f );
|
||||
b3Body_SetTransform( bodyId, b3Body_GetPosition( bodyId ), q2 );
|
||||
r = b3MakeMatrixFromQuat( q2 );
|
||||
world = b3MulMM( b3MulMM( r, local ), b3Transpose( r ) );
|
||||
invWorld = b3Body_GetWorldInverseRotationalInertia( bodyId );
|
||||
product = b3MulMM( invWorld, world );
|
||||
ENSURE_SMALL( product.cx.x - 1.0f, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cy.y - 1.0f, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cz.z - 1.0f, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cy.x, 1.0e-4f );
|
||||
ENSURE_SMALL( product.cz.y, 1.0e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static b3BodyId CreateSpinningHullBody( b3WorldId worldId, float x, const b3HullData* hull )
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position.x = x;
|
||||
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Normalize( (b3Vec3){ 0.3f, -0.8f, 0.5f } ), 1.3f );
|
||||
bodyDef.angularVelocity = (b3Vec3){ 1.0f, -2.0f, 3.0f };
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, hull );
|
||||
return bodyId;
|
||||
}
|
||||
|
||||
static bool HasGyroscopicTorque( b3BodyId bodyId )
|
||||
{
|
||||
b3World* world = b3GetWorld( bodyId.world0 );
|
||||
b3Body* body = b3GetBodyFullId( world, bodyId );
|
||||
return ( body->flags & b3_enableGyroscopicTorque ) != 0;
|
||||
}
|
||||
|
||||
// Bodies within the isotropic tolerance skip the gyroscopic solve, so in free flight the world
|
||||
// angular velocity is untouched. Anisotropic bodies precess.
|
||||
static int IsotropicInertiaTolerance( void )
|
||||
{
|
||||
b3Vec3 w0 = { 1.0f, -2.0f, 3.0f };
|
||||
|
||||
b3HullData* rock = b3CreateRock( 0.5f );
|
||||
b3HullData* complex = b3CreateComplexHull( 0.5f );
|
||||
b3BoxHull cube = b3MakeCubeHull( 0.5f );
|
||||
b3Transform cubeTransform = { b3Vec3_zero, b3MakeQuatFromAxisAngle( b3Normalize( (b3Vec3){ 1.0f, 2.0f, 3.0f } ), 0.7f ) };
|
||||
b3BoxHull rotatedCube = b3MakeTransformedBoxHull( 0.5f, 0.5f, 0.5f, cubeTransform );
|
||||
b3BoxHull plank = b3MakeBoxHull( 0.5f, 1.0f, 0.25f );
|
||||
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.gravity = b3Vec3_zero;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyId rockId = CreateSpinningHullBody( worldId, 0.0f, rock );
|
||||
b3BodyId complexId = CreateSpinningHullBody( worldId, 5.0f, complex );
|
||||
b3BodyId cubeId = CreateSpinningHullBody( worldId, 10.0f, &cube.base );
|
||||
b3BodyId rotatedCubeId = CreateSpinningHullBody( worldId, 15.0f, &rotatedCube.base );
|
||||
b3BodyId plankId = CreateSpinningHullBody( worldId, 20.0f, &plank.base );
|
||||
|
||||
ENSURE( HasGyroscopicTorque( rockId ) == false );
|
||||
ENSURE( HasGyroscopicTorque( complexId ) == false );
|
||||
ENSURE( HasGyroscopicTorque( cubeId ) == false );
|
||||
ENSURE( HasGyroscopicTorque( rotatedCubeId ) == false );
|
||||
ENSURE( HasGyroscopicTorque( plankId ) );
|
||||
|
||||
for ( int i = 0; i < 10; ++i )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
b3Vec3 w = b3Body_GetAngularVelocity( rockId );
|
||||
ENSURE( w.x == w0.x && w.y == w0.y && w.z == w0.z );
|
||||
w = b3Body_GetAngularVelocity( complexId );
|
||||
ENSURE( w.x == w0.x && w.y == w0.y && w.z == w0.z );
|
||||
w = b3Body_GetAngularVelocity( rotatedCubeId );
|
||||
ENSURE( w.x == w0.x && w.y == w0.y && w.z == w0.z );
|
||||
w = b3Body_GetAngularVelocity( plankId );
|
||||
ENSURE( b3Length( b3Sub( w, w0 ) ) > 0.01f );
|
||||
|
||||
b3Body_SetMotionLocks( plankId, (b3MotionLocks){ .angularX = true, .angularY = true, .angularZ = true } );
|
||||
ENSURE( HasGyroscopicTorque( plankId ) == false );
|
||||
|
||||
// Gyroscopic torque stays off until a mass update proves the body anisotropic
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position.x = 25.0f;
|
||||
bodyDef.angularVelocity = w0;
|
||||
b3BodyId emptyId = b3CreateBody( worldId, &bodyDef );
|
||||
ENSURE( HasGyroscopicTorque( emptyId ) == false );
|
||||
|
||||
bodyDef.position.x = 30.0f;
|
||||
b3BodyId deferredId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.updateBodyMass = false;
|
||||
b3CreateHullShape( deferredId, &shapeDef, &plank.base );
|
||||
ENSURE( HasGyroscopicTorque( deferredId ) == false );
|
||||
|
||||
b3Body_ApplyMassFromShapes( deferredId );
|
||||
ENSURE( HasGyroscopicTorque( deferredId ) );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
}
|
||||
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.gravity = b3Vec3_zero;
|
||||
worldDef.isotropicInertiaTolerance = 0.0f;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyId rockId = CreateSpinningHullBody( worldId, 0.0f, rock );
|
||||
b3BodyId cubeId = CreateSpinningHullBody( worldId, 10.0f, &cube.base );
|
||||
|
||||
ENSURE( HasGyroscopicTorque( rockId ) );
|
||||
ENSURE( HasGyroscopicTorque( cubeId ) == false );
|
||||
|
||||
for ( int i = 0; i < 10; ++i )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
b3Vec3 w = b3Body_GetAngularVelocity( rockId );
|
||||
ENSURE( b3Length( b3Sub( w, w0 ) ) > 0.001f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
}
|
||||
|
||||
b3DestroyHull( rock );
|
||||
b3DestroyHull( complex );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BodyTest( void )
|
||||
{
|
||||
RUN_SUBTEST( FarSingleSphereMass );
|
||||
|
|
@ -486,5 +654,7 @@ int BodyTest( void )
|
|||
RUN_SUBTEST( SetMassDataZeroMass );
|
||||
RUN_SUBTEST( SetMassDataConsistentVelocity );
|
||||
RUN_SUBTEST( ShapeExtents );
|
||||
RUN_SUBTEST( CompoundInertiaConsistency );
|
||||
RUN_SUBTEST( IsotropicInertiaTolerance );
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,27 +19,27 @@
|
|||
// 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 267
|
||||
#define RAGDOLL_HASH 0x01EB8FDB
|
||||
#define WAVE_PILE_SLEEP_STEP 333
|
||||
#define WAVE_PILE_HASH 0x80D5266B
|
||||
#define RAGDOLL_SLEEP_STEP 263
|
||||
#define RAGDOLL_HASH 0xA5423F98
|
||||
#define WAVE_PILE_SLEEP_STEP 272
|
||||
#define WAVE_PILE_HASH 0xC8E5F3D6
|
||||
#define QUERY_SPAWN_SLEEP_STEP 242
|
||||
#define QUERY_SPAWN_HASH 0x90EE3E7C
|
||||
#define QUERY_SPAWN_HASH 0x489585E6
|
||||
#define QUERY_SPAWN_HIT_COUNT 59
|
||||
#define QUERY_SPAWN_QUERY_HASH 0x5B4429DC
|
||||
#define MESH_DROP_SLEEP_STEP 218
|
||||
#define MESH_DROP_HASH 0x10051259
|
||||
#define MESH_DROP_HASH 0xD378CF04
|
||||
#else
|
||||
#define RAGDOLL_SLEEP_STEP 265
|
||||
#define RAGDOLL_HASH 0xB98B3638
|
||||
#define WAVE_PILE_SLEEP_STEP 288
|
||||
#define WAVE_PILE_HASH 0xFC1CD2DC
|
||||
#define RAGDOLL_SLEEP_STEP 291
|
||||
#define RAGDOLL_HASH 0xFF4C605B
|
||||
#define WAVE_PILE_SLEEP_STEP 272
|
||||
#define WAVE_PILE_HASH 0x266F83B0
|
||||
#define QUERY_SPAWN_SLEEP_STEP 242
|
||||
#define QUERY_SPAWN_HASH 0x58F47348
|
||||
#define QUERY_SPAWN_HASH 0xF3AD9BA6
|
||||
#define QUERY_SPAWN_HIT_COUNT 59
|
||||
#define QUERY_SPAWN_QUERY_HASH 0xE3271F3D
|
||||
#define MESH_DROP_SLEEP_STEP 218
|
||||
#define MESH_DROP_HASH 0x983F27EA
|
||||
#define MESH_DROP_SLEEP_STEP 226
|
||||
#define MESH_DROP_HASH 0xD2FF069D
|
||||
#endif
|
||||
|
||||
// The goldens above pin exact values for the default four point manifold. A build that
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
|
|
|||
Loading…
Reference in a new issue