Solver stuff (#189)

This commit is contained in:
Erin Catto 2026-10-14 22:20:52 -07:00 • committed by GitHub
commit 0cfc475b88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 316 additions and 297 deletions

View file

@ -1123,6 +1123,7 @@ B3_FORCE_INLINE void b3PushContactsW( b3SolverBlock block, b3StepContext* contex
b3FloatW impulseScale = b3BlendW( dynamicImpulseScale, staticImpulseScale, softMask );
b3Vec3W dp = b3SubVW( bB.dp, bA.dp );
b3FloatW invMass = b3AddW( c->invMassA, c->invMassB );
b3ContactManifoldWide* manifolds = b3GetManifoldsW( c );
for ( int manifoldIndex = 0; manifoldIndex < manifoldCount; ++manifoldIndex )
@ -1135,6 +1136,9 @@ B3_FORCE_INLINE void b3PushContactsW( b3SolverBlock block, b3StepContext* contex
b3Vec3W normalA = b3InvRotateVectorW( bA.dq, cm->normal );
b3Vec3W normalB = b3InvRotateVectorW( bB.dq, cm->normal );
b3FloatW linearNormalVelocity = b3DotW( cm->normal, b3SubVW( bB.v, bA.v ) );
b3FloatW manifoldImpulse = zeroW;
for ( int pointIndex = 0; pointIndex < cm->pointCount; ++pointIndex )
{
b3ContactConstraintPointWide* cp = cm->points + pointIndex;
@ -1159,27 +1163,27 @@ B3_FORCE_INLINE void b3PushContactsW( b3SolverBlock block, b3StepContext* contex
b3FloatW pointMassScale = b3BlendW( massScale, oneW, separated );
b3FloatW pointImpulseScale = b3BlendW( impulseScale, zeroW, separated );
// Relative velocity at contact
b3Vec3W vrA = b3AddVW( bA.v, b3CrossW( bA.w, rA ) );
b3Vec3W vrB = b3AddVW( bB.v, b3CrossW( bB.w, rB ) );
b3FloatW vn = b3DotW( b3SubVW( vrB, vrA ), cm->normal );
b3Vec3W uA = b3CrossW( rA, cm->normal );
b3Vec3W uB = b3CrossW( rB, cm->normal );
b3Vec3W gA = b3MulMVW( c->invIA, uA );
b3Vec3W gB = b3MulMVW( c->invIB, uB );
b3FloatW vn = b3AddW( linearNormalVelocity, b3SubW( b3DotW( uB, bB.w ), b3DotW( uA, bA.w ) ) );
// Compute normal impulse
b3FloatW negImpulse = b3AddW( b3MulW( cp->normalMasses, b3AddW( b3MulW( pointMassScale, vn ), velocityBias ) ),
b3MulW( pointImpulseScale, cp->normalImpulses ) );
b3FloatW impulse = b3MaxW( b3NegW( negImpulse ), b3NegW( cp->normalImpulses ) );
cp->normalImpulses = b3AddW( cp->normalImpulses, impulse );
// Clamp the accumulated impulse
b3FloatW newImpulse = b3MaxW( b3SubW( cp->normalImpulses, negImpulse ), zeroW );
b3FloatW deltaImpulse = b3SubW( newImpulse, cp->normalImpulses );
cp->normalImpulses = newImpulse;
// Apply contact impulse
b3Vec3W P = b3MulSVW( deltaImpulse, cm->normal );
bA.w = b3MulSubMVW( bA.w, c->invIA, b3CrossW( rA, P ) );
bA.v = b3MulSubSVW( bA.v, c->invMassA, P );
bB.w = b3MulAddMVW( bB.w, c->invIB, b3CrossW( rB, P ) );
bB.v = b3MulAddSVW( bB.v, c->invMassB, P );
linearNormalVelocity = b3MulAddW( linearNormalVelocity, invMass, impulse );
manifoldImpulse = b3AddW( manifoldImpulse, impulse );
bA.w = b3MulSubSVW( bA.w, impulse, gA );
bB.w = b3MulAddSVW( bB.w, impulse, gB );
}
b3Vec3W P = b3MulSVW( manifoldImpulse, cm->normal );
bA.v = b3MulSubSVW( bA.v, c->invMassA, P );
bB.v = b3MulAddSVW( bB.v, c->invMassB, P );
}
b3ScatterBodies( states, c->indexA, &bA );
@ -1208,6 +1212,7 @@ B3_FORCE_INLINE void b3SolveContactsW( b3SolverBlock block, b3StepContext* conte
bool anyRolling = b3AllZeroW( c->rollingResistance ) == false;
b3Vec3W dp = b3SubVW( bB.dp, bA.dp );
b3FloatW invMass = b3AddW( c->invMassA, c->invMassB );
b3ContactManifoldWide* manifolds = b3GetManifoldsW( c );
for ( int manifoldIndex = 0; manifoldIndex < manifoldCount; ++manifoldIndex )
@ -1219,6 +1224,12 @@ B3_FORCE_INLINE void b3SolveContactsW( b3SolverBlock block, b3StepContext* conte
b3Vec3W normalA = b3InvRotateVectorW( bA.dq, cm->normal );
b3Vec3W normalB = b3InvRotateVectorW( bB.dq, cm->normal );
b3Vec3W dv = b3SubVW( bB.v, bA.v );
b3FloatW linearNormalVelocity = b3DotW( cm->normal, dv );
b3FloatW linearTangentVelocity1 = b3SubW( b3DotW( cm->tangent1, dv ), cm->tangentVelocity1 );
b3FloatW linearTangentVelocity2 = b3SubW( b3DotW( cm->tangent2, dv ), cm->tangentVelocity2 );
b3FloatW manifoldImpulse = zeroW;
b3FloatW totalNormalImpulse = zeroW;
b3FloatW totalTwistLimit = zeroW;
@ -1237,28 +1248,25 @@ B3_FORCE_INLINE void b3SolveContactsW( b3SolverBlock block, b3StepContext* conte
// Speculative bias, positive if separated and zero if overlapped
b3FloatW velocityBias = b3MaxW( zeroW, b3MulW( s, inv_h ) );
// Relative velocity at contact
b3Vec3W vrA = b3AddVW( bA.v, b3CrossW( bA.w, rA ) );
b3Vec3W vrB = b3AddVW( bB.v, b3CrossW( bB.w, rB ) );
b3FloatW vn = b3DotW( b3SubVW( vrB, vrA ), cm->normal );
b3Vec3W uA = b3CrossW( rA, cm->normal );
b3Vec3W uB = b3CrossW( rB, cm->normal );
b3Vec3W gA = b3MulMVW( c->invIA, uA );
b3Vec3W gB = b3MulMVW( c->invIB, uB );
b3FloatW vn = b3AddW( linearNormalVelocity, b3SubW( b3DotW( uB, bB.w ), b3DotW( uA, bA.w ) ) );
// Compute normal impulse
b3FloatW negImpulse = b3MulW( cp->normalMasses, b3AddW( vn, velocityBias ) );
// Clamp the accumulated impulse
b3FloatW newImpulse = b3MaxW( b3SubW( cp->normalImpulses, negImpulse ), zeroW );
b3FloatW deltaImpulse = b3SubW( newImpulse, cp->normalImpulses );
b3FloatW impulse = b3MaxW( b3NegW( negImpulse ), b3NegW( cp->normalImpulses ) );
b3FloatW newImpulse = b3AddW( cp->normalImpulses, impulse );
cp->normalImpulses = newImpulse;
cp->totalNormalImpulses = b3AddW( cp->totalNormalImpulses, newImpulse );
totalNormalImpulse = b3AddW( totalNormalImpulse, newImpulse );
totalTwistLimit = b3AddW( totalTwistLimit, b3MulW( cp->leverArms, newImpulse ) );
// Apply contact impulse
b3Vec3W P = b3MulSVW( deltaImpulse, cm->normal );
bA.w = b3MulSubMVW( bA.w, c->invIA, b3CrossW( rA, P ) );
bA.v = b3MulSubSVW( bA.v, c->invMassA, P );
bB.w = b3MulAddMVW( bB.w, c->invIB, b3CrossW( rB, P ) );
bB.v = b3MulAddSVW( bB.v, c->invMassB, P );
linearNormalVelocity = b3MulAddW( linearNormalVelocity, invMass, impulse );
manifoldImpulse = b3AddW( manifoldImpulse, impulse );
bA.w = b3MulSubSVW( bA.w, impulse, gA );
bB.w = b3MulAddSVW( bB.w, impulse, gB );
}
// Rolling resistance. After normal constraints because it uses the normal impulse.
@ -1299,14 +1307,13 @@ B3_FORCE_INLINE void b3SolveContactsW( b3SolverBlock block, b3StepContext* conte
{
b3FloatW twistSpeed = b3DotW( cm->normal, b3SubVW( bB.w, bA.w ) );
b3FloatW maxLambda = b3MulW( c->friction, totalTwistLimit );
b3FloatW deltaImpulse = b3NegW( b3MulW( cm->twistMass, twistSpeed ) );
b3FloatW oldImpulse = cm->twistImpulse;
cm->twistImpulse = b3SymClampW( b3AddW( oldImpulse, deltaImpulse ), maxLambda );
deltaImpulse = b3SubW( cm->twistImpulse, oldImpulse );
b3FloatW impulse = b3NegW( b3MulW( cm->twistMass, twistSpeed ) );
impulse = b3MaxW( b3SubW( b3NegW( maxLambda ), oldImpulse ), b3MinW( impulse, b3SubW( maxLambda, oldImpulse ) ) );
cm->twistImpulse = b3AddW( oldImpulse, impulse );
b3Vec3W L = b3MulSVW( deltaImpulse, cm->normal );
bA.w = b3MulSubMVW( bA.w, c->invIA, L );
bB.w = b3MulAddMVW( bB.w, c->invIB, L );
bA.w = b3MulSubSVW( bA.w, impulse, b3MulMVW( c->invIA, cm->normal ) );
bB.w = b3MulAddSVW( bB.w, impulse, b3MulMVW( c->invIB, cm->normal ) );
}
// Central friction. Friction goes last to improve stacking stability.
@ -1318,13 +1325,12 @@ B3_FORCE_INLINE void b3SolveContactsW( b3SolverBlock block, b3StepContext* conte
b3Vec3W rA = cm->centerA;
b3Vec3W rB = cm->centerB;
b3Vec3W angularRelativeVelocity = b3SubVW( b3CrossW( bB.w, rB ), b3CrossW( bA.w, rA ) );
// Relative tangent velocity at contact
b3Vec3W vrA = b3AddVW( bA.v, b3CrossW( bA.w, rA ) );
b3Vec3W vrB = b3AddVW( bB.v, b3CrossW( bB.w, rB ) );
b3Vec3W vr = b3SubVW( vrB, vrA );
b3Vec2W vt = {
b3SubW( b3DotW( vr, tangent1 ), cm->tangentVelocity1 ),
b3SubW( b3DotW( vr, tangent2 ), cm->tangentVelocity2 ),
b3AddW( linearTangentVelocity1, b3DotW( angularRelativeVelocity, tangent1 ) ),
b3AddW( linearTangentVelocity2, b3DotW( angularRelativeVelocity, tangent2 ) ),
};
// Incremental tangent impulse
@ -1359,8 +1365,10 @@ B3_FORCE_INLINE void b3SolveContactsW( b3SolverBlock block, b3StepContext* conte
// Apply delta impulse
b3Vec3W P = b3AddVW( b3MulSVW( deltaImpulse.x, tangent1 ), b3MulSVW( deltaImpulse.y, tangent2 ) );
bA.w = b3MulSubMVW( bA.w, c->invIA, b3CrossW( rA, P ) );
bA.v = b3MulSubSVW( bA.v, c->invMassA, P );
bB.w = b3MulAddMVW( bB.w, c->invIB, b3CrossW( rB, P ) );
P = b3MulAddSVW( P, manifoldImpulse, cm->normal );
bA.v = b3MulSubSVW( bA.v, c->invMassA, P );
bB.v = b3MulAddSVW( bB.v, c->invMassB, P );
}
}

View file

@ -413,3 +413,46 @@ void b3DrawRevoluteJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform
void b3DrawSphericalJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawWeldJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawWheelJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
static inline b3Vec3 b3SolvePointConstraint( float mA, float mB, b3Matrix3 iA, b3Matrix3 iB, b3Vec3 rA, b3Vec3 rB, b3Vec3 rhs )
{
float axy = rA.z * iA.cy.y - rA.y * iA.cz.y;
float axz = rA.z * iA.cy.z - rA.y * iA.cz.z;
b3Vec3 ay = b3Sub( b3MulSV( rA.x, iA.cz ), b3MulSV( rA.z, iA.cx ) );
b3Vec3 az = b3Sub( b3MulSV( rA.y, iA.cx ), b3MulSV( rA.x, iA.cy ) );
float bxy = rB.z * iB.cy.y - rB.y * iB.cz.y;
float bxz = rB.z * iB.cy.z - rB.y * iB.cz.z;
b3Vec3 by = b3Sub( b3MulSV( rB.x, iB.cz ), b3MulSV( rB.z, iB.cx ) );
b3Vec3 bz = b3Sub( b3MulSV( rB.y, iB.cx ), b3MulSV( rB.x, iB.cy ) );
float m = mA + mB;
float kxx = ( axy * rA.z - axz * rA.y ) + ( bxy * rB.z - bxz * rB.y ) + m;
float kyy = ( ay.z * rA.x - ay.x * rA.z ) + ( by.z * rB.x - by.x * rB.z ) + m;
float kzz = ( az.x * rA.y - az.y * rA.x ) + ( bz.x * rB.y - bz.y * rB.x ) + m;
float kxy = ( ay.y * rA.z - ay.z * rA.y ) + ( by.y * rB.z - by.z * rB.y );
float kxz = ( az.y * rA.z - az.z * rA.y ) + ( bz.y * rB.z - bz.z * rB.y );
float kyz = ( az.z * rA.x - az.x * rA.z ) + ( bz.z * rB.x - bz.x * rB.z );
float cxx = kyy * kzz - kyz * kyz;
float cyy = kxx * kzz - kxz * kxz;
float czz = kxx * kyy - kxy * kxy;
float cxy = kxz * kyz - kxy * kzz;
float cxz = kxy * kyz - kyy * kxz;
float cyz = kxy * kxz - kxx * kyz;
float det = kxx * cxx + kxy * cxy + kxz * cxz;
if ( b3AbsFloat( det ) > 1000.0f * FLT_MIN )
{
float invDet = 1.0f / det;
b3Vec3 b = {
invDet * ( cxx * rhs.x + cxy * rhs.y + cxz * rhs.z ),
invDet * ( cxy * rhs.x + cyy * rhs.y + cyz * rhs.z ),
invDet * ( cxz * rhs.x + cyz * rhs.y + czz * rhs.z ),
};
return b;
}
return b3Vec3_zero;
}

View file

@ -361,16 +361,7 @@ void b3SolveMotorJoint( b3JointSim* base, b3StepContext* context )
b3Vec3 cdot = b3Sub( b3Add( vB, b3Cross( wB, rB ) ), b3Add( vA, b3Cross( wA, rA ) ) );
//// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
b3Matrix3 sA = b3Skew( rA );
b3Matrix3 sB = b3Skew( rB );
b3Matrix3 kA = b3MulMM( sA, b3MulMM( base->invIA, sA ) );
b3Matrix3 kB = b3MulMM( sB, b3MulMM( base->invIB, sB ) );
b3Matrix3 k = b3NegateMat3( b3AddMM( kA, kB ) );
k.cx.x += mA + mB;
k.cy.y += mA + mB;
k.cz.z += mA + mB;
b3Vec3 b = b3Solve3( k, b3Add( cdot, bias ) );
b3Vec3 b = b3SolvePointConstraint( mA, mB, base->invIA, base->invIB, rA, rB, b3Add( cdot, bias ) );
b3Vec3 oldImpulse = joint->linearSpringImpulse;
b3Vec3 impulse = b3MulSub( b3MulSV( -massScale, b ), impulseScale, oldImpulse );
@ -396,16 +387,7 @@ void b3SolveMotorJoint( b3JointSim* base, b3StepContext* context )
b3Vec3 cdot = b3Sub( b3Add( vB, b3Cross( wB, rB ) ), b3Add( vA, b3Cross( wA, rA ) ) );
cdot = b3Sub( cdot, joint->linearVelocity );
//// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
b3Matrix3 sA = b3Skew( rA );
b3Matrix3 sB = b3Skew( rB );
b3Matrix3 kA = b3MulMM( sA, b3MulMM( base->invIA, sA ) );
b3Matrix3 kB = b3MulMM( sB, b3MulMM( base->invIB, sB ) );
b3Matrix3 k = b3NegateMat3( b3AddMM( kA, kB ) );
k.cx.x += mA + mB;
k.cy.y += mA + mB;
k.cz.z += mA + mB;
b3Vec3 b = b3Solve3( k, cdot );
b3Vec3 b = b3SolvePointConstraint( mA, mB, base->invIA, base->invIB, rA, rB, cdot );
b3Vec3 impulse = b3Neg( b );
b3Vec3 oldImpulse = joint->linearVelocityImpulse;

View file

@ -411,106 +411,111 @@ void b3SolveRevoluteJoint( b3JointSim* base, b3StepContext* context, bool useBia
b3Quat relQ = b3InvMulQuat( quatA, quatB );
// Solve spring
if ( joint->enableSpring && fixedRotation == false )
if ( fixedRotation == false && ( joint->enableSpring || joint->enableMotor || joint->enableLimit ) )
{
// Get the substep relative rotation
float targetAngle = joint->targetAngle;
float angle = b3GetTwistAngle( relQ );
float c = angle - targetAngle;
float bias = joint->springSoftness.biasRate * c;
float massScale = joint->springSoftness.massScale;
float impulseScale = joint->springSoftness.impulseScale;
float cdot = b3Dot( b3Sub( wB, wA ), joint->rotationAxisZ );
float deltaImpulse = -massScale * joint->axialMass * ( cdot + bias ) - impulseScale * joint->springImpulse;
joint->springImpulse += deltaImpulse;
wA = b3MulSub( wA, deltaImpulse, b3MulMV( iA, joint->rotationAxisZ ) );
wB = b3MulAdd( wB, deltaImpulse, b3MulMV( iB, joint->rotationAxisZ ) );
}
if ( joint->enableMotor && fixedRotation == false )
{
float cdot = b3Dot( b3Sub( wB, wA ), joint->rotationAxisZ ) - joint->motorSpeed;
float deltaImpulse = -joint->axialMass * cdot;
float newImpulse = joint->motorImpulse + deltaImpulse;
float maxImpulse = joint->maxMotorTorque * context->h;
newImpulse = b3ClampFloat( newImpulse, -maxImpulse, maxImpulse );
deltaImpulse = newImpulse - joint->motorImpulse;
joint->motorImpulse = newImpulse;
wA = b3MulSub( wA, deltaImpulse, b3MulMV( iA, joint->rotationAxisZ ) );
wB = b3MulAdd( wB, deltaImpulse, b3MulMV( iB, joint->rotationAxisZ ) );
}
if ( joint->enableLimit && fixedRotation == false )
{
float angle = b3GetTwistAngle( relQ );
// todo does an updated twist axis help?
b3Vec3 axis = joint->rotationAxisZ;
b3Vec3 gA = b3MulMV( iA, axis );
b3Vec3 gB = b3MulMV( iB, axis );
float k = b3Dot( axis, b3Add( gA, gB ) );
float axialCdot = b3Dot( b3Sub( wB, wA ), axis );
float axialImpulse = 0.0f;
// Lower limit
// Solve spring
if ( joint->enableSpring )
{
float c = angle - joint->lowerAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
}
// Get the substep relative rotation
float targetAngle = joint->targetAngle;
float angle = b3GetTwistAngle( relQ );
float c = angle - targetAngle;
float cdot = b3Dot( b3Sub( wB, wA ), axis );
float oldImpulse = joint->lowerImpulse;
float deltaImpulse = -massScale * joint->axialMass * ( cdot + bias ) - impulseScale * oldImpulse;
joint->lowerImpulse = b3MaxFloat( oldImpulse + deltaImpulse, 0.0f );
deltaImpulse = joint->lowerImpulse - oldImpulse;
float bias = joint->springSoftness.biasRate * c;
float massScale = joint->springSoftness.massScale;
float impulseScale = joint->springSoftness.impulseScale;
wA = b3MulSub( wA, deltaImpulse, b3MulMV( iA, axis ) );
wB = b3MulAdd( wB, deltaImpulse, b3MulMV( iB, axis ) );
float deltaImpulse = -massScale * joint->axialMass * ( axialCdot + bias ) - impulseScale * joint->springImpulse;
joint->springImpulse += deltaImpulse;
axialCdot += k * deltaImpulse;
axialImpulse += deltaImpulse;
}
// Upper limit
if ( joint->enableMotor )
{
float c = joint->upperAngle - angle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
}
float cdot = axialCdot - joint->motorSpeed;
float oldImpulse = joint->motorImpulse;
float maxImpulse = joint->maxMotorTorque * context->h;
float deltaImpulse = b3ClampFloat( -joint->axialMass * cdot, -maxImpulse - oldImpulse, maxImpulse - oldImpulse );
joint->motorImpulse = oldImpulse + deltaImpulse;
// sign flipped on Cdot
float cdot = b3Dot( b3Sub( wA, wB ), axis );
float oldImpulse = joint->upperImpulse;
float deltaImpulse = -massScale * joint->axialMass * ( cdot + bias ) - impulseScale * oldImpulse;
joint->upperImpulse = b3MaxFloat( oldImpulse + deltaImpulse, 0.0f );
deltaImpulse = joint->upperImpulse - oldImpulse;
// sign flipped on applied impulse
wA = b3MulAdd( wA, deltaImpulse, b3MulMV( iA, axis ) );
wB = b3MulSub( wB, deltaImpulse, b3MulMV( iB, axis ) );
axialCdot += k * deltaImpulse;
axialImpulse += deltaImpulse;
}
if ( joint->enableLimit )
{
float angle = b3GetTwistAngle( relQ );
// todo does an updated twist axis help?
// Lower limit
{
float c = angle - joint->lowerAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
}
float oldImpulse = joint->lowerImpulse;
float deltaImpulse =
b3MaxFloat( -massScale * joint->axialMass * ( axialCdot + bias ) - impulseScale * oldImpulse, -oldImpulse );
joint->lowerImpulse = oldImpulse + deltaImpulse;
axialCdot += k * deltaImpulse;
axialImpulse += deltaImpulse;
}
// Upper limit
{
float c = joint->upperAngle - angle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
}
// sign flipped on Cdot
float cdot = -axialCdot;
float oldImpulse = joint->upperImpulse;
float deltaImpulse =
b3MaxFloat( -massScale * joint->axialMass * ( cdot + bias ) - impulseScale * oldImpulse, -oldImpulse );
joint->upperImpulse = oldImpulse + deltaImpulse;
// sign flipped on applied impulse
axialImpulse -= deltaImpulse;
}
}
wA = b3MulSub( wA, axialImpulse, gA );
wB = b3MulAdd( wB, axialImpulse, gB );
}
// Collinearity constraint
@ -578,16 +583,7 @@ void b3SolveRevoluteJoint( b3JointSim* base, b3StepContext* context, bool useBia
}
//// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
b3Matrix3 sA = b3Skew( rA );
b3Matrix3 sB = b3Skew( rB );
b3Matrix3 kA = b3MulMM( sA, b3MulMM( base->invIA, sA ) );
b3Matrix3 kB = b3MulMM( sB, b3MulMM( base->invIB, sB ) );
b3Matrix3 k = b3NegateMat3( b3AddMM( kA, kB ) );
k.cx.x += mA + mB;
k.cy.y += mA + mB;
k.cz.z += mA + mB;
b3Vec3 b = b3Solve3( k, b3Add( cdot, bias ) );
b3Vec3 b = b3SolvePointConstraint( mA, mB, base->invIA, base->invIB, rA, rB, b3Add( cdot, bias ) );
b3Vec3 impulse = b3Sub( b3MulSV( -massScale, b ), b3MulSV( impulseScale, joint->linearImpulse ) );
joint->linearImpulse = b3Add( joint->linearImpulse, impulse );

View file

@ -454,86 +454,132 @@ void b3SolveSphericalJoint( b3JointSim* base, b3StepContext* context, bool useBi
b3Quat relQ = b3InvMulQuat( quatA, quatB );
// Solve spring
if ( joint->enableSpring && fixedRotation == false )
if ( fixedRotation == false &&
( joint->enableSpring || joint->enableMotor || joint->enableTwistLimit || joint->enableConeLimit ) )
{
// Rotation constraint error
b3Vec3 deltaRotation = b3DeltaQuatToRotation( relQ, joint->targetRotation );
b3Vec3 c = b3Neg( b3RotateVector( quatA, deltaRotation ) );
b3Vec3 bias = b3MulSV( joint->springSoftness.biasRate, c );
float massScale = joint->springSoftness.massScale;
float impulseScale = joint->springSoftness.impulseScale;
b3Matrix3 invInertiaSum = b3AddMM( iA, iB );
b3Vec3 cdot = b3Sub( wB, wA );
b3Vec3 angularImpulse = b3Vec3_zero;
b3Vec3 impulse = b3MulSub( b3MulSV( -massScale, b3MulMV( joint->rotationMass, b3Add( cdot, bias ) ) ),
impulseScale, joint->springImpulse );
joint->springImpulse = b3Add( joint->springImpulse, impulse );
wA = b3Sub( wA, b3MulMV( iA, impulse ) );
wB = b3Add( wB, b3MulMV( iB, impulse ) );
}
if ( joint->enableMotor && fixedRotation == false )
{
b3Vec3 cdot = b3Sub( wB, wA );
// todo it might be more accurate to have the motor velocity in frameA due to sub-stepping.
b3Vec3 lambda = b3Neg( b3MulMV( joint->rotationMass, b3Sub( cdot, joint->motorVelocity ) ) );
b3Vec3 newImpulse = b3Add( joint->motorImpulse, lambda );
float length = b3Length( newImpulse );
float maxImpulse = joint->maxMotorTorque * context->h;
if ( length > maxImpulse )
// Solve spring
if ( joint->enableSpring )
{
newImpulse = b3MulSV( maxImpulse / length, newImpulse );
// Rotation constraint error
b3Vec3 deltaRotation = b3DeltaQuatToRotation( relQ, joint->targetRotation );
b3Vec3 c = b3Neg( b3RotateVector( quatA, deltaRotation ) );
b3Vec3 bias = b3MulSV( joint->springSoftness.biasRate, c );
float massScale = joint->springSoftness.massScale;
float impulseScale = joint->springSoftness.impulseScale;
b3Vec3 impulse = b3MulSub( b3MulSV( -massScale, b3MulMV( joint->rotationMass, b3Add( cdot, bias ) ) ),
impulseScale, joint->springImpulse );
joint->springImpulse = b3Add( joint->springImpulse, impulse );
cdot = b3Add( cdot, b3MulMV( invInertiaSum, impulse ) );
angularImpulse = b3Add( angularImpulse, impulse );
}
lambda = b3Sub( newImpulse, joint->motorImpulse );
joint->motorImpulse = newImpulse;
wA = b3Sub( wA, b3MulMV( iA, lambda ) );
wB = b3Add( wB, b3MulMV( iB, lambda ) );
}
if ( joint->enableTwistLimit && fixedRotation == false )
{
float twistAngle = b3GetTwistAngle( relQ );
// todo does an updated twist axis help?
b3Vec3 twistJacobian = joint->twistJacobian;
// Lower limit
if ( joint->enableMotor )
{
float c = twistAngle - joint->lowerTwistAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
// todo it might be more accurate to have the motor velocity in frameA due to sub-stepping.
b3Vec3 lambda = b3Neg( b3MulMV( joint->rotationMass, b3Sub( cdot, joint->motorVelocity ) ) );
b3Vec3 newImpulse = b3Add( joint->motorImpulse, lambda );
float length = b3Length( newImpulse );
float maxImpulse = joint->maxMotorTorque * context->h;
if ( length > maxImpulse )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
newImpulse = b3MulSV( maxImpulse / length, newImpulse );
}
float cdot = b3Dot( b3Sub( wB, wA ), twistJacobian );
float oldImpulse = joint->lowerTwistImpulse;
float deltaImpulse = -massScale * joint->twistMass * ( cdot + bias ) - impulseScale * oldImpulse;
joint->lowerTwistImpulse = b3MaxFloat( oldImpulse + deltaImpulse, 0.0f );
deltaImpulse = joint->lowerTwistImpulse - oldImpulse;
lambda = b3Sub( newImpulse, joint->motorImpulse );
joint->motorImpulse = newImpulse;
wA = b3MulSub( wA, deltaImpulse, b3MulMV( iA, twistJacobian ) );
wB = b3MulAdd( wB, deltaImpulse, b3MulMV( iB, twistJacobian ) );
cdot = b3Add( cdot, b3MulMV( invInertiaSum, lambda ) );
angularImpulse = b3Add( angularImpulse, lambda );
}
// Upper limit
if ( joint->enableTwistLimit )
{
float c = joint->upperTwistAngle - twistAngle;
float twistAngle = b3GetTwistAngle( relQ );
// todo does an updated twist axis help?
b3Vec3 twistJacobian = joint->twistJacobian;
b3Vec3 twistResponse = b3MulMV( invInertiaSum, twistJacobian );
// Lower limit
{
float c = twistAngle - joint->lowerTwistAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
}
float twistCdot = b3Dot( cdot, twistJacobian );
float oldImpulse = joint->lowerTwistImpulse;
float deltaImpulse =
b3MaxFloat( -massScale * joint->twistMass * ( twistCdot + bias ) - impulseScale * oldImpulse, -oldImpulse );
joint->lowerTwistImpulse = oldImpulse + deltaImpulse;
cdot = b3MulAdd( cdot, deltaImpulse, twistResponse );
angularImpulse = b3MulAdd( angularImpulse, deltaImpulse, twistJacobian );
}
// Upper limit
{
float c = joint->upperTwistAngle - twistAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
}
// sign flipped on Cdot
float twistCdot = -b3Dot( cdot, twistJacobian );
float oldImpulse = joint->upperTwistImpulse;
float deltaImpulse =
b3MaxFloat( -massScale * joint->twistMass * ( twistCdot + bias ) - impulseScale * oldImpulse, -oldImpulse );
joint->upperTwistImpulse = oldImpulse + deltaImpulse;
// sign flipped on applied impulse
cdot = b3MulSub( cdot, deltaImpulse, twistResponse );
angularImpulse = b3MulSub( angularImpulse, deltaImpulse, twistJacobian );
}
}
if ( joint->enableConeLimit )
{
float swingAngle = b3GetSwingAngle( relQ );
// todo does an updated swing axis help?
// b3Vec3 axisA = b3RotateVector( quatA, b3Vec3_axisZ );
// b3Vec3 axisB = b3RotateVector( quatB, b3Vec3_axisZ );
// b3Vec3 swingAxis = b3Normalize( b3Cross( axisA, axisB ) );
// joint->swingAxis = swingAxis;
b3Vec3 swingAxis = joint->swingAxis;
float c = joint->coneAngle - swingAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
@ -550,56 +596,18 @@ void b3SolveSphericalJoint( b3JointSim* base, b3StepContext* context, bool useBi
}
// sign flipped on Cdot
float cdot = b3Dot( b3Sub( wA, wB ), twistJacobian );
float oldImpulse = joint->upperTwistImpulse;
float deltaImpulse = -massScale * joint->twistMass * ( cdot + bias ) - impulseScale * oldImpulse;
joint->upperTwistImpulse = b3MaxFloat( oldImpulse + deltaImpulse, 0.0f );
deltaImpulse = joint->upperTwistImpulse - oldImpulse;
float swingCdot = -b3Dot( cdot, swingAxis );
float oldImpulse = joint->swingImpulse;
float deltaImpulse =
b3MaxFloat( -massScale * joint->swingMass * ( swingCdot + bias ) - impulseScale * oldImpulse, -oldImpulse );
joint->swingImpulse = oldImpulse + deltaImpulse;
// sign flipped on applied impulse
wA = b3MulAdd( wA, deltaImpulse, b3MulMV( iA, twistJacobian ) );
wB = b3MulSub( wB, deltaImpulse, b3MulMV( iB, twistJacobian ) );
}
}
if ( joint->enableConeLimit && fixedRotation == false )
{
float swingAngle = b3GetSwingAngle( relQ );
// todo does an updated swing axis help?
// b3Vec3 axisA = b3RotateVector( quatA, b3Vec3_axisZ );
// b3Vec3 axisB = b3RotateVector( quatB, b3Vec3_axisZ );
// b3Vec3 swingAxis = b3Normalize( b3Cross( axisA, axisB ) );
// joint->swingAxis = swingAxis;
b3Vec3 swingAxis = joint->swingAxis;
float c = joint->coneAngle - swingAngle;
float bias = 0.0f;
float massScale = 1.0f;
float impulseScale = 0.0f;
if ( c > 0.0f )
{
// speculation
bias = c * context->inv_h;
}
else if ( useBias )
{
bias = base->constraintSoftness.biasRate * c;
massScale = base->constraintSoftness.massScale;
impulseScale = base->constraintSoftness.impulseScale;
angularImpulse = b3MulSub( angularImpulse, deltaImpulse, swingAxis );
}
// sign flipped on Cdot
float cdot = b3Dot( b3Sub( wA, wB ), swingAxis );
float oldImpulse = joint->swingImpulse;
float deltaImpulse = -massScale * joint->swingMass * ( cdot + bias ) - impulseScale * oldImpulse;
joint->swingImpulse = b3MaxFloat( oldImpulse + deltaImpulse, 0.0f );
deltaImpulse = joint->swingImpulse - oldImpulse;
// sign flipped on applied impulse
wA = b3MulAdd( wA, deltaImpulse, b3MulMV( iA, swingAxis ) );
wB = b3MulSub( wB, deltaImpulse, b3MulMV( iB, swingAxis ) );
wA = b3Sub( wA, b3MulMV( iA, angularImpulse ) );
wB = b3Add( wB, b3MulMV( iB, angularImpulse ) );
}
// Solve point-to-point constraint
@ -626,16 +634,7 @@ void b3SolveSphericalJoint( b3JointSim* base, b3StepContext* context, bool useBi
}
//// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
b3Matrix3 sA = b3Skew( rA );
b3Matrix3 sB = b3Skew( rB );
b3Matrix3 kA = b3MulMM( sA, b3MulMM( base->invIA, sA ) );
b3Matrix3 kB = b3MulMM( sB, b3MulMM( base->invIB, sB ) );
b3Matrix3 k = b3NegateMat3( b3AddMM( kA, kB ) );
k.cx.x += mA + mB;
k.cy.y += mA + mB;
k.cz.z += mA + mB;
b3Vec3 b = b3Solve3( k, b3Add( cdot, bias ) );
b3Vec3 b = b3SolvePointConstraint( mA, mB, base->invIA, base->invIB, rA, rB, b3Add( cdot, bias ) );
b3Vec3 impulse = b3MulSub( b3MulSV( -massScale, b ), impulseScale, joint->linearImpulse );
joint->linearImpulse = b3Add( joint->linearImpulse, impulse );

View file

@ -270,16 +270,7 @@ void b3SolveWeldJoint( b3JointSim* base, b3StepContext* context, bool useBias )
}
//// K = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
b3Matrix3 sA = b3Skew( rA );
b3Matrix3 sB = b3Skew( rB );
b3Matrix3 kA = b3MulMM( sA, b3MulMM( base->invIA, sA ) );
b3Matrix3 kB = b3MulMM( sB, b3MulMM( base->invIB, sB ) );
b3Matrix3 k = b3NegateMat3( b3AddMM( kA, kB ) );
k.cx.x += mA + mB;
k.cy.y += mA + mB;
k.cz.z += mA + mB;
b3Vec3 b = b3Solve3( k, b3Add( cdot, bias ) );
b3Vec3 b = b3SolvePointConstraint( mA, mB, base->invIA, base->invIB, rA, rB, b3Add( cdot, bias ) );
b3Vec3 impulse = b3MulSub( b3MulSV( -massScale, b ), impulseScale, joint->linearImpulse );
joint->linearImpulse = b3Add( joint->linearImpulse, impulse );

