@flighthq/physics2d 0.3.1-next.975.01aea94 → 0.4.0-edge.1908.751021c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/broadphase.d.ts.map +1 -1
- package/dist/colliderTransform.d.ts +2 -2
- package/dist/colliderTransform.d.ts.map +1 -1
- package/dist/colliderTransform.js +19 -0
- package/dist/colliderTransform.js.map +1 -1
- package/dist/contract.d.ts +5 -0
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +5 -0
- package/dist/contract.js.map +1 -1
- package/dist/debugGeometry.js +23 -0
- package/dist/debugGeometry.js.map +1 -1
- package/dist/enablePhysics2DGuards.d.ts +4 -0
- package/dist/enablePhysics2DGuards.d.ts.map +1 -0
- package/dist/enablePhysics2DGuards.js +143 -0
- package/dist/enablePhysics2DGuards.js.map +1 -0
- package/dist/explainPhysics2DCollision.d.ts +3 -0
- package/dist/explainPhysics2DCollision.d.ts.map +1 -0
- package/dist/explainPhysics2DCollision.js +37 -0
- package/dist/explainPhysics2DCollision.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/jointFactories.d.ts.map +1 -1
- package/dist/jointFactories.js +12 -0
- package/dist/jointFactories.js.map +1 -1
- package/dist/jointReactions.d.ts +4 -0
- package/dist/jointReactions.d.ts.map +1 -0
- package/dist/jointReactions.js +31 -0
- package/dist/jointReactions.js.map +1 -0
- package/dist/jointRows.d.ts +2 -0
- package/dist/jointRows.d.ts.map +1 -0
- package/dist/jointRows.js +53 -0
- package/dist/jointRows.js.map +1 -0
- package/dist/joints.d.ts +10 -1
- package/dist/joints.d.ts.map +1 -1
- package/dist/joints.js +176 -54
- package/dist/joints.js.map +1 -1
- package/dist/massProperties.d.ts.map +1 -1
- package/dist/massProperties.js +25 -0
- package/dist/massProperties.js.map +1 -1
- package/dist/nodeSync.d.ts +3 -0
- package/dist/nodeSync.d.ts.map +1 -0
- package/dist/nodeSync.js +12 -0
- package/dist/nodeSync.js.map +1 -0
- package/dist/step.d.ts +4 -1
- package/dist/step.d.ts.map +1 -1
- package/dist/step.js +116 -27
- package/dist/step.js.map +1 -1
- package/dist/stepValidation.d.ts.map +1 -1
- package/dist/stepValidation.js +40 -4
- package/dist/stepValidation.js.map +1 -1
- package/dist/world.d.ts +3 -3
- package/dist/world.d.ts.map +1 -1
- package/dist/world.js +6 -3
- package/dist/world.js.map +1 -1
- package/dist/worldQueries.d.ts +4 -2
- package/dist/worldQueries.d.ts.map +1 -1
- package/dist/worldQueries.js +128 -15
- package/dist/worldQueries.js.map +1 -1
- package/package.json +8 -5
- package/src/colliderTransform.test.ts +27 -2
- package/src/debugGeometry.test.ts +37 -0
- package/src/enablePhysics2DGuards.test.ts +277 -0
- package/src/explainPhysics2DCollision.test.ts +61 -0
- package/src/islands.test.ts +4 -0
- package/src/jointFactories.test.ts +2 -0
- package/src/jointReactions.test.ts +366 -0
- package/src/jointRegistry.test.ts +2 -0
- package/src/jointRows.test.ts +99 -0
- package/src/joints.test.ts +237 -0
- package/src/massProperties.test.ts +82 -2
- package/src/nodeSync.test.ts +41 -0
- package/src/step.test.ts +603 -11
- package/src/stepValidation.test.ts +2 -0
- package/src/world.test.ts +4 -0
- package/src/worldQueries.test.ts +212 -0
package/dist/joints.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { writePhysics2DSoftRowParameters } from './jointRows';
|
|
1
2
|
import { applyPhysics2DImpulse } from './solver';
|
|
2
3
|
import { findPhysics2DBody } from './world';
|
|
3
4
|
// The built-in joint kinds. Bare names are reserved for these; a user's own joint takes a vendor prefix.
|
|
@@ -52,31 +53,14 @@ export const physics2DDistanceJointSolver = {
|
|
|
52
53
|
normalizedAxisX = 1;
|
|
53
54
|
normalizedAxisY = 0;
|
|
54
55
|
}
|
|
55
|
-
const
|
|
56
|
+
const inverseMass = axisInverseEffectiveMass(bodyA, bodyB, joint, normalizedAxisX, normalizedAxisY);
|
|
56
57
|
const separation = length - distance.length;
|
|
57
|
-
|
|
58
|
-
let bias;
|
|
59
|
-
let gamma;
|
|
60
|
-
if (distance.frequencyHz > 0) {
|
|
61
|
-
const angular = 2 * Math.PI * distance.frequencyHz;
|
|
62
|
-
const damp = 2 * mass * distance.dampingRatio * angular;
|
|
63
|
-
const spring = mass * angular * angular;
|
|
64
|
-
const gammaDenominator = dt * (damp + dt * spring);
|
|
65
|
-
gamma = gammaDenominator > 0 ? 1 / gammaDenominator : 0;
|
|
66
|
-
bias = separation * dt * spring * gamma;
|
|
67
|
-
const softMass = mass + gamma;
|
|
68
|
-
effectiveMass = softMass > 0 ? 1 / softMass : 0;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
effectiveMass = mass > 0 ? 1 / mass : 0;
|
|
72
|
-
bias = separation * (BAUMGARTE / dt);
|
|
73
|
-
gamma = 0;
|
|
74
|
-
}
|
|
58
|
+
writePhysics2DSoftRowParameters(inverseMass > 0 ? 1 / inverseMass : 0, distance.frequencyHz, distance.dampingRatio, dt, BAUMGARTE / dt, softRowScratch);
|
|
75
59
|
distance.rAX = joint.rAX;
|
|
76
60
|
const distanceState = beginJointSolve(distance, 5);
|
|
77
|
-
distanceState[0] =
|
|
78
|
-
distanceState[1] =
|
|
79
|
-
distanceState[2] =
|
|
61
|
+
distanceState[0] = softRowScratch[0];
|
|
62
|
+
distanceState[1] = separation * softRowScratch[1];
|
|
63
|
+
distanceState[2] = softRowScratch[2];
|
|
80
64
|
distanceState[3] = normalizedAxisX;
|
|
81
65
|
distanceState[4] = normalizedAxisY;
|
|
82
66
|
},
|
|
@@ -94,6 +78,14 @@ export const physics2DDistanceJointSolver = {
|
|
|
94
78
|
joint.impulse0 += lambda;
|
|
95
79
|
applyPhysics2DImpulse(bodyA, bodyB, joint.rAX, joint.rAY, joint.rBX, joint.rBY, -lambda * axisX, -lambda * axisY);
|
|
96
80
|
},
|
|
81
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
82
|
+
const state = jointSolveStates.get(joint);
|
|
83
|
+
if (state === undefined)
|
|
84
|
+
return false;
|
|
85
|
+
// `impulse0` is a signed scalar along the anchor-to-anchor axis, negative when the joint is
|
|
86
|
+
// stretched and pulling its ends together.
|
|
87
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0 * state[3], joint.impulse0 * state[4], 0, inverseTimestep, out);
|
|
88
|
+
},
|
|
97
89
|
};
|
|
98
90
|
// Couples two scalar coordinates measured against the world frame. Each coordinate contributes one
|
|
99
91
|
// Jacobian row: rotation contributes angular velocity directly, while translation contributes the
|
|
@@ -166,6 +158,17 @@ export const physics2DGearJointSolver = {
|
|
|
166
158
|
joint.impulse0 += lambda;
|
|
167
159
|
applyGearImpulse(bodyA, bodyB, state, lambda);
|
|
168
160
|
},
|
|
161
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
162
|
+
const state = jointSolveStates.get(joint);
|
|
163
|
+
if (state === undefined)
|
|
164
|
+
return false;
|
|
165
|
+
// A gear DOES have a reaction, and the same expression covers both coordinate kinds: `state[3..5]`
|
|
166
|
+
// is body B's Jacobian row, which is `(0,0,1)` for an angular coordinate — giving a pure torque and
|
|
167
|
+
// no force — and `(unit, r x unit)` for a translational one, giving a force along the axis and no
|
|
168
|
+
// couple beyond it. The ratio scales B's whole row, so it multiplies the impulse once here.
|
|
169
|
+
const impulse = joint.impulse0 * state[8];
|
|
170
|
+
return writePhysics2DReactionFromImpulses(joint, impulse * state[3], impulse * state[4], impulse * state[5], inverseTimestep, out);
|
|
171
|
+
},
|
|
169
172
|
};
|
|
170
173
|
// Drags one body toward a moving world-space target with bounded force.
|
|
171
174
|
//
|
|
@@ -202,13 +205,16 @@ export const physics2DMouseJointSolver = {
|
|
|
202
205
|
joint.rBY = localAnchorX * sin + localAnchorY * cos;
|
|
203
206
|
joint.rAX = 0;
|
|
204
207
|
joint.rAY = 0;
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
208
|
+
// The mouse spring is tuned against the body's own mass rather than a row denominator: it drags a
|
|
209
|
+
// free body toward a target rather than constraining a pair, so there is no lever arm to fold in.
|
|
210
|
+
// Only `gamma` and the bias factor are taken from the derivation — the row mass it returns is scalar
|
|
211
|
+
// and this constraint is a 2x2 block, so softness enters on the matrix diagonal below instead.
|
|
212
|
+
writePhysics2DSoftRowParameters(bodyB.inverseMass > 0 ? 1 / bodyB.inverseMass : 0, mouse.frequencyHz > 0 ? mouse.frequencyHz : DEFAULT_MOUSE_FREQUENCY_HZ, mouse.dampingRatio, dt,
|
|
213
|
+
// Unreachable: the frequency above is always positive, so only a non-positive timestep takes the
|
|
214
|
+
// hard path, and a step of no duration should correct nothing.
|
|
215
|
+
0, softRowScratch);
|
|
216
|
+
const biasFactor = softRowScratch[1];
|
|
217
|
+
const gamma = softRowScratch[2];
|
|
212
218
|
const k11 = bodyB.inverseMass + bodyB.inverseInertia * joint.rBY * joint.rBY + gamma;
|
|
213
219
|
const k12 = -bodyB.inverseInertia * joint.rBX * joint.rBY;
|
|
214
220
|
const k22 = bodyB.inverseMass + bodyB.inverseInertia * joint.rBX * joint.rBX + gamma;
|
|
@@ -262,6 +268,16 @@ export const physics2DMouseJointSolver = {
|
|
|
262
268
|
bodyB.velocityY += impulseY * bodyB.inverseMass;
|
|
263
269
|
bodyB.angularVelocity += bodyB.inverseInertia * (joint.rBX * impulseY - joint.rBY * impulseX);
|
|
264
270
|
},
|
|
271
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
272
|
+
const state = jointSolveStates.get(joint);
|
|
273
|
+
if (state === undefined)
|
|
274
|
+
return false;
|
|
275
|
+
// The one kind whose accumulators are already a world-space impulse on B, because the drag is
|
|
276
|
+
// applied to B directly rather than through the equal-and-opposite pair helper. There is no body A
|
|
277
|
+
// to push back on: the reaction goes to the world, which is what makes a mouse joint able to add
|
|
278
|
+
// momentum rather than only redistribute it.
|
|
279
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0, joint.impulse1, 0, inverseTimestep, out);
|
|
280
|
+
},
|
|
265
281
|
};
|
|
266
282
|
// Constrains two anchors to one translation axis and locks their relative angle — a slider, piston, or
|
|
267
283
|
// elevator rail. The perpendicular and angular constraints are solved as a coupled 2x2 block because
|
|
@@ -331,7 +347,7 @@ export const physics2DPrismaticJointSolver = {
|
|
|
331
347
|
const k12 = bodyA.inverseInertia * s1 + bodyB.inverseInertia * s2;
|
|
332
348
|
const k22 = bodyA.inverseInertia + bodyB.inverseInertia;
|
|
333
349
|
const axisMass = bodyA.inverseMass + bodyB.inverseMass + bodyA.inverseInertia * a1 * a1 + bodyB.inverseInertia * a2 * a2;
|
|
334
|
-
const state = beginJointSolve(prismatic,
|
|
350
|
+
const state = beginJointSolve(prismatic, 19);
|
|
335
351
|
state[0] = axisX;
|
|
336
352
|
state[1] = axisY;
|
|
337
353
|
state[2] = perpendicularX;
|
|
@@ -346,9 +362,17 @@ export const physics2DPrismaticJointSolver = {
|
|
|
346
362
|
state[11] = axisMass > 0 ? 1 / axisMass : 0;
|
|
347
363
|
state[12] = (distanceX * perpendicularX + distanceY * perpendicularY) * (BAUMGARTE / dt);
|
|
348
364
|
state[13] = (bodyB.angle - bodyA.angle - prismatic.referenceAngle) * (BAUMGARTE / dt);
|
|
349
|
-
state[14] = BAUMGARTE / dt;
|
|
350
365
|
state[15] = Math.max(0, dt * prismatic.maxMotorForce);
|
|
351
366
|
state[16] = distanceX * axisX + distanceY * axisY;
|
|
367
|
+
// The travel stop's three numbers, derived exactly as the hinge's are: `state[14]` is the bias
|
|
368
|
+
// factor, `state[17]` the row mass, `state[18]` the compliance. A hard stop keeps `BAUMGARTE / dt`
|
|
369
|
+
// rather than the hinge's `1 / dt` because a slider's stop has always corrected at that gentler rate
|
|
370
|
+
// here, and changing it would retune every existing rail under the guise of adding a feature.
|
|
371
|
+
const prismaticAxisMass = state[11];
|
|
372
|
+
writePhysics2DSoftRowParameters(prismatic.enableLimitSpring ? prismaticAxisMass : 0, prismatic.enableLimitSpring ? prismatic.limitFrequencyHz : 0, prismatic.limitDampingRatio, dt, BAUMGARTE / dt, softRowScratch);
|
|
373
|
+
state[14] = softRowScratch[1];
|
|
374
|
+
state[17] = prismatic.enableLimitSpring ? softRowScratch[0] : prismaticAxisMass;
|
|
375
|
+
state[18] = softRowScratch[2];
|
|
352
376
|
prismatic.motorImpulse ??= 0;
|
|
353
377
|
if (prismatic.enableMotor) {
|
|
354
378
|
prismatic.motorImpulse = Math.min(Math.max(prismatic.motorImpulse, -state[15]), state[15]);
|
|
@@ -403,20 +427,27 @@ export const physics2DPrismaticJointSolver = {
|
|
|
403
427
|
const translation = state[16];
|
|
404
428
|
const velocity = prismaticAxisVelocity(bodyA, bodyB, axisX, axisY, a1, a2);
|
|
405
429
|
const previous = joint.impulse2;
|
|
430
|
+
const bias = state[14];
|
|
431
|
+
const limitMass = state[17];
|
|
432
|
+
// The compliance term, bleeding the accumulated impulse back in proportion to how soft the stop is.
|
|
433
|
+
// Zero for a hard stop, which leaves every line below exactly what it was.
|
|
434
|
+
const limitGamma = state[18];
|
|
406
435
|
let total = previous;
|
|
407
436
|
if (Math.abs(prismatic.upperTranslation - prismatic.lowerTranslation) < EPSILON) {
|
|
437
|
+
// A pinned rail: the two bounds coincide, so this is a two-sided equality row and not a stop at
|
|
438
|
+
// all. It stays hard whatever the spring settings say, because there is no side to yield toward.
|
|
408
439
|
const error = translation - prismatic.lowerTranslation;
|
|
409
440
|
total += -axisMass * (velocity + error * state[14]);
|
|
410
441
|
}
|
|
411
442
|
else if (translation < prismatic.lowerTranslation) {
|
|
412
443
|
const lowerPrevious = previous < 0 ? 0 : previous;
|
|
413
444
|
const error = translation - prismatic.lowerTranslation;
|
|
414
|
-
total = Math.max(0, lowerPrevious -
|
|
445
|
+
total = Math.max(0, lowerPrevious - limitMass * (velocity + error * bias + limitGamma * lowerPrevious));
|
|
415
446
|
}
|
|
416
447
|
else if (translation > prismatic.upperTranslation) {
|
|
417
448
|
const upperPrevious = previous > 0 ? 0 : previous;
|
|
418
449
|
const error = translation - prismatic.upperTranslation;
|
|
419
|
-
total = Math.min(0, upperPrevious -
|
|
450
|
+
total = Math.min(0, upperPrevious - limitMass * (velocity + error * bias + limitGamma * upperPrevious));
|
|
420
451
|
}
|
|
421
452
|
else {
|
|
422
453
|
total = 0;
|
|
@@ -450,6 +481,17 @@ export const physics2DPrismaticJointSolver = {
|
|
|
450
481
|
joint.impulse1 += angularImpulse;
|
|
451
482
|
applyPrismaticImpulse(bodyA, bodyB, axisX, axisY, perpendicularX, perpendicularY, s1, s2, a1, a2, perpendicularImpulse, angularImpulse, 0);
|
|
452
483
|
},
|
|
484
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
485
|
+
const state = jointSolveStates.get(joint);
|
|
486
|
+
if (state === undefined)
|
|
487
|
+
return false;
|
|
488
|
+
// The rail carries three lanes and they are not interchangeable. `impulse0` resists sideways
|
|
489
|
+
// motion across the axis, `impulse2` plus the motor pushes ALONG it — a slider's motor is a linear
|
|
490
|
+
// drive, so it belongs in the force and not the torque — and `impulse1` is the only genuine couple,
|
|
491
|
+
// the one holding the two bodies at a fixed relative angle.
|
|
492
|
+
const axisImpulse = joint.impulse2 + (joint.motorImpulse ?? 0);
|
|
493
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0 * state[2] + axisImpulse * state[0], joint.impulse0 * state[3] + axisImpulse * state[1], joint.impulse1, inverseTimestep, out);
|
|
494
|
+
},
|
|
453
495
|
};
|
|
454
496
|
// Couples two independently suspended anchor lengths through a fixed ratio. The two ground anchors are
|
|
455
497
|
// world-space points and absorb the reaction, so each body's impulse is applied toward its own ground
|
|
@@ -534,6 +576,17 @@ export const physics2DPulleyJointSolver = {
|
|
|
534
576
|
joint.impulse0 += lambda;
|
|
535
577
|
applyPulleyImpulse(bodyA, bodyB, joint, state[0], state[1], state[2], state[3], state[6], lambda);
|
|
536
578
|
},
|
|
579
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
580
|
+
const state = jointSolveStates.get(joint);
|
|
581
|
+
if (state === undefined)
|
|
582
|
+
return false;
|
|
583
|
+
// Body B is pulled along ITS OWN rope segment, toward its own ground anchor — not toward body A.
|
|
584
|
+
// The two ends of a pulley are coupled through the rope rather than acting on each other, so this
|
|
585
|
+
// reaction is not the negative of body A's; the ground anchors take the difference. The ratio is
|
|
586
|
+
// the mechanical advantage on B's segment and multiplies the tension it sees.
|
|
587
|
+
const impulse = joint.impulse0 * state[6];
|
|
588
|
+
return writePhysics2DReactionFromImpulses(joint, impulse * state[2], impulse * state[3], 0, inverseTimestep, out);
|
|
589
|
+
},
|
|
537
590
|
};
|
|
538
591
|
// Pins two bodies at a point, leaving rotation free — a hinge.
|
|
539
592
|
//
|
|
@@ -621,14 +674,21 @@ export const physics2DRevoluteJointSolver = {
|
|
|
621
674
|
const angularMass = inverseInertiaSum > 0 ? 1 / inverseInertiaSum : 0;
|
|
622
675
|
// The motor accumulator is revolute-specific, so the shared entry point above cannot reach it.
|
|
623
676
|
revolute.motorImpulse ??= 0;
|
|
624
|
-
const revoluteState = beginJointSolve(joint,
|
|
677
|
+
const revoluteState = beginJointSolve(joint, 9);
|
|
625
678
|
revoluteState[0] = errorX * (BAUMGARTE / dt);
|
|
626
679
|
revoluteState[1] = errorY * (BAUMGARTE / dt);
|
|
627
680
|
revoluteState[2] = angularMass;
|
|
628
681
|
revoluteState[3] = 0;
|
|
629
682
|
revoluteState[4] = 0;
|
|
630
683
|
revoluteState[5] = dt * revolute.maxMotorTorque;
|
|
631
|
-
|
|
684
|
+
// The LIMIT row's three numbers. A hard stop corrects its whole violation in one step — bias factor
|
|
685
|
+
// `1 / dt`, no compliance, and the row's own mass — and a compliant one trades that for the spring the
|
|
686
|
+
// author described. Both leave the row's arithmetic below identical, which is the point of deriving
|
|
687
|
+
// them here rather than branching in `solve`.
|
|
688
|
+
writePhysics2DSoftRowParameters(revolute.enableLimitSpring ? angularMass : 0, revolute.enableLimitSpring ? revolute.limitFrequencyHz : 0, revolute.limitDampingRatio, dt, 1 / dt, softRowScratch);
|
|
689
|
+
revoluteState[6] = softRowScratch[1];
|
|
690
|
+
revoluteState[7] = revolute.enableLimitSpring ? softRowScratch[0] : angularMass;
|
|
691
|
+
revoluteState[8] = softRowScratch[2];
|
|
632
692
|
},
|
|
633
693
|
solve(world, joint) {
|
|
634
694
|
const revolute = joint;
|
|
@@ -658,6 +718,8 @@ export const physics2DRevoluteJointSolver = {
|
|
|
658
718
|
}
|
|
659
719
|
if (revolute.enableLimit) {
|
|
660
720
|
const bias = state[6];
|
|
721
|
+
const limitMass = state[7];
|
|
722
|
+
const limitGamma = state[8];
|
|
661
723
|
const angle = bodyB.angle - bodyA.angle - revolute.referenceAngle;
|
|
662
724
|
// Each end is a one-sided constraint: the non-negative clamp on the running impulse lets a
|
|
663
725
|
// limit push the angle back inside the interval and never pull it in.
|
|
@@ -678,16 +740,20 @@ export const physics2DRevoluteJointSolver = {
|
|
|
678
740
|
// negative and the clamp holds the accumulator at zero, so the limit contributes nothing.
|
|
679
741
|
// Outside it is negative, which drives the desired impulse positive and pushes the angle back.
|
|
680
742
|
const lowerError = angle - revolute.lowerAngle;
|
|
681
|
-
const lowerDesired = -angularMass * (bodyB.angularVelocity - bodyA.angularVelocity + lowerError * bias);
|
|
682
743
|
const lowerPrevious = state[3];
|
|
744
|
+
// `gamma * previous` is the compliance term, and it is what makes a soft stop yield: it bleeds the
|
|
745
|
+
// accumulated impulse back each iteration in proportion to how compliant the row is, so the row
|
|
746
|
+
// converges on a spring force instead of on whatever it takes to arrest the coordinate. At
|
|
747
|
+
// `gamma` zero this is exactly the hard row it replaced.
|
|
748
|
+
const lowerDesired = -limitMass * (bodyB.angularVelocity - bodyA.angularVelocity + lowerError * bias + limitGamma * lowerPrevious);
|
|
683
749
|
const lowerTotal = Math.max(lowerPrevious + lowerDesired, 0);
|
|
684
750
|
state[3] = lowerTotal;
|
|
685
751
|
const lowerApplied = lowerTotal - lowerPrevious;
|
|
686
752
|
bodyA.angularVelocity -= bodyA.inverseInertia * lowerApplied;
|
|
687
753
|
bodyB.angularVelocity += bodyB.inverseInertia * lowerApplied;
|
|
688
754
|
const upperError = revolute.upperAngle - angle;
|
|
689
|
-
const upperDesired = -angularMass * (bodyA.angularVelocity - bodyB.angularVelocity + upperError * bias);
|
|
690
755
|
const upperPrevious = state[4];
|
|
756
|
+
const upperDesired = -limitMass * (bodyA.angularVelocity - bodyB.angularVelocity + upperError * bias + limitGamma * upperPrevious);
|
|
691
757
|
const upperTotal = Math.max(upperPrevious + upperDesired, 0);
|
|
692
758
|
state[4] = upperTotal;
|
|
693
759
|
const upperApplied = upperTotal - upperPrevious;
|
|
@@ -697,6 +763,16 @@ export const physics2DRevoluteJointSolver = {
|
|
|
697
763
|
}
|
|
698
764
|
solvePointConstraint(bodyA, bodyB, joint, state[0], state[1]);
|
|
699
765
|
},
|
|
766
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
767
|
+
const state = jointSolveStates.get(joint);
|
|
768
|
+
if (state === undefined)
|
|
769
|
+
return false;
|
|
770
|
+
// The hinge itself is a pure force at the anchor: `impulse0`/`impulse1` are already world x and y.
|
|
771
|
+
// Everything angular is bolted on beside it — the motor, and the two one-sided limits, whose signs
|
|
772
|
+
// are OPPOSITE because the lower end pushes the relative angle up and the upper end pushes it down.
|
|
773
|
+
const limitImpulse = state[3] - state[4];
|
|
774
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0, joint.impulse1, (joint.motorImpulse ?? 0) + limitImpulse, inverseTimestep, out);
|
|
775
|
+
},
|
|
700
776
|
};
|
|
701
777
|
// An inequality constraint that acts only at full extension. Slack within `maxLength`, caught at it.
|
|
702
778
|
//
|
|
@@ -728,13 +804,13 @@ export const physics2DRopeJointSolver = {
|
|
|
728
804
|
const length = Math.sqrt(axisX * axisX + axisY * axisY);
|
|
729
805
|
const unitX = length > EPSILON ? axisX / length : 1;
|
|
730
806
|
const unitY = length > EPSILON ? axisY / length : 0;
|
|
731
|
-
const
|
|
807
|
+
const inverseMass = axisInverseEffectiveMass(bodyA, bodyB, joint, unitX, unitY);
|
|
732
808
|
const excess = length - rope.maxLength;
|
|
733
809
|
// Slack: no constraint at all this step, which is what a rope IS. Marked by a zero effective mass so
|
|
734
810
|
// the iterations skip it without a second flag to keep in sync.
|
|
735
811
|
const active = excess > 0;
|
|
736
812
|
const ropeState = beginJointSolve(rope, 5);
|
|
737
|
-
ropeState[0] = active &&
|
|
813
|
+
ropeState[0] = active && inverseMass > 0 ? 1 / inverseMass : 0;
|
|
738
814
|
ropeState[1] = active ? excess * (BAUMGARTE / dt) : 0;
|
|
739
815
|
ropeState[2] = 0;
|
|
740
816
|
ropeState[3] = unitX;
|
|
@@ -758,6 +834,14 @@ export const physics2DRopeJointSolver = {
|
|
|
758
834
|
lambda = joint.impulse0 - previous;
|
|
759
835
|
applyPhysics2DImpulse(bodyA, bodyB, joint.rAX, joint.rAY, joint.rBX, joint.rBY, -lambda * axisX, -lambda * axisY);
|
|
760
836
|
},
|
|
837
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
838
|
+
const state = jointSolveStates.get(joint);
|
|
839
|
+
if (state === undefined)
|
|
840
|
+
return false;
|
|
841
|
+
// One-sided, so this reports zero for every step the rope spends slack — which is the honest
|
|
842
|
+
// answer and the one a breakable rope needs: a rope that is not taut is carrying no load.
|
|
843
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0 * state[3], joint.impulse0 * state[4], 0, inverseTimestep, out);
|
|
844
|
+
},
|
|
761
845
|
};
|
|
762
846
|
// Keeps two anchors on a suspension axis while allowing relative rotation. The lateral constraint is
|
|
763
847
|
// rigid; travel along the axis is free when frequencyHz is zero and becomes a damped spring otherwise.
|
|
@@ -823,14 +907,12 @@ export const physics2DWheelJointSolver = {
|
|
|
823
907
|
let gamma = 0;
|
|
824
908
|
let springBias = 0;
|
|
825
909
|
if (wheel.frequencyHz > 0 && axisDenominator > 0) {
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
gamma =
|
|
832
|
-
springBias = (translation - wheel.restTranslation) * dt * spring * gamma;
|
|
833
|
-
springMass = 1 / (axisDenominator + gamma);
|
|
910
|
+
// A zero frequency disables the suspension row outright below rather than stiffening it, so the
|
|
911
|
+
// hard fallback is unreachable from here.
|
|
912
|
+
writePhysics2DSoftRowParameters(1 / axisDenominator, wheel.frequencyHz, wheel.dampingRatio, dt, 0, softRowScratch);
|
|
913
|
+
springMass = softRowScratch[0];
|
|
914
|
+
springBias = (translation - wheel.restTranslation) * softRowScratch[1];
|
|
915
|
+
gamma = softRowScratch[2];
|
|
834
916
|
}
|
|
835
917
|
else {
|
|
836
918
|
joint.impulse1 = 0;
|
|
@@ -891,6 +973,16 @@ export const physics2DWheelJointSolver = {
|
|
|
891
973
|
joint.impulse0 += lateralImpulse;
|
|
892
974
|
applyPrismaticImpulse(bodyA, bodyB, state[0], state[1], state[2], state[3], state[4], state[5], state[6], state[7], lateralImpulse, 0, 0);
|
|
893
975
|
},
|
|
976
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
977
|
+
const state = jointSolveStates.get(joint);
|
|
978
|
+
if (state === undefined)
|
|
979
|
+
return false;
|
|
980
|
+
// A wheel leaves relative rotation free, so unlike the prismatic rail it has no angular constraint
|
|
981
|
+
// lane at all and its only couple is the drive motor. `impulse0` is the lateral grip that keeps the
|
|
982
|
+
// wheel from sliding sideways; `impulse1` is the suspension load along the axis, which is the one a
|
|
983
|
+
// caller usually wants.
|
|
984
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0 * state[2] + joint.impulse1 * state[0], joint.impulse0 * state[3] + joint.impulse1 * state[1], joint.motorImpulse ?? 0, inverseTimestep, out);
|
|
985
|
+
},
|
|
894
986
|
};
|
|
895
987
|
// Pins the anchors together and locks the relative angle — rigid attachment.
|
|
896
988
|
//
|
|
@@ -954,7 +1046,31 @@ export const physics2DWeldJointSolver = {
|
|
|
954
1046
|
bodyB.angularVelocity += bodyB.inverseInertia * angularLambda;
|
|
955
1047
|
solvePointConstraint(bodyA, bodyB, joint, state[0], state[1]);
|
|
956
1048
|
},
|
|
1049
|
+
writeReaction(_world, joint, inverseTimestep, out) {
|
|
1050
|
+
const state = jointSolveStates.get(joint);
|
|
1051
|
+
if (state === undefined)
|
|
1052
|
+
return false;
|
|
1053
|
+
// The one built-in kind that resists both ways at once, which is why it is the natural thing to
|
|
1054
|
+
// make breakable: `impulse0`/`impulse1` hold the anchors together and `impulse2` holds the angle.
|
|
1055
|
+
return writePhysics2DReactionFromImpulses(joint, joint.impulse0, joint.impulse1, joint.impulse2, inverseTimestep, out);
|
|
1056
|
+
},
|
|
957
1057
|
};
|
|
1058
|
+
// Scales one kind's total impulse on body B into the reaction pair, and is the single place the
|
|
1059
|
+
// impulse-to-force conversion happens.
|
|
1060
|
+
//
|
|
1061
|
+
// Callers pass the couple ALREADY separated from the anchor force, and every built-in kind can do that
|
|
1062
|
+
// by inspection rather than by arithmetic: each of the three shared appliers (`applyPhysics2DImpulse`,
|
|
1063
|
+
// `applyPrismaticImpulse`, `applyPulleyImpulse`) gives body B exactly `rB x F` of angular impulse for the
|
|
1064
|
+
// linear impulse it delivers, so a kind's couple is whatever it wrote to `bodyB.angularVelocity` BESIDES
|
|
1065
|
+
// that — a motor, a limit, an angle constraint — and zero for the kinds that only pull along a line.
|
|
1066
|
+
// That is why there are not nine torque formulas here, and why a new kind's author has one short
|
|
1067
|
+
// question to answer rather than a derivation to redo.
|
|
1068
|
+
function writePhysics2DReactionFromImpulses(joint, linearX, linearY, angular, inverseTimestep, out) {
|
|
1069
|
+
out.forceX = linearX * inverseTimestep;
|
|
1070
|
+
out.forceY = linearY * inverseTimestep;
|
|
1071
|
+
out.torque = angular * inverseTimestep;
|
|
1072
|
+
return true;
|
|
1073
|
+
}
|
|
958
1074
|
function writeGearJacobian(kind, axisX, axisY, rX, rY, state, offset) {
|
|
959
1075
|
if (kind === 'angular') {
|
|
960
1076
|
state[offset] = 0;
|
|
@@ -1020,14 +1136,14 @@ function applyPulleyImpulse(bodyA, bodyB, joint, unitAX, unitAY, unitBX, unitBY,
|
|
|
1020
1136
|
// The shared two-axis point solve behind revolute and weld. Each axis is solved against the velocities the
|
|
1021
1137
|
// other just left, which is the sequential-impulse property doing its job within one constraint.
|
|
1022
1138
|
function solvePointConstraint(bodyA, bodyB, joint, biasX, biasY) {
|
|
1023
|
-
const
|
|
1139
|
+
const inverseMassX = axisInverseEffectiveMass(bodyA, bodyB, joint, 1, 0);
|
|
1024
1140
|
const velocityX = axisRelativeVelocity(bodyA, bodyB, joint, 1, 0);
|
|
1025
|
-
const lambdaX =
|
|
1141
|
+
const lambdaX = inverseMassX > 0 ? -(velocityX + biasX) / inverseMassX : 0;
|
|
1026
1142
|
joint.impulse0 += lambdaX;
|
|
1027
1143
|
applyPhysics2DImpulse(bodyA, bodyB, joint.rAX, joint.rAY, joint.rBX, joint.rBY, -lambdaX, 0);
|
|
1028
|
-
const
|
|
1144
|
+
const inverseMassY = axisInverseEffectiveMass(bodyA, bodyB, joint, 0, 1);
|
|
1029
1145
|
const velocityY = axisRelativeVelocity(bodyA, bodyB, joint, 0, 1);
|
|
1030
|
-
const lambdaY =
|
|
1146
|
+
const lambdaY = inverseMassY > 0 ? -(velocityY + biasY) / inverseMassY : 0;
|
|
1031
1147
|
joint.impulse1 += lambdaY;
|
|
1032
1148
|
applyPhysics2DImpulse(bodyA, bodyB, joint.rAX, joint.rAY, joint.rBX, joint.rBY, 0, -lambdaY);
|
|
1033
1149
|
}
|
|
@@ -1043,9 +1159,11 @@ function writeJointAnchors(bodyA, bodyB, joint) {
|
|
|
1043
1159
|
joint.rBX = (joint.localAnchorBX - bodyB.centerX) * cosB - (joint.localAnchorBY - bodyB.centerY) * sinB;
|
|
1044
1160
|
joint.rBY = (joint.localAnchorBX - bodyB.centerX) * sinB + (joint.localAnchorBY - bodyB.centerY) * cosB;
|
|
1045
1161
|
}
|
|
1046
|
-
// The pair's
|
|
1047
|
-
// constraint uses, over the joint's lever arms instead of a contact point's.
|
|
1048
|
-
|
|
1162
|
+
// The pair's INVERSE effective mass along `axis` at the joint anchors — the same quantity a contact
|
|
1163
|
+
// constraint uses, over the joint's lever arms instead of a contact point's. Every caller divides by it
|
|
1164
|
+
// or reciprocates it; nothing may use it as a mass. The name says `Inverse` because the previous one did
|
|
1165
|
+
// not, and a spring built on it silently authored the wrong stiffness for years.
|
|
1166
|
+
function axisInverseEffectiveMass(bodyA, bodyB, joint, axisX, axisY) {
|
|
1049
1167
|
const crossA = joint.rAX * axisY - joint.rAY * axisX;
|
|
1050
1168
|
const crossB = joint.rBX * axisY - joint.rBY * axisX;
|
|
1051
1169
|
return (bodyA.inverseMass +
|
|
@@ -1116,8 +1234,12 @@ function beginJointSolve(joint, length) {
|
|
|
1116
1234
|
// Keying weakly makes the retention question disappear rather than answering it — the entry goes when
|
|
1117
1235
|
// the joint does, at every exit path, including ones nobody has written yet.
|
|
1118
1236
|
const jointSolveStates = new WeakMap();
|
|
1237
|
+
const DEFAULT_MOUSE_FREQUENCY_HZ = 5;
|
|
1119
1238
|
const EPSILON = 1e-9;
|
|
1120
1239
|
// The fraction of a joint's positional error corrected per step. Matches the contact solver's rationale:
|
|
1121
1240
|
// correcting all of it at once turns a deep error into an explosion.
|
|
1122
1241
|
const BAUMGARTE = 0.2;
|
|
1242
|
+
// Reused across every joint prepared this step: `prepare` runs in one synchronous pass per solver and
|
|
1243
|
+
// reads these three numbers out before the next call, and the world already rejects a recursive step.
|
|
1244
|
+
const softRowScratch = [0, 0, 0];
|
|
1123
1245
|
//# sourceMappingURL=joints.js.map
|