dynamic mover

This commit is contained in:
Erin Catto 2026-10-20 23:33:18 -07:00
commit f0b1ed2fca
29 changed files with 3078 additions and 432 deletions

View file

@ -296,6 +296,102 @@ static int TestMotorJoint( void )
return FinishJoint( jointId, f.worldId );
}
static int TestMoverJoint( void )
{
JointFixture f = CreateJointFixture();
b3MoverJointDef def = b3DefaultMoverJointDef();
SetCommonFrames( &def.base, &f );
b3JointId jointId = b3CreateMoverJoint( f.worldId, &def );
if ( ExerciseJointBase( jointId, f.worldId, f.groundId, f.bodyId, b3_moverJoint ) != 0 )
{
return 1;
}
b3Vec3 velocity = { 1.0f, 2.0f, -3.0f };
b3MoverJoint_SetLinearVelocity( jointId, velocity );
b3Vec3 gotVelocity = b3MoverJoint_GetLinearVelocity( jointId );
ENSURE( gotVelocity.x == 1.0f && gotVelocity.y == 2.0f && gotVelocity.z == -3.0f );
// The y axis has no force budget, so it must stay at rest while x and z reach the target
b3Vec3 maxForce = { 1000.0f, 0.0f, 1000.0f };
b3MoverJoint_SetMaxVelocityForce( jointId, maxForce );
b3Vec3 gotForce = b3MoverJoint_GetMaxVelocityForce( jointId );
ENSURE( gotForce.x == 1000.0f && gotForce.y == 0.0f && gotForce.z == 1000.0f );
for ( int i = 0; i < 30; ++i )
{
b3World_Step( f.worldId, 1.0f / 60.0f, 4 );
}
b3Vec3 v = b3Body_GetLinearVelocity( f.bodyId );
ENSURE_SMALL( v.x - 1.0f, 1.0e-4f );
ENSURE_SMALL( v.y, 1.0e-4f );
ENSURE_SMALL( v.z + 3.0f, 1.0e-4f );
b3Vec3 w = b3Body_GetAngularVelocity( f.bodyId );
ENSURE( w.x == 0.0f && w.y == 0.0f && w.z == 0.0f );
return FinishJoint( jointId, f.worldId );
}
// A box held up by a pogo spring under gravity should settle at the rest length
static int TestPogoJoint( void )
{
JointFixture f = CreateJointFixture();
b3World_SetGravity( f.worldId, (b3Vec3){ 0.0f, -10.0f, 0.0f } );
float restLength = 2.0f;
float mass = b3Body_GetMass( f.bodyId );
b3PogoJointDef def = b3DefaultPogoJointDef();
def.base.bodyIdA = f.groundId;
def.base.bodyIdB = f.bodyId;
def.base.localFrameA.p = (b3Vec3){ 0.0f, 1.0f, 0.0f };
def.base.localFrameB.p = (b3Vec3){ 0.0f, 0.0f, 0.0f };
def.normal = b3Vec3_axisY;
def.restLength = restLength;
def.hertz = 2.0f;
def.dampingRatio = 1.0f;
def.maxCompressionForce = 100.0f * 10.0f * mass;
def.maxTensionForce = 100.0f * 10.0f * mass;
b3JointId jointId = b3CreatePogoJoint( f.worldId, &def );
if ( ExerciseJointBase( jointId, f.worldId, f.groundId, f.bodyId, b3_pogoJoint ) != 0 )
{
return 1;
}
b3PogoJoint_SetRestLength( jointId, restLength );
ENSURE( b3PogoJoint_GetRestLength( jointId ) == restLength );
b3PogoJoint_SetSpringHertz( jointId, 3.0f );
ENSURE( b3PogoJoint_GetSpringHertz( jointId ) == 3.0f );
b3PogoJoint_SetSpringDampingRatio( jointId, 0.9f );
ENSURE( b3PogoJoint_GetSpringDampingRatio( jointId ) == 0.9f );
// Starts stretched by one meter
ENSURE_SMALL( b3PogoJoint_GetCurrentLength( jointId ) - 3.0f, 1.0e-5f );
for ( int i = 0; i < 300; ++i )
{
b3World_Step( f.worldId, 1.0f / 60.0f, 4 );
}
ENSURE_SMALL( b3PogoJoint_GetCurrentLength( jointId ) - restLength, 0.01f );
ENSURE_SMALL( b3Body_GetLinearVelocity( f.bodyId ).y, 0.01f );
// Holding the weight takes an impulse of m * g * h each sub-step
ENSURE_SMALL( b3PogoJoint_GetImpulse( jointId ) - mass * 10.0f / 240.0f, 0.001f * mass );
b3Vec3 force = b3Joint_GetConstraintForce( jointId );
ENSURE_SMALL( force.y - mass * 10.0f, 0.1f * mass );
return FinishJoint( jointId, f.worldId );
}
static int TestPrismaticJoint( void )
{
JointFixture f = CreateJointFixture();
@ -673,6 +769,8 @@ int JointTest( void )
RUN_SUBTEST( TestDistanceJoint );
RUN_SUBTEST( TestFilterJoint );
RUN_SUBTEST( TestMotorJoint );
RUN_SUBTEST( TestMoverJoint );
RUN_SUBTEST( TestPogoJoint );
RUN_SUBTEST( TestPrismaticJoint );
RUN_SUBTEST( TestRevoluteJoint );
RUN_SUBTEST( TestSphericalJoint );

View file

@ -1569,6 +1569,43 @@ static int AllOps( void )
b3ParallelJoint_SetSpringDampingRatio( parallelId, 0.5f );
b3ParallelJoint_SetMaxTorque( parallelId, 20.0f );
// Dynamic mover: a rotation locked capsule driven by a mover joint and held up by a pogo joint
b3BodyDef moverBodyDef = b3DefaultBodyDef();
moverBodyDef.type = b3_dynamicBody;
moverBodyDef.position = (b3Pos){ -20.0f, 3.0f, 0.0f };
moverBodyDef.motionLocks =
(b3MotionLocks){ .angularX = true, .angularY = true, .angularZ = true };
b3BodyId moverBodyId = b3CreateBody( worldId, &moverBodyDef );
b3ShapeDef moverShapeDef = b3DefaultShapeDef();
moverShapeDef.density = 1.0f;
moverShapeDef.baseMaterial.friction = 0.0f;
b3Capsule moverCapsule = { { 0.0f, -0.5f, 0.0f }, { 0.0f, 0.5f, 0.0f }, 0.3f };
b3CreateCapsuleShape( moverBodyId, &moverShapeDef, &moverCapsule );
b3MoverJointDef moverDef = b3DefaultMoverJointDef();
moverDef.base.bodyIdA = groundId;
moverDef.base.bodyIdB = moverBodyId;
moverDef.base.collideConnected = true;
moverDef.linearVelocity = (b3Vec3){ 1.0f, 0.0f, 0.5f };
moverDef.maxVelocityForce = (b3Vec3){ 0.5f, 0.0f, 0.5f };
b3JointId moverJointId = b3CreateMoverJoint( worldId, &moverDef );
b3PogoJointDef pogoDef = b3DefaultPogoJointDef();
pogoDef.base.bodyIdA = groundId;
pogoDef.base.bodyIdB = moverBodyId;
pogoDef.base.localFrameA.p = (b3Vec3){ -20.0f, 1.0f, 0.0f };
pogoDef.base.localFrameB.p = moverCapsule.center1;
pogoDef.base.collideConnected = true;
pogoDef.restLength = 1.0f;
pogoDef.hertz = 5.0f;
pogoDef.dampingRatio = 0.8f;
pogoDef.maxCompressionForce = 1000.0f;
pogoDef.maxTensionForce = 1000.0f;
b3JointId pogoJointId = b3CreatePogoJoint( worldId, &pogoDef );
b3PogoJoint_SetRestLength( pogoJointId, 0.9f );
b3PogoJoint_SetSpringHertz( pogoJointId, 4.0f );
b3PogoJoint_SetSpringDampingRatio( pogoJointId, 0.7f );
// World config mutators
b3World_SetGravity( worldId, (b3Vec3){ 0.0f, -9.8f, 0.0f } );
b3World_EnableSleeping( worldId, true );
@ -1632,8 +1669,21 @@ static int AllOps( void )
b3Body_ApplyLinearImpulseToCenter( capsuleBodyId, (b3Vec3){ 2.0f, 0.0f, 0.0f }, true );
b3Body_SetGravityScale( bodyId, 1.0f );
b3Body_SetSafetyFactor( fastId, 0.4f );
b3MoverJoint_SetMaxVelocityForce( moverJointId, (b3Vec3){ 60.0f, 0.0f, 60.0f } );
}
// The dynamic mover rebuilds its pogo every step, warm started from the old one
if ( i >= 6 )
{
pogoDef.impulse = b3PogoJoint_GetImpulse( pogoJointId );
pogoDef.velocity = b3PogoJoint_GetCurrentSpeed( pogoJointId );
pogoDef.base.localFrameA.p =
b3Body_GetLocalPoint( groundId, b3Body_GetPosition( moverBodyId ) );
pogoDef.base.localFrameA.p.y = 1.0f;
b3DestroyJoint( pogoJointId, false );
pogoJointId = b3CreatePogoJoint( worldId, &pogoDef );
b3MoverJoint_SetLinearVelocity( moverJointId, (b3Vec3){ -0.5f, 0.0f, 1.0f } );
}
// Issue queries mid-loop to exercise recording across steps
if ( i == 3 )
{