View file

@ -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 266
#define RAGDOLL_HASH 0x68FC7728
#define WAVE_PILE_SLEEP_STEP 322
#define WAVE_PILE_HASH 0x81798F75
#define RAGDOLL_SLEEP_STEP 267
#define RAGDOLL_HASH 0x01EB8FDB
#define WAVE_PILE_SLEEP_STEP 333
#define WAVE_PILE_HASH 0x80D5266B
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0x9B96A098
#define QUERY_SPAWN_HASH 0x90EE3E7C
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0x5B4429DC
#define MESH_DROP_SLEEP_STEP 218
#define MESH_DROP_HASH 0x26F9A566
#define MESH_DROP_HASH 0x10051259
#else
#define RAGDOLL_SLEEP_STEP 267
#define RAGDOLL_HASH 0x9BCA269D
#define WAVE_PILE_SLEEP_STEP 281
#define WAVE_PILE_HASH 0x23A90492
#define RAGDOLL_SLEEP_STEP 265
#define RAGDOLL_HASH 0xB98B3638
#define WAVE_PILE_SLEEP_STEP 288
#define WAVE_PILE_HASH 0xFC1CD2DC
#define QUERY_SPAWN_SLEEP_STEP 242
#define QUERY_SPAWN_HASH 0xF1EDEF47
#define QUERY_SPAWN_HASH 0x58F47348
#define QUERY_SPAWN_HIT_COUNT 59
#define QUERY_SPAWN_QUERY_HASH 0xE3271F3D
#define MESH_DROP_SLEEP_STEP 218
#define MESH_DROP_HASH 0x0A23CFA2
#define MESH_DROP_HASH 0x983F27EA
#endif
// The goldens above pin exact values for the default four point manifold. A build that