Dynamic Mover (#202)
Port the dynamic character mover from Box2D including the mover joint and pogo joint. Match the mover and sample structure of Box2D.
This commit is contained in:
parent
f36d061ba2
commit
3362633204
41 changed files with 4580 additions and 885 deletions
|
|
@ -51,6 +51,7 @@ set(BOX3D_SOURCE_FILES
|
|||
mesh.c
|
||||
motor_joint.c
|
||||
mover.c
|
||||
mover_joint.c
|
||||
name_cache.c
|
||||
name_cache.h
|
||||
parallel_for.c
|
||||
|
|
@ -59,6 +60,7 @@ set(BOX3D_SOURCE_FILES
|
|||
physics_world.c
|
||||
physics_world.h
|
||||
platform.h
|
||||
pogo_joint.c
|
||||
prismatic_joint.c
|
||||
qsort.h
|
||||
rapidhash.h
|
||||
|
|
|
|||
197
src/joint.c
197
src/joint.c
|
|
@ -62,6 +62,21 @@ b3MotorJointDef b3DefaultMotorJointDef( void )
|
|||
return def;
|
||||
}
|
||||
|
||||
b3MoverJointDef b3DefaultMoverJointDef( void )
|
||||
{
|
||||
b3MoverJointDef def = { 0 };
|
||||
def.base = b3DefaultJointDef();
|
||||
return def;
|
||||
}
|
||||
|
||||
b3PogoJointDef b3DefaultPogoJointDef( void )
|
||||
{
|
||||
b3PogoJointDef def = { 0 };
|
||||
def.base = b3DefaultJointDef();
|
||||
def.normal = b3Vec3_axisY;
|
||||
return def;
|
||||
}
|
||||
|
||||
b3FilterJointDef b3DefaultFilterJointDef( void )
|
||||
{
|
||||
b3FilterJointDef def = { 0 };
|
||||
|
|
@ -315,7 +330,8 @@ static b3JointPair b3CreateJoint( b3World* world, const b3JointDef* def, b3Joint
|
|||
else
|
||||
{
|
||||
// joint connected between sleeping and/or static bodies
|
||||
B3_ASSERT( bodyA->setIndex >= b3_firstSleepingSet || bodyB->setIndex >= b3_firstSleepingSet );
|
||||
B3_ASSERT( bodyA->setIndex >= b3_firstSleepingSet ||
|
||||
bodyB->setIndex >= b3_firstSleepingSet );
|
||||
B3_ASSERT( bodyA->setIndex != b3_staticSet || bodyB->setIndex != b3_staticSet );
|
||||
|
||||
// joint should go into the sleeping set (not static set)
|
||||
|
|
@ -511,6 +527,66 @@ b3JointId b3CreateMotorJoint( b3WorldId worldId, const b3MotorJointDef* def )
|
|||
return jointId;
|
||||
}
|
||||
|
||||
b3JointId b3CreateMoverJoint( b3WorldId worldId, const b3MoverJointDef* def )
|
||||
{
|
||||
B3_CHECK_JOINT_DEF( def );
|
||||
B3_ASSERT( b3IsValidVec3( def->linearVelocity ) );
|
||||
B3_ASSERT( b3IsValidFloat( def->maxForce ) && def->maxForce >= 0.0f );
|
||||
|
||||
b3World* world = b3GetUnlockedWorldFromId( worldId );
|
||||
if ( world == NULL )
|
||||
{
|
||||
return (b3JointId){ 0 };
|
||||
}
|
||||
|
||||
b3JointPair pair = b3CreateJoint( world, &def->base, b3_moverJoint );
|
||||
b3JointSim* joint = pair.jointSim;
|
||||
|
||||
joint->moverJoint = (b3MoverJoint){ 0 };
|
||||
joint->moverJoint.linearVelocity = def->linearVelocity;
|
||||
joint->moverJoint.maxForce = def->maxForce;
|
||||
|
||||
b3JointId jointId = { joint->jointId + 1, world->worldId, pair.joint->generation };
|
||||
B3_REC_CREATE( world, CreateMoverJoint, jointId, worldId, *def );
|
||||
return jointId;
|
||||
}
|
||||
|
||||
b3JointId b3CreatePogoJoint( b3WorldId worldId, const b3PogoJointDef* def )
|
||||
{
|
||||
B3_CHECK_JOINT_DEF( def );
|
||||
B3_ASSERT( b3IsValidVec3( def->normal ) && b3IsNormalized( def->normal ) );
|
||||
B3_ASSERT( b3IsValidFloat( def->restLength ) && def->restLength >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->hertz ) && def->hertz >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->dampingRatio ) && def->dampingRatio >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->maxTensionForce ) && def->maxTensionForce >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->maxCompressionForce ) && def->maxCompressionForce >= 0.0f );
|
||||
B3_ASSERT( b3IsValidFloat( def->impulse ) );
|
||||
B3_ASSERT( b3IsValidFloat( def->velocity ) );
|
||||
|
||||
b3World* world = b3GetUnlockedWorldFromId( worldId );
|
||||
if ( world == NULL )
|
||||
{
|
||||
return (b3JointId){ 0 };
|
||||
}
|
||||
|
||||
b3JointPair pair = b3CreateJoint( world, &def->base, b3_pogoJoint );
|
||||
b3JointSim* joint = pair.jointSim;
|
||||
|
||||
joint->pogoJoint = (b3PogoJoint){ 0 };
|
||||
joint->pogoJoint.normal = def->normal;
|
||||
joint->pogoJoint.restLength = def->restLength;
|
||||
joint->pogoJoint.hertz = def->hertz;
|
||||
joint->pogoJoint.dampingRatio = def->dampingRatio;
|
||||
joint->pogoJoint.maxTensionForce = def->maxTensionForce;
|
||||
joint->pogoJoint.maxCompressionForce = def->maxCompressionForce;
|
||||
joint->pogoJoint.impulse = def->impulse;
|
||||
joint->pogoJoint.velocity = def->velocity;
|
||||
|
||||
b3JointId jointId = { joint->jointId + 1, world->worldId, pair.joint->generation };
|
||||
B3_REC_CREATE( world, CreatePogoJoint, jointId, worldId, *def );
|
||||
return jointId;
|
||||
}
|
||||
|
||||
b3JointId b3CreateFilterJoint( b3WorldId worldId, const b3FilterJointDef* def )
|
||||
{
|
||||
B3_CHECK_JOINT_DEF( def );
|
||||
|
|
@ -647,8 +723,10 @@ b3JointId b3CreateSphericalJoint( b3WorldId worldId, const b3SphericalJointDef*
|
|||
|
||||
float lowerAngle = b3MinFloat( def->lowerTwistAngle, def->upperTwistAngle );
|
||||
float upperAngle = b3MaxFloat( def->lowerTwistAngle, def->upperTwistAngle );
|
||||
joint->sphericalJoint.lowerTwistAngle = b3ClampFloat( lowerAngle, -0.99f * B3_PI, 0.99f * B3_PI );
|
||||
joint->sphericalJoint.upperTwistAngle = b3ClampFloat( upperAngle, -0.99f * B3_PI, 0.99f * B3_PI );
|
||||
joint->sphericalJoint.lowerTwistAngle =
|
||||
b3ClampFloat( lowerAngle, -0.99f * B3_PI, 0.99f * B3_PI );
|
||||
joint->sphericalJoint.upperTwistAngle =
|
||||
b3ClampFloat( upperAngle, -0.99f * B3_PI, 0.99f * B3_PI );
|
||||
|
||||
joint->sphericalJoint.maxMotorTorque = def->maxMotorTorque;
|
||||
joint->sphericalJoint.motorVelocity = def->motorVelocity;
|
||||
|
|
@ -805,7 +883,8 @@ void b3DestroyJointInternal( b3World* world, b3Joint* joint, bool wakeBodies )
|
|||
|
||||
if ( setIndex == b3_awakeSet )
|
||||
{
|
||||
b3RemoveJointFromGraph( world, joint->edges[0].bodyId, joint->edges[1].bodyId, joint->colorIndex, localIndex );
|
||||
b3RemoveJointFromGraph( world, joint->edges[0].bodyId, joint->edges[1].bodyId,
|
||||
joint->colorIndex, localIndex );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -1017,7 +1096,8 @@ bool b3Joint_IsAwake( b3JointId jointId )
|
|||
return joint->setIndex == b3_awakeSet;
|
||||
}
|
||||
|
||||
void b3GetJointReaction( b3World* world, b3JointSim* sim, float invTimeStep, float* force, float* torque )
|
||||
void b3GetJointReaction( b3World* world, b3JointSim* sim, float invTimeStep, float* force,
|
||||
float* torque )
|
||||
{
|
||||
float linearImpulse = 0.0f;
|
||||
float angularImpulse = 0.0f;
|
||||
|
|
@ -1039,15 +1119,18 @@ void b3GetJointReaction( b3World* world, b3JointSim* sim, float invTimeStep, flo
|
|||
case b3_distanceJoint:
|
||||
{
|
||||
b3DistanceJoint* joint = &sim->distanceJoint;
|
||||
linearImpulse = b3AbsFloat( joint->impulse + joint->lowerImpulse - joint->upperImpulse + joint->motorImpulse );
|
||||
linearImpulse = b3AbsFloat( joint->impulse + joint->lowerImpulse - joint->upperImpulse +
|
||||
joint->motorImpulse );
|
||||
}
|
||||
break;
|
||||
|
||||
case b3_motorJoint:
|
||||
{
|
||||
b3MotorJoint* joint = &sim->motorJoint;
|
||||
linearImpulse = b3Length( b3Add( joint->linearVelocityImpulse, joint->linearSpringImpulse ) );
|
||||
angularImpulse = b3Length( b3Add( joint->angularVelocityImpulse, joint->angularSpringImpulse ) );
|
||||
linearImpulse =
|
||||
b3Length( b3Add( joint->linearVelocityImpulse, joint->linearSpringImpulse ) );
|
||||
angularImpulse =
|
||||
b3Length( b3Add( joint->angularVelocityImpulse, joint->angularSpringImpulse ) );
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -1094,7 +1177,8 @@ void b3GetJointReaction( b3World* world, b3JointSim* sim, float invTimeStep, flo
|
|||
b3Vec3 swingAxis = b3Normalize( b3Cross( coneAxis, twistAxis ) );
|
||||
|
||||
b3Vec3 impulse = b3Add( joint->springImpulse, joint->motorImpulse );
|
||||
impulse = b3MulAdd( impulse, joint->lowerTwistImpulse - joint->upperTwistImpulse, twistAxis );
|
||||
impulse =
|
||||
b3MulAdd( impulse, joint->lowerTwistImpulse - joint->upperTwistImpulse, twistAxis );
|
||||
impulse = b3MulAdd( impulse, joint->swingImpulse, swingAxis );
|
||||
|
||||
angularImpulse = b3Length( impulse );
|
||||
|
|
@ -1114,12 +1198,28 @@ void b3GetJointReaction( b3World* world, b3JointSim* sim, float invTimeStep, flo
|
|||
// todo probably wrong
|
||||
b3WheelJoint* joint = &sim->wheelJoint;
|
||||
b3Vec2 perpImpulse = joint->linearImpulse;
|
||||
float axialImpulse = joint->suspensionSpringImpulse + joint->lowerSuspensionImpulse - joint->upperSuspensionImpulse;
|
||||
linearImpulse = sqrtf( perpImpulse.x * perpImpulse.x + perpImpulse.y * perpImpulse.y + axialImpulse * axialImpulse );
|
||||
float axialImpulse = joint->suspensionSpringImpulse + joint->lowerSuspensionImpulse -
|
||||
joint->upperSuspensionImpulse;
|
||||
linearImpulse = sqrtf( perpImpulse.x * perpImpulse.x + perpImpulse.y * perpImpulse.y +
|
||||
axialImpulse * axialImpulse );
|
||||
angularImpulse = b3AbsFloat( joint->spinImpulse );
|
||||
}
|
||||
break;
|
||||
|
||||
case b3_moverJoint:
|
||||
{
|
||||
b3MoverJoint* joint = &sim->moverJoint;
|
||||
linearImpulse = b3Length2( joint->impulse );
|
||||
}
|
||||
break;
|
||||
|
||||
case b3_pogoJoint:
|
||||
{
|
||||
b3PogoJoint* joint = &sim->pogoJoint;
|
||||
linearImpulse = b3AbsFloat( joint->impulse );
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
@ -1161,6 +1261,12 @@ static b3Vec3 b3GetJointConstraintForce( b3World* world, b3Joint* joint )
|
|||
case b3_wheelJoint:
|
||||
return b3GetWheelJointForce( world, base );
|
||||
|
||||
case b3_moverJoint:
|
||||
return b3GetMoverJointForce( world, base );
|
||||
|
||||
case b3_pogoJoint:
|
||||
return b3GetPogoJointForce( world, base );
|
||||
|
||||
default:
|
||||
B3_ASSERT( false );
|
||||
return b3Vec3_zero;
|
||||
|
|
@ -1200,6 +1306,10 @@ static b3Vec3 b3GetJointConstraintTorque( b3World* world, b3Joint* joint )
|
|||
case b3_wheelJoint:
|
||||
return b3GetWheelJointTorque( world, base );
|
||||
|
||||
case b3_moverJoint:
|
||||
case b3_pogoJoint:
|
||||
return b3Vec3_zero;
|
||||
|
||||
default:
|
||||
B3_ASSERT( false );
|
||||
return b3Vec3_zero;
|
||||
|
|
@ -1268,6 +1378,12 @@ float b3Joint_GetLinearSeparation( b3JointId jointId )
|
|||
case b3_motorJoint:
|
||||
return 0.0f;
|
||||
|
||||
case b3_moverJoint:
|
||||
return 0.0f;
|
||||
|
||||
case b3_pogoJoint:
|
||||
return 0.0f;
|
||||
|
||||
case b3_filterJoint:
|
||||
return 0.0f;
|
||||
|
||||
|
|
@ -1293,7 +1409,8 @@ float b3Joint_GetLinearSeparation( b3JointId jointId )
|
|||
}
|
||||
}
|
||||
|
||||
return sqrtf( perpendicularSeparation * perpendicularSeparation + limitSeparation * limitSeparation );
|
||||
return sqrtf( perpendicularSeparation * perpendicularSeparation +
|
||||
limitSeparation * limitSeparation );
|
||||
}
|
||||
|
||||
case b3_revoluteJoint:
|
||||
|
|
@ -1335,7 +1452,8 @@ float b3Joint_GetLinearSeparation( b3JointId jointId )
|
|||
}
|
||||
}
|
||||
|
||||
return sqrtf( perpendicularSeparation * perpendicularSeparation + limitSeparation * limitSeparation );
|
||||
return sqrtf( perpendicularSeparation * perpendicularSeparation +
|
||||
limitSeparation * limitSeparation );
|
||||
}
|
||||
|
||||
default:
|
||||
|
|
@ -1370,6 +1488,12 @@ float b3Joint_GetAngularSeparation( b3JointId jointId )
|
|||
case b3_motorJoint:
|
||||
return 0.0f;
|
||||
|
||||
case b3_moverJoint:
|
||||
return 0.0f;
|
||||
|
||||
case b3_pogoJoint:
|
||||
return 0.0f;
|
||||
|
||||
case b3_filterJoint:
|
||||
return 0.0f;
|
||||
|
||||
|
|
@ -1522,6 +1646,14 @@ void b3PrepareJoint( b3JointSim* joint, b3StepContext* context )
|
|||
b3PrepareWheelJoint( joint, context );
|
||||
break;
|
||||
|
||||
case b3_moverJoint:
|
||||
b3PrepareMoverJoint( joint, context );
|
||||
break;
|
||||
|
||||
case b3_pogoJoint:
|
||||
b3PreparePogoJoint( joint, context );
|
||||
break;
|
||||
|
||||
default:
|
||||
B3_ASSERT( false );
|
||||
}
|
||||
|
|
@ -1566,6 +1698,14 @@ void b3WarmStartJoint( b3JointSim* joint, b3StepContext* context )
|
|||
b3WarmStartWheelJoint( joint, context );
|
||||
break;
|
||||
|
||||
case b3_moverJoint:
|
||||
b3WarmStartMoverJoint( joint, context );
|
||||
break;
|
||||
|
||||
case b3_pogoJoint:
|
||||
b3WarmStartPogoJoint( joint, context );
|
||||
break;
|
||||
|
||||
default:
|
||||
B3_ASSERT( false );
|
||||
}
|
||||
|
|
@ -1612,6 +1752,14 @@ void b3SolveJoint( b3JointSim* joint, b3StepContext* context, bool useBias )
|
|||
b3SolveWheelJoint( joint, context, useBias );
|
||||
break;
|
||||
|
||||
case b3_moverJoint:
|
||||
b3SolveMoverJoint( joint, context );
|
||||
break;
|
||||
|
||||
case b3_pogoJoint:
|
||||
b3SolvePogoJoint( joint, context, useBias );
|
||||
break;
|
||||
|
||||
default:
|
||||
B3_ASSERT( false );
|
||||
}
|
||||
|
|
@ -1728,6 +1876,14 @@ void b3DrawJoint( b3DebugDraw* draw, b3World* world, b3Joint* joint )
|
|||
b3DrawWheelJoint( draw, jointSim, transformA, transformB, scale );
|
||||
break;
|
||||
|
||||
case b3_moverJoint:
|
||||
b3DrawMoverJoint( draw, world, jointSim, transformA, transformB );
|
||||
break;
|
||||
|
||||
case b3_pogoJoint:
|
||||
b3DrawPogoJoint( draw, world, jointSim, transformA, transformB, scale );
|
||||
break;
|
||||
|
||||
default:
|
||||
draw->DrawSegmentFcn( transformA.p, pA, color, draw->context );
|
||||
draw->DrawSegmentFcn( pA, pB, color, draw->context );
|
||||
|
|
@ -1738,10 +1894,12 @@ void b3DrawJoint( b3DebugDraw* draw, b3World* world, b3Joint* joint )
|
|||
if ( draw->drawGraphColors )
|
||||
{
|
||||
b3HexColor graphColors[B3_GRAPH_COLOR_COUNT] = {
|
||||
b3_colorRed, b3_colorOrange, b3_colorYellow, b3_colorGreen, b3_colorCyan, b3_colorBlue,
|
||||
b3_colorViolet, b3_colorPink, b3_colorChocolate, b3_colorGoldenRod, b3_colorCoral, b3_colorRosyBrown,
|
||||
b3_colorAqua, b3_colorPeru, b3_colorLime, b3_colorGold, b3_colorPlum, b3_colorSnow,
|
||||
b3_colorTeal, b3_colorKhaki, b3_colorSalmon, b3_colorPeachPuff, b3_colorHoneyDew, b3_colorBlack,
|
||||
b3_colorRed, b3_colorOrange, b3_colorYellow, b3_colorGreen,
|
||||
b3_colorCyan, b3_colorBlue, b3_colorViolet, b3_colorPink,
|
||||
b3_colorChocolate, b3_colorGoldenRod, b3_colorCoral, b3_colorRosyBrown,
|
||||
b3_colorAqua, b3_colorPeru, b3_colorLime, b3_colorGold,
|
||||
b3_colorPlum, b3_colorSnow, b3_colorTeal, b3_colorKhaki,
|
||||
b3_colorSalmon, b3_colorPeachPuff, b3_colorHoneyDew, b3_colorBlack,
|
||||
};
|
||||
|
||||
int colorIndex = joint->colorIndex;
|
||||
|
|
@ -1752,13 +1910,14 @@ void b3DrawJoint( b3DebugDraw* draw, b3World* world, b3Joint* joint )
|
|||
}
|
||||
}
|
||||
|
||||
if ( draw->drawJointExtras )
|
||||
if ( draw->drawJointExtras && joint->type != b3_moverJoint && joint->type != b3_pogoJoint )
|
||||
{
|
||||
b3Vec3 force = b3GetJointConstraintForce( world, joint );
|
||||
b3Vec3 torque = b3GetJointConstraintTorque( world, joint );
|
||||
b3Pos p = b3LerpPosition( pA, pB, 0.5f );
|
||||
|
||||
draw->DrawSegmentFcn( p, b3OffsetPos( p, b3MulSV( 0.001f, force ) ), b3_colorAzure, draw->context );
|
||||
draw->DrawSegmentFcn( p, b3OffsetPos( p, b3MulSV( draw->forceScale, force ) ),
|
||||
b3_colorAzure, draw->context );
|
||||
|
||||
char buffer[64];
|
||||
snprintf( buffer, 64, "f = %g, t = %g", b3Length( force ), b3Length( torque ) );
|
||||
|
|
|
|||
47
src/joint.h
47
src/joint.h
|
|
@ -121,6 +121,39 @@ typedef struct b3MotorJoint
|
|||
b3Matrix3 angularMass;
|
||||
} b3MotorJoint;
|
||||
|
||||
typedef struct b3MoverJoint
|
||||
{
|
||||
b3Vec3 linearVelocity;
|
||||
float maxForce;
|
||||
|
||||
b3Vec3 axisX;
|
||||
b3Vec3 axisZ;
|
||||
b3Vec2 impulse;
|
||||
|
||||
int indexA;
|
||||
int indexB;
|
||||
float linearMass;
|
||||
} b3MoverJoint;
|
||||
|
||||
typedef struct b3PogoJoint
|
||||
{
|
||||
b3Vec3 normal;
|
||||
float restLength;
|
||||
float hertz;
|
||||
float dampingRatio;
|
||||
float maxTensionForce;
|
||||
float maxCompressionForce;
|
||||
|
||||
float impulse;
|
||||
int indexA;
|
||||
int indexB;
|
||||
b3Transform frameA;
|
||||
b3Transform frameB;
|
||||
b3Vec3 deltaCenter;
|
||||
float linearMass;
|
||||
float velocity;
|
||||
} b3PogoJoint;
|
||||
|
||||
typedef struct b3ParallelJoint
|
||||
{
|
||||
float hertz;
|
||||
|
|
@ -336,7 +369,9 @@ typedef struct b3JointSim
|
|||
{
|
||||
b3DistanceJoint distanceJoint;
|
||||
b3MotorJoint motorJoint;
|
||||
b3MoverJoint moverJoint;
|
||||
b3ParallelJoint parallelJoint;
|
||||
b3PogoJoint pogoJoint;
|
||||
b3RevoluteJoint revoluteJoint;
|
||||
b3SphericalJoint sphericalJoint;
|
||||
b3PrismaticJoint prismaticJoint;
|
||||
|
|
@ -365,6 +400,8 @@ void b3DrawJoint( b3DebugDraw* draw, b3World* world, b3Joint* joint );
|
|||
|
||||
b3Vec3 b3GetDistanceJointForce( b3World* world, b3JointSim* base );
|
||||
b3Vec3 b3GetMotorJointForce( b3World* world, b3JointSim* base );
|
||||
b3Vec3 b3GetMoverJointForce( b3World* world, b3JointSim* base );
|
||||
b3Vec3 b3GetPogoJointForce( b3World* world, b3JointSim* base );
|
||||
b3Vec3 b3GetPrismaticJointForce( b3World* world, b3JointSim* base );
|
||||
b3Vec3 b3GetRevoluteJointForce( b3World* world, b3JointSim* base );
|
||||
b3Vec3 b3GetSphericalJointForce( b3World* world, b3JointSim* base );
|
||||
|
|
@ -381,6 +418,8 @@ b3Vec3 b3GetWheelJointTorque( b3World* world, b3JointSim* base );
|
|||
|
||||
void b3PrepareDistanceJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3PrepareMotorJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3PrepareMoverJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3PreparePogoJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3PrepareParallelJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3PreparePrismaticJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3PrepareRevoluteJoint( b3JointSim* base, b3StepContext* context );
|
||||
|
|
@ -390,6 +429,8 @@ void b3PrepareWheelJoint( b3JointSim* base, b3StepContext* context );
|
|||
|
||||
void b3WarmStartDistanceJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3WarmStartMotorJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3WarmStartMoverJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3WarmStartPogoJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3WarmStartParallelJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3WarmStartPrismaticJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3WarmStartRevoluteJoint( b3JointSim* base, b3StepContext* context );
|
||||
|
|
@ -399,6 +440,8 @@ void b3WarmStartWheelJoint( b3JointSim* base, b3StepContext* context );
|
|||
|
||||
void b3SolveDistanceJoint( b3JointSim* base, b3StepContext* context, bool useBias );
|
||||
void b3SolveMotorJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3SolveMoverJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3SolvePogoJoint( b3JointSim* base, b3StepContext* context, bool useBias );
|
||||
void b3SolveParallelJoint( b3JointSim* base, b3StepContext* context );
|
||||
void b3SolvePrismaticJoint( b3JointSim* base, b3StepContext* context, bool useBias );
|
||||
void b3SolveRevoluteJoint( b3JointSim* base, b3StepContext* context, bool useBias );
|
||||
|
|
@ -407,6 +450,10 @@ void b3SolveWeldJoint( b3JointSim* base, b3StepContext* context, bool useBias );
|
|||
void b3SolveWheelJoint( b3JointSim* base, b3StepContext* context, bool useBias );
|
||||
|
||||
void b3DrawDistanceJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB );
|
||||
void b3DrawMoverJoint( b3DebugDraw* draw, b3World* world, b3JointSim* base, b3WorldTransform transformA,
|
||||
b3WorldTransform transformB );
|
||||
void b3DrawPogoJoint( b3DebugDraw* draw, b3World* world, b3JointSim* base, b3WorldTransform transformA,
|
||||
b3WorldTransform transformB, float scale );
|
||||
void b3DrawParallelJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
|
||||
void b3DrawPrismaticJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
|
||||
void b3DrawRevoluteJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
|
||||
|
|
|
|||
286
src/mover_joint.c
Normal file
286
src/mover_joint.c
Normal file
|
|
@ -0,0 +1,286 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "body.h"
|
||||
#include "core.h"
|
||||
#include "joint.h"
|
||||
#include "physics_world.h"
|
||||
#include "recording.h"
|
||||
#include "solver.h"
|
||||
#include "solver_set.h"
|
||||
|
||||
// needed for dll export
|
||||
#include "box3d/box3d.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void b3MoverJoint_SetLinearVelocity( b3JointId jointId, b3Vec3 velocity )
|
||||
{
|
||||
B3_ASSERT( b3IsValidVec3( velocity ) );
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, MoverJointSetLinearVelocity, jointId, velocity );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_moverJoint );
|
||||
joint->moverJoint.linearVelocity = velocity;
|
||||
}
|
||||
|
||||
b3Vec3 b3MoverJoint_GetLinearVelocity( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_moverJoint );
|
||||
return joint->moverJoint.linearVelocity;
|
||||
}
|
||||
|
||||
void b3MoverJoint_SetMaxVelocityForce( b3JointId jointId, float maxForce )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( maxForce ) && maxForce >= 0.0f );
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, MoverJointSetMaxVelocityForce, jointId, maxForce );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_moverJoint );
|
||||
joint->moverJoint.maxForce = maxForce;
|
||||
}
|
||||
|
||||
float b3MoverJoint_GetMaxVelocityForce( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_moverJoint );
|
||||
return joint->moverJoint.maxForce;
|
||||
}
|
||||
|
||||
b3Vec3 b3GetMoverJointForce( b3World* world, b3JointSim* base )
|
||||
{
|
||||
b3MoverJoint* joint = &base->moverJoint;
|
||||
b3Vec3 P = b3Blend2( joint->impulse.x, joint->axisX, joint->impulse.y, joint->axisZ );
|
||||
return b3MulSV( world->inv_h, P );
|
||||
}
|
||||
|
||||
void b3PrepareMoverJoint( b3JointSim* base, b3StepContext* context )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_moverJoint );
|
||||
|
||||
int idA = base->bodyIdA;
|
||||
int idB = base->bodyIdB;
|
||||
|
||||
b3World* world = context->world;
|
||||
|
||||
b3Body* bodyA = b3Array_Get( world->bodies, idA );
|
||||
b3Body* bodyB = b3Array_Get( world->bodies, idB );
|
||||
|
||||
B3_ASSERT( bodyA->setIndex == b3_awakeSet || bodyB->setIndex == b3_awakeSet );
|
||||
|
||||
b3SolverSet* setA = b3Array_Get( world->solverSets, bodyA->setIndex );
|
||||
b3SolverSet* setB = b3Array_Get( world->solverSets, bodyB->setIndex );
|
||||
|
||||
int localIndexA = bodyA->localIndex;
|
||||
int localIndexB = bodyB->localIndex;
|
||||
|
||||
b3BodySim* bodySimA = b3Array_Get( setA->bodySims, localIndexA );
|
||||
b3BodySim* bodySimB = b3Array_Get( setB->bodySims, localIndexB );
|
||||
|
||||
float mA = bodySimA->invMass;
|
||||
float mB = bodySimB->invMass;
|
||||
|
||||
base->invMassA = mA;
|
||||
base->invMassB = mB;
|
||||
base->invIA = b3Mat3_zero;
|
||||
base->invIB = b3Mat3_zero;
|
||||
|
||||
b3MoverJoint* joint = &base->moverJoint;
|
||||
joint->indexA = bodyA->setIndex == b3_awakeSet ? localIndexA : B3_NULL_INDEX;
|
||||
joint->indexB = bodyB->setIndex == b3_awakeSet ? localIndexB : B3_NULL_INDEX;
|
||||
|
||||
b3Quat quatB = b3MulQuat( bodySimB->transform.q, base->localFrameB.q );
|
||||
b3Matrix3 matrixB = b3MakeMatrixFromQuat( quatB );
|
||||
joint->axisX = matrixB.cx;
|
||||
joint->axisZ = matrixB.cz;
|
||||
|
||||
float k = mA + mB;
|
||||
joint->linearMass = k > 0.0f ? 1.0f / k : 0.0f;
|
||||
|
||||
if ( context->enableWarmStarting == false )
|
||||
{
|
||||
joint->impulse = (b3Vec2){ 0 };
|
||||
}
|
||||
}
|
||||
|
||||
void b3WarmStartMoverJoint( b3JointSim* base, b3StepContext* context )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_moverJoint );
|
||||
|
||||
float mA = base->invMassA;
|
||||
float mB = base->invMassB;
|
||||
|
||||
b3MoverJoint* joint = &base->moverJoint;
|
||||
|
||||
// dummy state for static bodies
|
||||
b3BodyState dummyState = b3_identityBodyState;
|
||||
|
||||
b3BodyState* stateA =
|
||||
joint->indexA == B3_NULL_INDEX ? &dummyState : context->states + joint->indexA;
|
||||
b3BodyState* stateB =
|
||||
joint->indexB == B3_NULL_INDEX ? &dummyState : context->states + joint->indexB;
|
||||
|
||||
b3Vec3 P = b3Blend2( joint->impulse.x, joint->axisX, joint->impulse.y, joint->axisZ );
|
||||
if ( stateA->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateA->linearVelocity = b3MulSub( stateA->linearVelocity, mA, P );
|
||||
}
|
||||
|
||||
if ( stateB->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateB->linearVelocity = b3MulAdd( stateB->linearVelocity, mB, P );
|
||||
}
|
||||
}
|
||||
|
||||
void b3SolveMoverJoint( b3JointSim* base, b3StepContext* context )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_moverJoint );
|
||||
|
||||
float mA = base->invMassA;
|
||||
float mB = base->invMassB;
|
||||
|
||||
// dummy state for static bodies
|
||||
b3BodyState dummyState = b3_identityBodyState;
|
||||
|
||||
b3MoverJoint* joint = &base->moverJoint;
|
||||
b3BodyState* stateA =
|
||||
joint->indexA == B3_NULL_INDEX ? &dummyState : context->states + joint->indexA;
|
||||
b3BodyState* stateB =
|
||||
joint->indexB == B3_NULL_INDEX ? &dummyState : context->states + joint->indexB;
|
||||
|
||||
b3Vec3 vA = stateA->linearVelocity;
|
||||
b3Vec3 vB = stateB->linearVelocity;
|
||||
|
||||
float maxForce = joint->maxForce;
|
||||
b3Vec3 vr = b3Sub( b3Sub( vB, vA ), joint->linearVelocity );
|
||||
b3Vec2 cdot = { b3Dot( vr, joint->axisX ), b3Dot( vr, joint->axisZ ) };
|
||||
b3Vec2 deltaImpulse = b3MulSV2( -joint->linearMass, cdot );
|
||||
b3Vec2 newImpulse = {
|
||||
joint->impulse.x + deltaImpulse.x,
|
||||
joint->impulse.y + deltaImpulse.y,
|
||||
};
|
||||
|
||||
float maxImpulse = context->h * maxForce;
|
||||
|
||||
float lengthSquared = b3Dot2( newImpulse, newImpulse );
|
||||
if ( lengthSquared > maxImpulse * maxImpulse )
|
||||
{
|
||||
float scale = maxImpulse / sqrtf( lengthSquared );
|
||||
newImpulse.x *= scale;
|
||||
newImpulse.y *= scale;
|
||||
}
|
||||
|
||||
deltaImpulse = b3Sub2( newImpulse, joint->impulse );
|
||||
joint->impulse = newImpulse;
|
||||
|
||||
b3Vec3 P = b3Blend2( deltaImpulse.x, joint->axisX, deltaImpulse.y, joint->axisZ );
|
||||
vA = b3MulSub( vA, mA, P );
|
||||
vB = b3MulAdd( vB, mB, P );
|
||||
|
||||
if ( stateA->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateA->linearVelocity = vA;
|
||||
}
|
||||
|
||||
if ( stateB->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateB->linearVelocity = vB;
|
||||
}
|
||||
}
|
||||
|
||||
static b3HexColor b3LerpColor( b3HexColor a, b3HexColor b, float t )
|
||||
{
|
||||
uint32_t result = 0;
|
||||
for ( int shift = 0; shift < 24; shift += 8 )
|
||||
{
|
||||
float ca = (float)( ( (uint32_t)a >> shift ) & 0xFF );
|
||||
float cb = (float)( ( (uint32_t)b >> shift ) & 0xFF );
|
||||
uint32_t c = (uint32_t)( b3LerpFloat( ca, cb, t ) + 0.5f );
|
||||
result |= c << shift;
|
||||
}
|
||||
return (b3HexColor)result;
|
||||
}
|
||||
|
||||
static void b3DrawArrow( b3DebugDraw* draw, b3Pos a, b3Pos b, b3Vec3 planeNormal, float size,
|
||||
b3HexColor color )
|
||||
{
|
||||
draw->DrawSegmentFcn( a, b, color, draw->context );
|
||||
|
||||
b3Vec3 shaft = b3SubPos( b, a );
|
||||
float shaftLen = b3Length( shaft );
|
||||
if ( shaftLen < 1e-6f )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3Vec3 dir = { shaft.x / shaftLen, shaft.y / shaftLen, shaft.z / shaftLen };
|
||||
float headLen = size;
|
||||
|
||||
b3Vec3 perp = b3Normalize( b3Cross( planeNormal, dir ) );
|
||||
b3Vec3 backFromTip = b3MulSV( -headLen, dir );
|
||||
b3Vec3 sideStep = b3MulSV( headLen * 0.5f, perp );
|
||||
b3Pos tip1 = b3OffsetPos( b, b3Add( backFromTip, sideStep ) );
|
||||
b3Pos tip2 = b3OffsetPos( b, b3Sub( backFromTip, sideStep ) );
|
||||
draw->DrawSegmentFcn( b, tip1, color, draw->context );
|
||||
draw->DrawSegmentFcn( b, tip2, color, draw->context );
|
||||
}
|
||||
|
||||
void b3DrawMoverJoint( b3DebugDraw* draw, b3World* world, b3JointSim* base,
|
||||
b3WorldTransform transformA, b3WorldTransform transformB )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_moverJoint );
|
||||
B3_UNUSED( transformA );
|
||||
|
||||
b3MoverJoint* joint = &base->moverJoint;
|
||||
|
||||
b3WorldTransform frameB = b3MulWorldTransforms( transformB, base->localFrameB );
|
||||
b3Matrix3 matrixB = b3MakeMatrixFromQuat( frameB.q );
|
||||
b3Vec3 axisX = matrixB.cx;
|
||||
b3Vec3 axisZ = matrixB.cz;
|
||||
|
||||
b3Body* bodyA = b3Array_Get( world->bodies, base->bodyIdA );
|
||||
b3Body* bodyB = b3Array_Get( world->bodies, base->bodyIdB );
|
||||
b3BodyState* stateA = b3GetBodyState( world, bodyA );
|
||||
b3BodyState* stateB = b3GetBodyState( world, bodyB );
|
||||
b3Vec3 vA = stateA != NULL ? stateA->linearVelocity : b3Vec3_zero;
|
||||
b3Vec3 vB = stateB != NULL ? stateB->linearVelocity : b3Vec3_zero;
|
||||
|
||||
b3Vec3 velocityError = b3Sub( b3Sub( vB, vA ), joint->linearVelocity );
|
||||
b3Vec2 planarVelocityError = { b3Dot( velocityError, axisX ), b3Dot( velocityError, axisZ ) };
|
||||
b3Vec2 targetVelocity = { b3Dot( joint->linearVelocity, axisX ),
|
||||
b3Dot( joint->linearVelocity, axisZ ) };
|
||||
|
||||
// Colorize ring by ratio of velocity error magnitude to target speed.
|
||||
b3HexColor color = b3_colorWhite;
|
||||
float targetSpeed = b3Length2( targetVelocity );
|
||||
if ( targetSpeed > 0.0f )
|
||||
{
|
||||
float t = b3ClampFloat( b3Length2( planarVelocityError ) / targetSpeed, 0.0f, 1.0f );
|
||||
color = b3LerpColor( b3_colorWhite, b3_colorBlue, t );
|
||||
}
|
||||
|
||||
float radius = draw->jointScale;
|
||||
b3Vec3 rx = b3MulSV( radius, axisX );
|
||||
b3Vec3 rz = b3MulSV( radius, axisZ );
|
||||
const int segmentCount = 32;
|
||||
b3CosSin cs = b3ComputeCosSin( 2.0f * B3_PI / segmentCount );
|
||||
float x1 = 1.0f, y1 = 0.0f;
|
||||
b3Pos p1 = b3OffsetPos( frameB.p, rx );
|
||||
for ( int i = 0; i < segmentCount; ++i )
|
||||
{
|
||||
float x2 = cs.cosine * x1 - cs.sine * y1;
|
||||
float y2 = cs.sine * x1 + cs.cosine * y1;
|
||||
b3Pos p2 = b3OffsetPos( frameB.p, b3Blend2( x2, rx, y2, rz ) );
|
||||
draw->DrawSegmentFcn( p1, p2, color, draw->context );
|
||||
x1 = x2;
|
||||
y1 = y2;
|
||||
p1 = p2;
|
||||
}
|
||||
|
||||
if ( joint->maxForce > 0.0f )
|
||||
{
|
||||
b3Vec3 force = b3Blend2( joint->impulse.x, rx, joint->impulse.y, rz );
|
||||
b3Vec3 dir = b3Normalize( force );
|
||||
b3Pos origin = b3OffsetPos( frameB.p, b3MulSV( -radius, dir ) );
|
||||
float scale = world->inv_h / joint->maxForce;
|
||||
b3Pos tip = b3OffsetPos( origin, b3MulSV( scale, force ) );
|
||||
b3DrawArrow( draw, origin, tip, matrixB.cy, 0.1f * draw->jointScale, b3_colorWhite );
|
||||
}
|
||||
}
|
||||
|
|
@ -646,6 +646,7 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
b3WorldTransform transformA = bodySimA->transform;
|
||||
b3WorldTransform transformB = bodySimB->transform;
|
||||
|
||||
// This is really just needed for mesh collision to prevent tunneling.
|
||||
bool isFast = ( ( bodySimA->flags | bodySimB->flags ) & b3_isFast ) != 0;
|
||||
|
||||
float recycleTolerance = wasTouching ? recycleDistance : recycleDistanceNonTouching;
|
||||
|
|
@ -743,7 +744,8 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
contact->flags |= b3_relativeTransformValid;
|
||||
|
||||
// This updates solid contacts
|
||||
bool touching = b3UpdateContact( world, workerIndex, contact, shapeA, bodySimA->localCenter, transformA, shapeB,
|
||||
bool touching = b3UpdateContact( world, workerIndex, contact, shapeA, bodySimA->localCenter,
|
||||
transformA, shapeB,
|
||||
bodySimB->localCenter, transformB, isFast, taskContext->arena );
|
||||
|
||||
int bucketIndex = b3MinInt( contact->manifoldCount, B3_CONTACT_MANIFOLD_COUNT_BUCKETS - 1 );
|
||||
|
|
|
|||
449
src/pogo_joint.c
Normal file
449
src/pogo_joint.c
Normal file
|
|
@ -0,0 +1,449 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "body.h"
|
||||
#include "core.h"
|
||||
#include "joint.h"
|
||||
#include "math_internal.h"
|
||||
#include "physics_world.h"
|
||||
#include "recording.h"
|
||||
#include "solver.h"
|
||||
#include "solver_set.h"
|
||||
|
||||
// needed for dll export
|
||||
#include "box3d/box3d.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void b3PogoJoint_SetRestLength( b3JointId jointId, float length )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( length ) && length >= 0.0f );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
if ( joint->pogoJoint.restLength == length )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, PogoJointSetRestLength, jointId, length );
|
||||
joint->pogoJoint.restLength = length;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetRestLength( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.restLength;
|
||||
}
|
||||
|
||||
void b3PogoJoint_SetSpringHertz( b3JointId jointId, float hertz )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( hertz ) && hertz >= 0.0f );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
if ( joint->pogoJoint.hertz == hertz )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, PogoJointSetSpringHertz, jointId, hertz );
|
||||
joint->pogoJoint.hertz = hertz;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetSpringHertz( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.hertz;
|
||||
}
|
||||
|
||||
void b3PogoJoint_SetSpringDampingRatio( b3JointId jointId, float dampingRatio )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( dampingRatio ) && dampingRatio >= 0.0f );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
if ( joint->pogoJoint.dampingRatio == dampingRatio )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, PogoJointSetSpringDampingRatio, jointId, dampingRatio );
|
||||
joint->pogoJoint.dampingRatio = dampingRatio;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetSpringDampingRatio( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.dampingRatio;
|
||||
}
|
||||
|
||||
void b3PogoJoint_SetMaxTensionForce( b3JointId jointId, float force )
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( force ) && force >= 0.0f );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
if ( joint->pogoJoint.maxTensionForce == force )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, PogoJointSetMaxTensionForce, jointId, force );
|
||||
joint->pogoJoint.maxTensionForce = force;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetMaxTensionForce(b3JointId jointId)
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.maxTensionForce;
|
||||
}
|
||||
|
||||
void b3PogoJoint_SetMaxCompressionForce(b3JointId jointId, float force)
|
||||
{
|
||||
B3_ASSERT( b3IsValidFloat( force ) && force >= 0.0f );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
if ( joint->pogoJoint.maxCompressionForce == force )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, PogoJointSetMaxCompressionForce, jointId, force );
|
||||
joint->pogoJoint.maxCompressionForce = force;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetMaxCompressionForce(b3JointId jointId)
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.maxCompressionForce;
|
||||
}
|
||||
|
||||
void b3PogoJoint_SetNormal( b3JointId jointId, b3Vec3 normal )
|
||||
{
|
||||
B3_ASSERT( b3IsValidVec3( normal ) && b3IsNormalized( normal ) );
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
b3Vec3 current = joint->pogoJoint.normal;
|
||||
if ( current.x == normal.x && current.y == normal.y && current.z == normal.z )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
B3_REC( world, PogoJointSetNormal, jointId, normal );
|
||||
joint->pogoJoint.normal = normal;
|
||||
}
|
||||
|
||||
b3Vec3 b3PogoJoint_GetNormal( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.normal;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetCurrentLength( b3JointId jointId )
|
||||
{
|
||||
b3World* world = b3GetWorld( jointId.world0 );
|
||||
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
|
||||
b3WorldTransform worldTransformA = b3GetBodyTransform( world, base->bodyIdA );
|
||||
b3Transform transformA = b3ToRelativeTransform( worldTransformA, worldTransformA.p );
|
||||
b3Transform transformB =
|
||||
b3ToRelativeTransform( b3GetBodyTransform( world, base->bodyIdB ), worldTransformA.p );
|
||||
|
||||
b3Vec3 axis = b3RotateVector( b3MulQuat( transformB.q, base->localFrameB.q ), b3Vec3_axisY );
|
||||
b3Vec3 pA = b3TransformPoint( transformA, base->localFrameA.p );
|
||||
b3Vec3 pB = b3TransformPoint( transformB, base->localFrameB.p );
|
||||
return b3Dot( b3Sub( pB, pA ), axis );
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetImpulse( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.impulse;
|
||||
}
|
||||
|
||||
float b3PogoJoint_GetCurrentSpeed( b3JointId jointId )
|
||||
{
|
||||
b3JointSim* joint = b3GetJointSimCheckType( jointId, b3_pogoJoint );
|
||||
return joint->pogoJoint.velocity;
|
||||
}
|
||||
|
||||
b3Vec3 b3GetPogoJointForce( b3World* world, b3JointSim* base )
|
||||
{
|
||||
return b3MulSV( world->inv_h * base->pogoJoint.impulse, base->pogoJoint.normal );
|
||||
}
|
||||
|
||||
void b3PreparePogoJoint( b3JointSim* base, b3StepContext* context )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_pogoJoint );
|
||||
|
||||
b3World* world = context->world;
|
||||
|
||||
// chase body id to the solver set where the body lives
|
||||
int idA = base->bodyIdA;
|
||||
int idB = base->bodyIdB;
|
||||
|
||||
b3Body* bodyA = b3Array_Get( world->bodies, idA );
|
||||
b3Body* bodyB = b3Array_Get( world->bodies, idB );
|
||||
|
||||
B3_ASSERT( bodyA->setIndex == b3_awakeSet || bodyB->setIndex == b3_awakeSet );
|
||||
|
||||
b3SolverSet* setA = b3Array_Get( world->solverSets, bodyA->setIndex );
|
||||
b3SolverSet* setB = b3Array_Get( world->solverSets, bodyB->setIndex );
|
||||
|
||||
int localIndexA = bodyA->localIndex;
|
||||
int localIndexB = bodyB->localIndex;
|
||||
|
||||
b3BodySim* bodySimA = b3Array_Get( setA->bodySims, localIndexA );
|
||||
b3BodySim* bodySimB = b3Array_Get( setB->bodySims, localIndexB );
|
||||
|
||||
float mA = bodySimA->invMass;
|
||||
float mB = bodySimB->invMass;
|
||||
b3Matrix3 iA = bodySimA->invInertiaWorld;
|
||||
b3Matrix3 iB = bodySimB->invInertiaWorld;
|
||||
|
||||
base->invMassA = mA;
|
||||
base->invMassB = mB;
|
||||
base->invIA = iA;
|
||||
base->invIB = iB;
|
||||
|
||||
b3PogoJoint* joint = &base->pogoJoint;
|
||||
joint->indexA = bodyA->setIndex == b3_awakeSet ? localIndexA : B3_NULL_INDEX;
|
||||
joint->indexB = bodyB->setIndex == b3_awakeSet ? localIndexB : B3_NULL_INDEX;
|
||||
|
||||
// Compute joint anchor frames with world space rotation, relative to center of mass
|
||||
joint->frameA.q = b3MulQuat( bodySimA->transform.q, base->localFrameA.q );
|
||||
joint->frameA.p = b3RotateVector( bodySimA->transform.q,
|
||||
b3Sub( base->localFrameA.p, bodySimA->localCenter ) );
|
||||
joint->frameB.q = b3MulQuat( bodySimB->transform.q, base->localFrameB.q );
|
||||
joint->frameB.p = b3RotateVector( bodySimB->transform.q,
|
||||
b3Sub( base->localFrameB.p, bodySimB->localCenter ) );
|
||||
|
||||
// Compute the initial center delta. Incremental position updates are relative to this.
|
||||
joint->deltaCenter = b3SubPos( bodySimB->center, bodySimA->center );
|
||||
|
||||
b3Vec3 rnA = b3Cross( joint->frameA.p, joint->normal );
|
||||
b3Vec3 rnB = b3Cross( joint->frameB.p, joint->normal );
|
||||
float k = mA + mB + b3Dot( rnA, b3MulMV( iA, rnA ) ) + b3Dot( rnB, b3MulMV( iB, rnB ) );
|
||||
joint->linearMass = k > 0.0f ? 1.0f / k : 0.0f;
|
||||
|
||||
if ( context->enableWarmStarting == false )
|
||||
{
|
||||
joint->velocity = 0.0f;
|
||||
joint->impulse = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void b3WarmStartPogoJoint( b3JointSim* base, b3StepContext* context )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_pogoJoint );
|
||||
|
||||
float mA = base->invMassA;
|
||||
float mB = base->invMassB;
|
||||
b3Matrix3 iA = base->invIA;
|
||||
b3Matrix3 iB = base->invIB;
|
||||
|
||||
b3PogoJoint* joint = &base->pogoJoint;
|
||||
|
||||
if ( joint->hertz == 0.0f )
|
||||
{
|
||||
joint->impulse = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
// dummy state for static bodies
|
||||
b3BodyState dummyState = b3_identityBodyState;
|
||||
|
||||
b3BodyState* stateA =
|
||||
joint->indexA == B3_NULL_INDEX ? &dummyState : context->states + joint->indexA;
|
||||
b3BodyState* stateB =
|
||||
joint->indexB == B3_NULL_INDEX ? &dummyState : context->states + joint->indexB;
|
||||
|
||||
b3Vec3 rA = b3RotateVector( stateA->deltaRotation, joint->frameA.p );
|
||||
b3Vec3 rB = b3RotateVector( stateB->deltaRotation, joint->frameB.p );
|
||||
|
||||
b3Vec3 linearImpulse = b3MulSV( joint->impulse, joint->normal );
|
||||
|
||||
if ( stateA->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateA->linearVelocity = b3MulSub( stateA->linearVelocity, mA, linearImpulse );
|
||||
stateA->angularVelocity =
|
||||
b3Sub( stateA->angularVelocity, b3MulMV( iA, b3Cross( rA, linearImpulse ) ) );
|
||||
}
|
||||
|
||||
if ( stateB->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateB->linearVelocity = b3MulAdd( stateB->linearVelocity, mB, linearImpulse );
|
||||
stateB->angularVelocity =
|
||||
b3Add( stateB->angularVelocity, b3MulMV( iB, b3Cross( rB, linearImpulse ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
void b3SolvePogoJoint( b3JointSim* base, b3StepContext* context, bool useBias )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_pogoJoint );
|
||||
|
||||
float mA = base->invMassA;
|
||||
float mB = base->invMassB;
|
||||
b3Matrix3 iA = base->invIA;
|
||||
b3Matrix3 iB = base->invIB;
|
||||
|
||||
// dummy state for static bodies
|
||||
b3BodyState dummyState = b3_identityBodyState;
|
||||
|
||||
b3PogoJoint* joint = &base->pogoJoint;
|
||||
if ( joint->hertz == 0.0f )
|
||||
{
|
||||
joint->impulse = 0.0f;
|
||||
joint->velocity = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
b3BodyState* stateA =
|
||||
joint->indexA == B3_NULL_INDEX ? &dummyState : context->states + joint->indexA;
|
||||
b3BodyState* stateB =
|
||||
joint->indexB == B3_NULL_INDEX ? &dummyState : context->states + joint->indexB;
|
||||
|
||||
b3Vec3 vA = stateA->linearVelocity;
|
||||
b3Vec3 wA = stateA->angularVelocity;
|
||||
b3Vec3 vB = stateB->linearVelocity;
|
||||
b3Vec3 wB = stateB->angularVelocity;
|
||||
|
||||
b3Vec3 rA = b3RotateVector( stateA->deltaRotation, joint->frameA.p );
|
||||
b3Vec3 rB = b3RotateVector( stateB->deltaRotation, joint->frameB.p );
|
||||
|
||||
float bias = 0.0f;
|
||||
if ( useBias )
|
||||
{
|
||||
// This is a custom pogo position correction designed to avoid feeding velocity into
|
||||
// the mover body. Otherwise the mover can make huge jumps going up stairs. This still
|
||||
// works with continuous collision.
|
||||
|
||||
b3Vec3 dcA = stateA->deltaPosition;
|
||||
b3Vec3 dcB = stateB->deltaPosition;
|
||||
b3Vec3 d = b3Add( b3Add( b3Sub( dcB, dcA ), b3Sub( rB, rA ) ), joint->deltaCenter );
|
||||
|
||||
// The pogo axis is usually fixed as the world y-axis. Different than the ground normal.
|
||||
b3Vec3 pogoAxis = b3RotateVector( joint->frameB.q, b3Vec3_axisY );
|
||||
float c = b3Dot( pogoAxis, d ) - joint->restLength;
|
||||
|
||||
// This could be divided by dot(joint->normal, pogoAxis) to account for the constraint
|
||||
// direction, but I'd rather diminish the pogo recovery rate than risk making it huge.
|
||||
joint->velocity =
|
||||
b3SpringDamper( joint->hertz, joint->dampingRatio, c, joint->velocity, context->h );
|
||||
bias = -joint->velocity;
|
||||
}
|
||||
|
||||
b3Vec3 vr = b3Sub( b3Add( vB, b3Cross( wB, rB ) ), b3Add( vA, b3Cross( wA, rA ) ) );
|
||||
float cdot = b3Dot( joint->normal, vr );
|
||||
|
||||
float maxTensionImpulse = context->h * joint->maxTensionForce;
|
||||
float maxCompressionImpulse = context->h * joint->maxCompressionForce;
|
||||
float oldImpulse = joint->impulse;
|
||||
float impulse = -joint->linearMass * ( cdot + bias );
|
||||
joint->impulse =
|
||||
b3ClampFloat( joint->impulse + impulse, -maxTensionImpulse, maxCompressionImpulse );
|
||||
impulse = joint->impulse - oldImpulse;
|
||||
|
||||
b3Vec3 P = b3MulSV( impulse, joint->normal );
|
||||
vA = b3MulSub( vA, mA, P );
|
||||
wA = b3Sub( wA, b3MulMV( iA, b3Cross( rA, P ) ) );
|
||||
vB = b3MulAdd( vB, mB, P );
|
||||
wB = b3Add( wB, b3MulMV( iB, b3Cross( rB, P ) ) );
|
||||
|
||||
if ( stateA->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateA->linearVelocity = vA;
|
||||
stateA->angularVelocity = wA;
|
||||
}
|
||||
|
||||
if ( stateB->flags & b3_dynamicFlag )
|
||||
{
|
||||
stateB->linearVelocity = vB;
|
||||
stateB->angularVelocity = wB;
|
||||
}
|
||||
}
|
||||
|
||||
void b3DrawPogoJoint( b3DebugDraw* draw, b3World* world, b3JointSim* base,
|
||||
b3WorldTransform transformA, b3WorldTransform transformB, float scale )
|
||||
{
|
||||
B3_ASSERT( base->type == b3_pogoJoint );
|
||||
|
||||
b3PogoJoint* joint = &base->pogoJoint;
|
||||
|
||||
b3Pos pA = b3TransformWorldPoint( transformA, base->localFrameA.p );
|
||||
b3Pos pB = b3TransformWorldPoint( transformB, base->localFrameB.p );
|
||||
b3Vec3 axis = b3RotateVector( b3MulQuat( transformB.q, base->localFrameB.q ), b3Vec3_axisY );
|
||||
b3Vec3 perp1 = b3Perp( axis );
|
||||
b3Vec3 perp2 = b3Cross( axis, perp1 );
|
||||
|
||||
float length = b3Dot( b3SubPos( pB, pA ), axis );
|
||||
b3Pos foot = b3OffsetPos( pB, b3MulSV( -length, axis ) );
|
||||
|
||||
float force = world->inv_h * joint->impulse;
|
||||
|
||||
// Tolerance for reaching max force.
|
||||
float saturation = 0.99f;
|
||||
|
||||
b3HexColor color;
|
||||
if ( joint->hertz == 0.0f )
|
||||
{
|
||||
color = b3_colorGray;
|
||||
}
|
||||
else if ( force > 0.0f && force >= saturation * joint->maxCompressionForce )
|
||||
{
|
||||
color = b3_colorRed;
|
||||
}
|
||||
else if ( force < 0.0f && -force >= saturation * joint->maxTensionForce )
|
||||
{
|
||||
color = b3_colorRed;
|
||||
}
|
||||
else if ( force > 0.0f )
|
||||
{
|
||||
color = b3_colorLimeGreen;
|
||||
}
|
||||
else if ( force < 0.0f )
|
||||
{
|
||||
color = b3_colorDeepSkyBlue;
|
||||
}
|
||||
else
|
||||
{
|
||||
color = b3_colorLightGray;
|
||||
}
|
||||
|
||||
// Spring
|
||||
int segmentCount = 12;
|
||||
float width = 0.1f * scale;
|
||||
b3Pos p1 = pB;
|
||||
for ( int i = 1; i <= segmentCount; ++i )
|
||||
{
|
||||
float side = i == segmentCount ? 0.0f : ( i & 1 ) ? width : -width;
|
||||
float t = (float)i / (float)segmentCount;
|
||||
b3Pos p2 = b3OffsetPos( pB, b3MulAdd( b3MulSV( -t * length, axis ), side, perp2 ) );
|
||||
draw->DrawSegmentFcn( p1, p2, color, draw->context );
|
||||
p1 = p2;
|
||||
}
|
||||
|
||||
// Ground plane
|
||||
b3Pos rest = b3OffsetPos( pB, b3MulSV( -joint->restLength, axis ) );
|
||||
float tick = 1.5f * width;
|
||||
draw->DrawSegmentFcn( b3OffsetPos( rest, b3MulSV( -tick, perp1 ) ),
|
||||
b3OffsetPos( rest, b3MulSV( tick, perp1 ) ), b3_colorWhite,
|
||||
draw->context );
|
||||
draw->DrawSegmentFcn( b3OffsetPos( rest, b3MulSV( -tick, perp2 ) ),
|
||||
b3OffsetPos( rest, b3MulSV( tick, perp2 ) ), b3_colorWhite,
|
||||
draw->context );
|
||||
|
||||
if ( b3Length( b3SubPos( pA, foot ) ) > B3_LINEAR_SLOP )
|
||||
{
|
||||
draw->DrawSegmentFcn( foot, pA, b3_colorDimGray, draw->context );
|
||||
}
|
||||
|
||||
draw->DrawPointFcn( pA, 6.0f, color, draw->context );
|
||||
draw->DrawSegmentFcn( pA, b3OffsetPos( pA, b3MulSV( 0.5f * scale, joint->normal ) ),
|
||||
b3_colorOrange, draw->context );
|
||||
|
||||
if ( draw->drawJointExtras )
|
||||
{
|
||||
char buffer[96];
|
||||
snprintf( buffer, 96, "length = %.3f, rest = %.3f, f = %.1f, v = %.3f", length,
|
||||
joint->restLength, force, joint->velocity );
|
||||
draw->DrawStringFcn( pB, buffer, color, draw->context );
|
||||
}
|
||||
}
|
||||
|
|
@ -342,6 +342,10 @@ _Static_assert( sizeof( void* ) != 8 || sizeof( b3WeldJointDef ) == 128,
|
|||
"b3WeldJointDef changed: update b3RecW_WELDJOINTDEF and its reader together" );
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3WheelJointDef ) == 184,
|
||||
"b3WheelJointDef changed: update b3RecW_WHEELJOINTDEF and its reader together" );
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3MoverJointDef ) == 128,
|
||||
"b3MoverJointDef changed: update b3RecW_MOVERJOINTDEF and its reader together" );
|
||||
_Static_assert( sizeof( void* ) != 8 || sizeof( b3PogoJointDef ) == 152,
|
||||
"b3PogoJointDef changed: update b3RecW_POGOJOINTDEF and its reader together" );
|
||||
|
||||
void b3RecW_EXPLOSIONDEF( b3RecBuffer* buf, b3ExplosionDef v )
|
||||
{
|
||||
|
|
@ -548,6 +552,26 @@ void b3RecW_WHEELJOINTDEF( b3RecBuffer* buf, b3WheelJointDef v )
|
|||
b3RecW_F32( buf, v.upperSteeringLimit );
|
||||
}
|
||||
|
||||
void b3RecW_MOVERJOINTDEF( b3RecBuffer* buf, b3MoverJointDef v )
|
||||
{
|
||||
b3RecW_JointBase( buf, &v.base );
|
||||
b3RecW_VEC3( buf, v.linearVelocity );
|
||||
b3RecW_F32( buf, v.maxForce );
|
||||
}
|
||||
|
||||
void b3RecW_POGOJOINTDEF( b3RecBuffer* buf, b3PogoJointDef v )
|
||||
{
|
||||
b3RecW_JointBase( buf, &v.base );
|
||||
b3RecW_VEC3( buf, v.normal );
|
||||
b3RecW_F32( buf, v.hertz );
|
||||
b3RecW_F32( buf, v.dampingRatio );
|
||||
b3RecW_F32( buf, v.restLength );
|
||||
b3RecW_F32( buf, v.maxTensionForce );
|
||||
b3RecW_F32( buf, v.maxCompressionForce );
|
||||
b3RecW_F32( buf, v.impulse );
|
||||
b3RecW_F32( buf, v.velocity );
|
||||
}
|
||||
|
||||
// Query recording. A query collects a variable number of hits through a user callback, so the count
|
||||
// is not known until the callback stops firing. The record is built in a local buffer with a
|
||||
// reserved hit-count slot, then committed whole under the lock so concurrent query threads never
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ typedef struct b3World b3World;
|
|||
|
||||
// Minor tracks op-stream additions that keep the 48 byte header shape.
|
||||
// Minor version 7 made the contact body sim locators persistent
|
||||
#define B3_REC_VERSION_MINOR 9
|
||||
#define B3_REC_VERSION_MINOR 10
|
||||
|
||||
// File header, fixed 48 bytes. Contains the registry locator so the player
|
||||
// can load geometry before replaying any ops.
|
||||
|
|
@ -196,6 +196,8 @@ typedef b3RevoluteJointDef b3RecCType_REVOLUTEJOINTDEF;
|
|||
typedef b3SphericalJointDef b3RecCType_SPHERICALJOINTDEF;
|
||||
typedef b3WeldJointDef b3RecCType_WELDJOINTDEF;
|
||||
typedef b3WheelJointDef b3RecCType_WHEELJOINTDEF;
|
||||
typedef b3MoverJointDef b3RecCType_MOVERJOINTDEF;
|
||||
typedef b3PogoJointDef b3RecCType_POGOJOINTDEF;
|
||||
|
||||
// Codegen pass 1a: arg structs. Generated here so call sites (body.c, shape.c, etc.) can see them.
|
||||
#define ARG( TAG, field ) b3RecCType_##TAG field;
|
||||
|
|
@ -266,6 +268,8 @@ void b3RecW_REVOLUTEJOINTDEF( b3RecBuffer* buf, b3RevoluteJointDef v );
|
|||
void b3RecW_SPHERICALJOINTDEF( b3RecBuffer* buf, b3SphericalJointDef v );
|
||||
void b3RecW_WELDJOINTDEF( b3RecBuffer* buf, b3WeldJointDef v );
|
||||
void b3RecW_WHEELJOINTDEF( b3RecBuffer* buf, b3WheelJointDef v );
|
||||
void b3RecW_MOVERJOINTDEF( b3RecBuffer* buf, b3MoverJointDef v );
|
||||
void b3RecW_POGOJOINTDEF( b3RecBuffer* buf, b3PogoJointDef v );
|
||||
|
||||
// Record framing
|
||||
void b3RecBeginRecord( b3Recording* rec, uint8_t opcode );
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
// 0x40-0x4F shape create/destroy
|
||||
// 0x50-0x6F shape mutators
|
||||
// 0x80 step
|
||||
// 0x81-0x8F mover and pogo joints
|
||||
// 0x90-0xE7 joints (create, generic, per-type)
|
||||
// 0xE8-0xEF spatial queries
|
||||
// 0xF0-0xFF markers
|
||||
|
|
@ -211,6 +212,20 @@ B3_REC_OP( 0xE5, WheelJointEnableSteeringLimit, RET_NONE, ARG( JOINTID, joint )
|
|||
B3_REC_OP( 0xE6, WheelJointSetSteeringLimits, RET_NONE, ARG( JOINTID, joint ) ARG( F32, lower ) ARG( F32, upper ) )
|
||||
B3_REC_OP( 0xE7, WheelJointSetTargetSteeringAngle, RET_NONE, ARG( JOINTID, joint ) ARG( F32, radians ) )
|
||||
|
||||
// Mover joint
|
||||
B3_REC_OP( 0x81, CreateMoverJoint, RET_JOINTID, ARG( WORLDID, world ) ARG( MOVERJOINTDEF, def ) )
|
||||
B3_REC_OP( 0x82, MoverJointSetLinearVelocity, RET_NONE, ARG( JOINTID, joint ) ARG( VEC3, velocity ) )
|
||||
B3_REC_OP( 0x83, MoverJointSetMaxVelocityForce, RET_NONE, ARG( JOINTID, joint ) ARG( F32, maxForce ) )
|
||||
|
||||
// Pogo joint
|
||||
B3_REC_OP( 0x84, CreatePogoJoint, RET_JOINTID, ARG( WORLDID, world ) ARG( POGOJOINTDEF, def ) )
|
||||
B3_REC_OP( 0x85, PogoJointSetRestLength, RET_NONE, ARG( JOINTID, joint ) ARG( F32, length ) )
|
||||
B3_REC_OP( 0x86, PogoJointSetSpringHertz, RET_NONE, ARG( JOINTID, joint ) ARG( F32, hertz ) )
|
||||
B3_REC_OP( 0x87, PogoJointSetSpringDampingRatio, RET_NONE, ARG( JOINTID, joint ) ARG( F32, dampingRatio ) )
|
||||
B3_REC_OP( 0x88, PogoJointSetMaxTensionForce, RET_NONE, ARG( JOINTID, joint ) ARG( F32, force ) )
|
||||
B3_REC_OP( 0x89, PogoJointSetMaxCompressionForce, RET_NONE, ARG( JOINTID, joint ) ARG( F32, force ) )
|
||||
B3_REC_OP( 0x8A, PogoJointSetNormal, RET_NONE, ARG( JOINTID, joint ) ARG( VEC3, normal ) )
|
||||
|
||||
// Spatial queries. Inputs flow through the manifest (reader side). The hit tail and result are
|
||||
// hand-written in recording.c / recording_replay.c since they are variable length.
|
||||
B3_REC_OP( 0xE8, QueryOverlapAABB, RET_NONE, ARG( WORLDID, world ) ARG( AABB, aabb ) ARG( QUERYFILTER, filter ) )
|
||||
|
|
|
|||
89
src/replay.c
89
src/replay.c
|
|
@ -649,6 +649,30 @@ b3WheelJointDef b3RecR_WHEELJOINTDEF( b3RecReader* rdr )
|
|||
return def;
|
||||
}
|
||||
|
||||
b3MoverJointDef b3RecR_MOVERJOINTDEF( b3RecReader* rdr )
|
||||
{
|
||||
b3MoverJointDef def = b3DefaultMoverJointDef();
|
||||
b3RecR_JointBase( rdr, &def.base );
|
||||
def.linearVelocity = b3RecR_VEC3( rdr );
|
||||
def.maxForce = b3RecR_F32( rdr );
|
||||
return def;
|
||||
}
|
||||
|
||||
b3PogoJointDef b3RecR_POGOJOINTDEF( b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJointDef def = b3DefaultPogoJointDef();
|
||||
b3RecR_JointBase( rdr, &def.base );
|
||||
def.normal = b3RecR_VEC3( rdr );
|
||||
def.hertz = b3RecR_F32( rdr );
|
||||
def.dampingRatio = b3RecR_F32( rdr );
|
||||
def.restLength = b3RecR_F32( rdr );
|
||||
def.maxTensionForce = b3RecR_F32( rdr );
|
||||
def.maxCompressionForce = b3RecR_F32( rdr );
|
||||
def.impulse = b3RecR_F32( rdr );
|
||||
def.velocity = b3RecR_F32( rdr );
|
||||
return def;
|
||||
}
|
||||
|
||||
// Outliner body tracking. Defined after the player struct; forward declared here because the
|
||||
// create/destroy dispatch sits above that struct.
|
||||
static void b3RecTrackBodyCreate( b3RecPlayer* player, b3BodyId id );
|
||||
|
|
@ -1262,6 +1286,24 @@ static void b3RecDispatch_CreateWheelJoint( const b3RecArgs_CreateWheelJoint* a,
|
|||
b3RecCheckJointId( rdr, b3CreateWheelJoint( rdr->replayWorldId, &def ), recId );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_CreateMoverJoint( const b3RecArgs_CreateMoverJoint* a, b3RecReader* rdr )
|
||||
{
|
||||
b3JointId recId = b3RecR_JOINTID( rdr );
|
||||
b3MoverJointDef def = a->def;
|
||||
def.base.bodyIdA = b3RecMakeBodyId( rdr, def.base.bodyIdA );
|
||||
def.base.bodyIdB = b3RecMakeBodyId( rdr, def.base.bodyIdB );
|
||||
b3RecCheckJointId( rdr, b3CreateMoverJoint( rdr->replayWorldId, &def ), recId );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_CreatePogoJoint( const b3RecArgs_CreatePogoJoint* a, b3RecReader* rdr )
|
||||
{
|
||||
b3JointId recId = b3RecR_JOINTID( rdr );
|
||||
b3PogoJointDef def = a->def;
|
||||
def.base.bodyIdA = b3RecMakeBodyId( rdr, def.base.bodyIdA );
|
||||
def.base.bodyIdB = b3RecMakeBodyId( rdr, def.base.bodyIdB );
|
||||
b3RecCheckJointId( rdr, b3CreatePogoJoint( rdr->replayWorldId, &def ), recId );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_DestroyJoint( const b3RecArgs_DestroyJoint* a, b3RecReader* rdr )
|
||||
{
|
||||
b3DestroyJoint( b3RecMakeJointId( rdr, a->joint ), a->wakeAttached );
|
||||
|
|
@ -1668,6 +1710,53 @@ static void b3RecDispatch_WheelJointSetTargetSteeringAngle( const b3RecArgs_Whee
|
|||
b3WheelJoint_SetTargetSteeringAngle( b3RecMakeJointId( rdr, a->joint ), a->radians );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_MoverJointSetLinearVelocity(
|
||||
const b3RecArgs_MoverJointSetLinearVelocity* a, b3RecReader* rdr )
|
||||
{
|
||||
b3MoverJoint_SetLinearVelocity( b3RecMakeJointId( rdr, a->joint ), a->velocity );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_MoverJointSetMaxVelocityForce(
|
||||
const b3RecArgs_MoverJointSetMaxVelocityForce* a, b3RecReader* rdr )
|
||||
{
|
||||
b3MoverJoint_SetMaxVelocityForce( b3RecMakeJointId( rdr, a->joint ), a->maxForce );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_PogoJointSetRestLength( const b3RecArgs_PogoJointSetRestLength* a,
|
||||
b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJoint_SetRestLength( b3RecMakeJointId( rdr, a->joint ), a->length );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_PogoJointSetSpringHertz( const b3RecArgs_PogoJointSetSpringHertz* a,
|
||||
b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJoint_SetSpringHertz( b3RecMakeJointId( rdr, a->joint ), a->hertz );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_PogoJointSetSpringDampingRatio(
|
||||
const b3RecArgs_PogoJointSetSpringDampingRatio* a, b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJoint_SetSpringDampingRatio( b3RecMakeJointId( rdr, a->joint ), a->dampingRatio );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_PogoJointSetMaxTensionForce(
|
||||
const b3RecArgs_PogoJointSetMaxTensionForce* a, b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJoint_SetMaxTensionForce( b3RecMakeJointId( rdr, a->joint ), a->force );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_PogoJointSetMaxCompressionForce(
|
||||
const b3RecArgs_PogoJointSetMaxCompressionForce* a, b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJoint_SetMaxCompressionForce( b3RecMakeJointId( rdr, a->joint ), a->force );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_PogoJointSetNormal( const b3RecArgs_PogoJointSetNormal* a, b3RecReader* rdr )
|
||||
{
|
||||
b3PogoJoint_SetNormal( b3RecMakeJointId( rdr, a->joint ), a->normal );
|
||||
}
|
||||
|
||||
static void b3RecDispatch_StateHash( const b3RecArgs_StateHash* a, b3RecReader* rdr )
|
||||
{
|
||||
b3World* world = b3GetWorldFromId( rdr->replayWorldId );
|
||||
|
|
|
|||
|
|
@ -246,6 +246,8 @@ b3RevoluteJointDef b3RecR_REVOLUTEJOINTDEF( b3RecReader* rdr );
|
|||
b3SphericalJointDef b3RecR_SPHERICALJOINTDEF( b3RecReader* rdr );
|
||||
b3WeldJointDef b3RecR_WELDJOINTDEF( b3RecReader* rdr );
|
||||
b3WheelJointDef b3RecR_WHEELJOINTDEF( b3RecReader* rdr );
|
||||
b3MoverJointDef b3RecR_MOVERJOINTDEF( b3RecReader* rdr );
|
||||
b3PogoJointDef b3RecR_POGOJOINTDEF( b3RecReader* rdr );
|
||||
b3QueryFilter b3RecR_QUERYFILTER( b3RecReader* rdr );
|
||||
b3ShapeProxy b3RecR_SHAPEPROXY( b3RecReader* rdr );
|
||||
b3TreeStats b3RecR_TREESTATS( b3RecReader* rdr );
|
||||
|
|
|
|||
|
|
@ -92,14 +92,6 @@ static inline b3FloatW8 b3MaxW8( b3FloatW8 a, b3FloatW8 b )
|
|||
return _mm256_max_ps( a, b );
|
||||
}
|
||||
|
||||
// Clamp a to [-b, b]
|
||||
static inline b3FloatW8 b3SymClampW8( b3FloatW8 a, b3FloatW8 b )
|
||||
{
|
||||
b3FloatW8 nb = b3NegW8( b );
|
||||
b3FloatW8 c = b3MaxW8( nb, a );
|
||||
return b3MinW8( c, b );
|
||||
}
|
||||
|
||||
static inline b3FloatW8 b3AndW8( b3FloatW8 a, b3FloatW8 b )
|
||||
{
|
||||
return _mm256_and_ps( a, b );
|
||||
|
|
|
|||
|
|
@ -89,14 +89,6 @@ static inline b3FloatW4 b3MaxW4( b3FloatW4 a, b3FloatW4 b )
|
|||
return vmaxq_f32( a, b );
|
||||
}
|
||||
|
||||
// clamp a to [-b, b]
|
||||
static inline b3FloatW4 b3SymClampW4( b3FloatW4 a, b3FloatW4 b )
|
||||
{
|
||||
b3FloatW4 nb = b3NegW4( b );
|
||||
b3FloatW4 c = b3MaxW4( nb, a );
|
||||
return b3MinW4( c, b );
|
||||
}
|
||||
|
||||
static inline b3FloatW4 b3AndW4( b3FloatW4 a, b3FloatW4 b )
|
||||
{
|
||||
return vreinterpretq_f32_u32( vandq_u32( vreinterpretq_u32_f32( a ), vreinterpretq_u32_f32( b ) ) );
|
||||
|
|
|
|||
|
|
@ -104,21 +104,6 @@ static inline b3FloatW4 b3MaxW4( b3FloatW4 a, b3FloatW4 b )
|
|||
return r;
|
||||
}
|
||||
|
||||
// clamp a to [-b, b]
|
||||
static inline b3FloatW4 b3SymClampW4( b3FloatW4 a, b3FloatW4 b )
|
||||
{
|
||||
b3FloatW4 r;
|
||||
r.x = a.x <= b.x ? a.x : b.x;
|
||||
r.y = a.y <= b.y ? a.y : b.y;
|
||||
r.z = a.z <= b.z ? a.z : b.z;
|
||||
r.w = a.w <= b.w ? a.w : b.w;
|
||||
r.x = r.x <= -b.x ? -b.x : r.x;
|
||||
r.y = r.y <= -b.y ? -b.y : r.y;
|
||||
r.z = r.z <= -b.z ? -b.z : r.z;
|
||||
r.w = r.w <= -b.w ? -b.w : r.w;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Logical operations on the scalar path are 0/1 float values. Not bit-wise like SIMD.
|
||||
|
||||
static inline b3FloatW4 b3AndW4( b3FloatW4 a, b3FloatW4 b )
|
||||
|
|
|
|||
|
|
@ -92,14 +92,6 @@ static inline b3FloatW4 b3MaxW4( b3FloatW4 a, b3FloatW4 b )
|
|||
return _mm_max_ps( a, b );
|
||||
}
|
||||
|
||||
// clamp a to [-b, b]
|
||||
static inline b3FloatW4 b3SymClampW4( b3FloatW4 a, b3FloatW4 b )
|
||||
{
|
||||
b3FloatW4 nb = b3NegW4( b );
|
||||
b3FloatW4 c = b3MaxW4( nb, a );
|
||||
return b3MinW4( c, b );
|
||||
}
|
||||
|
||||
static inline b3FloatW4 b3AndW4( b3FloatW4 a, b3FloatW4 b )
|
||||
{
|
||||
return _mm_and_ps( a, b );
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ typedef B3_WIDE( b3Vec3 ) b3Vec3W;
|
|||
#define b3MulAddW B3_WIDE( b3MulAdd )
|
||||
#define b3MinW B3_WIDE( b3Min )
|
||||
#define b3MaxW B3_WIDE( b3Max )
|
||||
#define b3SymClampW B3_WIDE( b3SymClamp )
|
||||
#define b3AndW B3_WIDE( b3And )
|
||||
#define b3OrW B3_WIDE( b3Or )
|
||||
#define b3AndNotW B3_WIDE( b3AndNot )
|
||||
|
|
|
|||
|
|
@ -153,7 +153,7 @@ b3DebugDraw b3DefaultDebugDraw( void )
|
|||
};
|
||||
|
||||
draw.jointScale = 1.0f;
|
||||
draw.forceScale = 1.0f;
|
||||
draw.forceScale = 0.001f;
|
||||
|
||||
return draw;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1072,14 +1072,16 @@ void b3DrawWheelJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform tra
|
|||
float upper = joint->upperSteeringLimit;
|
||||
|
||||
b3CosSin cs = b3ComputeCosSin( lower );
|
||||
b3CosSin delta = b3ComputeCosSin( ( upper - lower ) / sliceCount );
|
||||
b3Pos vertex1 = b3TransformWorldPoint( frame, (b3Vec3){ 0.0f, -radius * cs.sine, radius * cs.cosine } );
|
||||
|
||||
for ( int index = 0; index < sliceCount; ++index )
|
||||
{
|
||||
float t2 = ( index + 1.0f ) / sliceCount;
|
||||
float phi = b3LerpFloat( lower, upper, t2 );
|
||||
float cosine = delta.cosine * cs.cosine - delta.sine * cs.sine;
|
||||
float sine = delta.sine * cs.cosine + delta.cosine * cs.sine;
|
||||
cs.cosine = cosine;
|
||||
cs.sine = sine;
|
||||
|
||||
cs = b3ComputeCosSin( phi );
|
||||
b3Pos vertex2 = b3TransformWorldPoint( frame, (b3Vec3){ 0.0f, -radius * cs.sine, radius * cs.cosine } );
|
||||
|
||||
if ( index == 0 )
|
||||
|
|
|
|||
Loading…
Reference in a new issue