Fixes 03 (#40)
Fixes #33, #34, #35, #37, #39, #41, #42, #43 Also fixes b3Body_SetMassData inertia tensor.
This commit is contained in:
parent
29bf523ce7
commit
655799893a
25 changed files with 771 additions and 151 deletions
234
test/test_body.c
234
test/test_body.c
|
|
@ -7,6 +7,10 @@
|
|||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
// Reach into internals to observe body extents and the dirty mass flag.
|
||||
#include "body.h"
|
||||
#include "physics_world.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
// b3UpdateBodyMassData shifts each shape's inertia to the body center of mass with the parallel
|
||||
|
|
@ -117,9 +121,239 @@ static int FarCubeSphereMass( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Shapes added with updateBodyMass = false defer the mass update, which is also the only place
|
||||
// body extents are computed. A body that reaches the solver with minExtent == B3_HUGE never passes
|
||||
// the continuous collision gate. The dirty mass flag must track the deferral, and both
|
||||
// ApplyMassFromShapes and SetMassData must leave finite extents behind.
|
||||
static int DeferredMassExtents( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
shapeDef.updateBodyMass = false;
|
||||
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
|
||||
|
||||
// Deferred create leaves mass and extents untouched but marks the body dirty.
|
||||
b3BodyId applyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3CreateSphereShape( applyId, &shapeDef, &sphere );
|
||||
|
||||
b3World* world = b3GetWorld( applyId.world0 );
|
||||
b3Body* applyBody = b3GetBodyFullId( world, applyId );
|
||||
b3BodySim* applySim = b3GetBodySim( world, applyBody );
|
||||
|
||||
ENSURE( ( applyBody->flags & b3_dirtyMass ) != 0 );
|
||||
ENSURE( applySim->minExtent == B3_HUGE );
|
||||
|
||||
// ApplyMassFromShapes computes extents and clears the flag.
|
||||
b3Body_ApplyMassFromShapes( applyId );
|
||||
ENSURE( ( applyBody->flags & b3_dirtyMass ) == 0 );
|
||||
ENSURE( applySim->minExtent < B3_HUGE );
|
||||
|
||||
// SetMassData alone must also produce finite extents and clear the flag (the issue #35 repro).
|
||||
b3BodyId massId = b3CreateBody( worldId, &bodyDef );
|
||||
b3CreateSphereShape( massId, &shapeDef, &sphere );
|
||||
|
||||
b3Body* massBody = b3GetBodyFullId( world, massId );
|
||||
b3BodySim* massSim = b3GetBodySim( world, massBody );
|
||||
ENSURE( ( massBody->flags & b3_dirtyMass ) != 0 );
|
||||
|
||||
b3Matrix3 inertia = { { 0.2f, 0.0f, 0.0f }, { 0.0f, 0.2f, 0.0f }, { 0.0f, 0.0f, 0.2f } };
|
||||
b3MassData massData = { 2.0f, { 0.0f, 0.0f, 0.0f }, inertia };
|
||||
b3Body_SetMassData( massId, massData );
|
||||
|
||||
ENSURE( ( massBody->flags & b3_dirtyMass ) == 0 );
|
||||
ENSURE( massSim->minExtent < B3_HUGE );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// b3Body_SetMassData overrides the mass properties directly, bypassing the shapes. It must derive
|
||||
// everything the solver reads from the supplied tensor: the inverse mass, the local inverse inertia,
|
||||
// and the world inverse inertia rotated by the body orientation. Fixed rotation zeros the angular part.
|
||||
// These tests drive it through the public getters, no shapes required.
|
||||
|
||||
// Diagonal inertia with inverses that are exact in float, so tolerances stay tight.
|
||||
static const b3Matrix3 kDiagInertia = { { 2.0f, 0.0f, 0.0f }, { 0.0f, 4.0f, 0.0f }, { 0.0f, 0.0f, 8.0f } };
|
||||
|
||||
static int SetMassDataRoundTrip( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Pos){ 5.0f, -3.0f, 2.0f };
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3Vec3 center = { 0.1f, 0.2f, 0.3f };
|
||||
b3MassData massData = { 3.0f, center, kDiagInertia };
|
||||
b3Body_SetMassData( bodyId, massData );
|
||||
|
||||
ENSURE_SMALL( b3Body_GetMass( bodyId ) - 3.0f, 1e-6f );
|
||||
ENSURE_SMALL( b3Body_GetInverseMass( bodyId ) - 1.0f / 3.0f, 1e-6f );
|
||||
|
||||
b3MassData md = b3Body_GetMassData( bodyId );
|
||||
ENSURE_SMALL( md.mass - 3.0f, 1e-6f );
|
||||
ENSURE_SMALL( md.center.x - center.x, 1e-6f );
|
||||
ENSURE_SMALL( md.center.y - center.y, 1e-6f );
|
||||
ENSURE_SMALL( md.center.z - center.z, 1e-6f );
|
||||
ENSURE_SMALL( md.inertia.cx.x - 2.0f, 1e-6f );
|
||||
ENSURE_SMALL( md.inertia.cy.y - 4.0f, 1e-6f );
|
||||
ENSURE_SMALL( md.inertia.cz.z - 8.0f, 1e-6f );
|
||||
|
||||
b3Vec3 localCenter = b3Body_GetLocalCenterOfMass( bodyId );
|
||||
ENSURE_SMALL( localCenter.x - center.x, 1e-6f );
|
||||
ENSURE_SMALL( localCenter.y - center.y, 1e-6f );
|
||||
ENSURE_SMALL( localCenter.z - center.z, 1e-6f );
|
||||
|
||||
b3Matrix3 localInertia = b3Body_GetLocalRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( localInertia.cx.x - 2.0f, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cy.y - 4.0f, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cz.z - 8.0f, 1e-6f );
|
||||
|
||||
// Identity rotation, so the world inverse inertia is just the local inverse: diag(1/2, 1/4, 1/8).
|
||||
b3Matrix3 invWorld = b3Body_GetWorldInverseRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( invWorld.cx.x - 0.5f, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cy.y - 0.25f, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cz.z - 0.125f, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cy.x, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cz.x, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cx.y, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cz.y, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cx.z, 1e-5f );
|
||||
ENSURE_SMALL( invWorld.cy.z, 1e-5f );
|
||||
|
||||
// World center of mass is the body origin plus the local center under identity rotation.
|
||||
b3Pos worldCenter = b3Body_GetWorldCenterOfMass( bodyId );
|
||||
ENSURE_SMALL( worldCenter.x - ( 5.0f + center.x ), 1e-5f );
|
||||
ENSURE_SMALL( worldCenter.y - ( -3.0f + center.y ), 1e-5f );
|
||||
ENSURE_SMALL( worldCenter.z - ( 2.0f + center.z ), 1e-5f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The world inverse inertia must be the local inverse rotated into world space. A 90 degree turn about
|
||||
// z swaps the x and y principal moments, so diag(1/2, 1/4, 1/8) becomes diag(1/4, 1/2, 1/8). This is the
|
||||
// regression guard: before SetMassData rotated the tensor it left the world inverse inertia stale.
|
||||
static int SetMassDataWorldInertiaRotated( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 0.5f * B3_PI );
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3MassData massData = { 1.0f, { 0.0f, 0.0f, 0.0f }, kDiagInertia };
|
||||
b3Body_SetMassData( bodyId, massData );
|
||||
|
||||
// The local inertia is stored untouched by the world transform.
|
||||
b3Matrix3 localInertia = b3Body_GetLocalRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( localInertia.cx.x - 2.0f, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cy.y - 4.0f, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cz.z - 8.0f, 1e-6f );
|
||||
|
||||
b3Matrix3 invWorld = b3Body_GetWorldInverseRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( invWorld.cx.x - 0.25f, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cy.y - 0.5f, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cz.z - 0.125f, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cy.x, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cz.x, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cx.y, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cz.y, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cx.z, 1e-4f );
|
||||
ENSURE_SMALL( invWorld.cy.z, 1e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Fixed rotation must leave the mass intact but drive the whole angular inertia to zero, even when the
|
||||
// caller hands in a real tensor.
|
||||
static int SetMassDataFixedRotation( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.motionLocks.angularX = true;
|
||||
bodyDef.motionLocks.angularY = true;
|
||||
bodyDef.motionLocks.angularZ = true;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3MassData massData = { 5.0f, { 0.0f, 0.0f, 0.0f }, kDiagInertia };
|
||||
b3Body_SetMassData( bodyId, massData );
|
||||
|
||||
ENSURE_SMALL( b3Body_GetMass( bodyId ) - 5.0f, 1e-6f );
|
||||
ENSURE_SMALL( b3Body_GetInverseMass( bodyId ) - 0.2f, 1e-6f );
|
||||
|
||||
b3Matrix3 localInertia = b3Body_GetLocalRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( localInertia.cx.x, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cy.y, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cz.z, 1e-6f );
|
||||
|
||||
b3Matrix3 invWorld = b3Body_GetWorldInverseRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( invWorld.cx.x, 1e-6f );
|
||||
ENSURE_SMALL( invWorld.cy.y, 1e-6f );
|
||||
ENSURE_SMALL( invWorld.cz.z, 1e-6f );
|
||||
|
||||
b3MassData md = b3Body_GetMassData( bodyId );
|
||||
ENSURE_SMALL( md.inertia.cx.x, 1e-6f );
|
||||
ENSURE_SMALL( md.inertia.cy.y, 1e-6f );
|
||||
ENSURE_SMALL( md.inertia.cz.z, 1e-6f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Zero mass and a zero tensor have zero determinant, so the inverses must collapse to zero rather than
|
||||
// divide by it.
|
||||
static int SetMassDataZeroMass( void )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3MassData massData = { 0.0f, { 0.0f, 0.0f, 0.0f }, b3Mat3_zero };
|
||||
b3Body_SetMassData( bodyId, massData );
|
||||
|
||||
ENSURE_SMALL( b3Body_GetInverseMass( bodyId ), 1e-6f );
|
||||
|
||||
b3Matrix3 localInertia = b3Body_GetLocalRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( localInertia.cx.x, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cy.y, 1e-6f );
|
||||
ENSURE_SMALL( localInertia.cz.z, 1e-6f );
|
||||
|
||||
b3Matrix3 invWorld = b3Body_GetWorldInverseRotationalInertia( bodyId );
|
||||
ENSURE_SMALL( invWorld.cx.x, 1e-6f );
|
||||
ENSURE_SMALL( invWorld.cy.y, 1e-6f );
|
||||
ENSURE_SMALL( invWorld.cz.z, 1e-6f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BodyTest( void )
|
||||
{
|
||||
RUN_SUBTEST( FarSingleSphereMass );
|
||||
RUN_SUBTEST( FarCubeSphereMass );
|
||||
RUN_SUBTEST( DeferredMassExtents );
|
||||
RUN_SUBTEST( SetMassDataRoundTrip );
|
||||
RUN_SUBTEST( SetMassDataWorldInertiaRotated );
|
||||
RUN_SUBTEST( SetMassDataFixedRotation );
|
||||
RUN_SUBTEST( SetMassDataZeroMass );
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1729,6 +1729,126 @@ static int GeometryHashCollision( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Staged stepping must reveal a mid-stream body at its creation transform. A body created and given an
|
||||
// impulse in one recorded step is first placed by CreateBody, then displaced by the following Step. Atomic
|
||||
// replay fuses the two, so the body is only ever seen already moved. Staged replay parks between them so
|
||||
// the pre-integration pose is drawable. Verify the parked pose is the creation transform, that atomic
|
||||
// replay does not show it, and that the extra park does not perturb the end state.
|
||||
static int StagedStepCreationPose( void )
|
||||
{
|
||||
b3Recording* rec = b3CreateRecording( 0 );
|
||||
ENSURE( rec != NULL );
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
b3World_SetGravity( worldId, (b3Vec3){ 0.0f, -10.0f, 0.0f } );
|
||||
|
||||
b3World_StartRecording( worldId, rec );
|
||||
|
||||
float timeStep = 1.0f / 60.0f;
|
||||
int subStepCount = 4;
|
||||
|
||||
// A few empty steps so the creation lands inside the stream, not at frame 0.
|
||||
const int leadFrames = 3;
|
||||
for ( int i = 0; i < leadFrames; ++i )
|
||||
{
|
||||
b3World_Step( worldId, timeStep, subStepCount );
|
||||
}
|
||||
|
||||
// The creation transform under test. No ground, so the body is the only create and stays ballistic.
|
||||
// Components are exact in float so the round-trip pose compares tight.
|
||||
const b3Pos spawn = { 1.0, 20.0, -2.0 };
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = spawn;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
// Impulse along +x so the first integrated step moves the body a visible distance off the spawn.
|
||||
b3Body_ApplyLinearImpulseToCenter( bodyId, (b3Vec3){ 5.0f, 0.0f, 0.0f }, true );
|
||||
|
||||
const int creationFrame = leadFrames + 1;
|
||||
b3World_Step( worldId, timeStep, subStepCount ); // advances to creationFrame
|
||||
|
||||
const int totalFrames = creationFrame + 3;
|
||||
for ( int i = creationFrame; i < totalFrames; ++i )
|
||||
{
|
||||
b3World_Step( worldId, timeStep, subStepCount );
|
||||
}
|
||||
|
||||
b3World_StopRecording( worldId );
|
||||
b3DestroyWorld( worldId );
|
||||
|
||||
const uint8_t* data = b3Recording_GetData( rec );
|
||||
int sz = b3Recording_GetSize( rec );
|
||||
|
||||
// Atomic replay fuses the create and its step, so at the creation frame the body is already
|
||||
// integrated and displaced along +x. Capture that pose and the final state hash.
|
||||
b3RecPlayer* atomic = b3RecPlayer_Create( data, sz, 1 );
|
||||
ENSURE( atomic != NULL );
|
||||
b3Pos atomicPose = { 0.0, 0.0, 0.0 };
|
||||
while ( !b3RecPlayer_IsAtEnd( atomic ) )
|
||||
{
|
||||
b3RecPlayer_StepFrame( atomic );
|
||||
if ( b3RecPlayer_GetFrame( atomic ) == creationFrame )
|
||||
{
|
||||
b3BodyId id = b3RecPlayer_GetBodyId( atomic, 0 );
|
||||
ENSURE( b3Body_IsValid( id ) );
|
||||
atomicPose = b3Body_GetPosition( id );
|
||||
}
|
||||
}
|
||||
ENSURE( b3RecPlayer_GetFrame( atomic ) == totalFrames );
|
||||
ENSURE( !b3RecPlayer_HasDiverged( atomic ) );
|
||||
ENSURE( atomicPose.x - spawn.x > 0.01 ); // the impulse moved it before it was ever seen
|
||||
uint64_t atomicHash = b3HashWorldState( b3GetWorldFromId( b3RecPlayer_GetWorldId( atomic ) ) );
|
||||
b3RecPlayer_Destroy( atomic );
|
||||
|
||||
// Staged replay: step forward until the first pre-step park. It must sit at the creation frame's
|
||||
// pre-integration state with the new body at exactly its spawn transform.
|
||||
b3RecPlayer* staged = b3RecPlayer_Create( data, sz, 1 );
|
||||
ENSURE( staged != NULL );
|
||||
while ( !b3RecPlayer_IsAtEnd( staged ) )
|
||||
{
|
||||
b3RecPlayer_SubStepFrame( staged );
|
||||
if ( b3RecPlayer_IsAtPreStep( staged ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
ENSURE( b3RecPlayer_IsAtPreStep( staged ) );
|
||||
ENSURE( b3RecPlayer_GetFrame( staged ) == creationFrame - 1 ); // parked before the step, frame not advanced
|
||||
|
||||
b3BodyId stagedId = b3RecPlayer_GetBodyId( staged, 0 );
|
||||
ENSURE( b3Body_IsValid( stagedId ) );
|
||||
b3Pos parkPose = b3Body_GetPosition( stagedId );
|
||||
ENSURE_SMALL( parkPose.x - spawn.x, 1.0e-4 );
|
||||
ENSURE_SMALL( parkPose.y - spawn.y, 1.0e-4 );
|
||||
ENSURE_SMALL( parkPose.z - spawn.z, 1.0e-4 );
|
||||
|
||||
// Finishing the frame integrates the body, so it leaves the spawn pose on the next advance.
|
||||
b3RecPlayer_SubStepFrame( staged );
|
||||
ENSURE( b3RecPlayer_GetFrame( staged ) == creationFrame );
|
||||
ENSURE( !b3RecPlayer_IsAtPreStep( staged ) );
|
||||
|
||||
// Run staged to the end: the same op stream, so it must land on the atomic end state bit for bit.
|
||||
while ( !b3RecPlayer_IsAtEnd( staged ) )
|
||||
{
|
||||
b3RecPlayer_SubStepFrame( staged );
|
||||
}
|
||||
ENSURE( b3RecPlayer_GetFrame( staged ) == totalFrames );
|
||||
ENSURE( !b3RecPlayer_HasDiverged( staged ) );
|
||||
uint64_t stagedHash = b3HashWorldState( b3GetWorldFromId( b3RecPlayer_GetWorldId( staged ) ) );
|
||||
ENSURE( stagedHash == atomicHash );
|
||||
b3RecPlayer_Destroy( staged );
|
||||
|
||||
b3DestroyRecording( rec );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RecordingTest( void )
|
||||
{
|
||||
RUN_SUBTEST( GeometryHashCollision );
|
||||
|
|
@ -1737,6 +1857,7 @@ int RecordingTest( void )
|
|||
RUN_SUBTEST( HullDedup );
|
||||
RUN_SUBTEST( MidStreamNoContacts );
|
||||
RUN_SUBTEST( MidStreamContacts );
|
||||
RUN_SUBTEST( StagedStepCreationPose );
|
||||
RUN_SUBTEST( ScrubBackward );
|
||||
RUN_SUBTEST( SeekWithHull );
|
||||
RUN_SUBTEST( DebugShapeCallbacks );
|
||||
|
|
|
|||
Loading…
Reference in a new issue