@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.
Files changed (77) hide show
  1. package/dist/broadphase.d.ts.map +1 -1
  2. package/dist/colliderTransform.d.ts +2 -2
  3. package/dist/colliderTransform.d.ts.map +1 -1
  4. package/dist/colliderTransform.js +19 -0
  5. package/dist/colliderTransform.js.map +1 -1
  6. package/dist/contract.d.ts +5 -0
  7. package/dist/contract.d.ts.map +1 -1
  8. package/dist/contract.js +5 -0
  9. package/dist/contract.js.map +1 -1
  10. package/dist/debugGeometry.js +23 -0
  11. package/dist/debugGeometry.js.map +1 -1
  12. package/dist/enablePhysics2DGuards.d.ts +4 -0
  13. package/dist/enablePhysics2DGuards.d.ts.map +1 -0
  14. package/dist/enablePhysics2DGuards.js +143 -0
  15. package/dist/enablePhysics2DGuards.js.map +1 -0
  16. package/dist/explainPhysics2DCollision.d.ts +3 -0
  17. package/dist/explainPhysics2DCollision.d.ts.map +1 -0
  18. package/dist/explainPhysics2DCollision.js +37 -0
  19. package/dist/explainPhysics2DCollision.js.map +1 -0
  20. package/dist/index.d.ts +5 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +5 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/jointFactories.d.ts.map +1 -1
  25. package/dist/jointFactories.js +12 -0
  26. package/dist/jointFactories.js.map +1 -1
  27. package/dist/jointReactions.d.ts +4 -0
  28. package/dist/jointReactions.d.ts.map +1 -0
  29. package/dist/jointReactions.js +31 -0
  30. package/dist/jointReactions.js.map +1 -0
  31. package/dist/jointRows.d.ts +2 -0
  32. package/dist/jointRows.d.ts.map +1 -0
  33. package/dist/jointRows.js +53 -0
  34. package/dist/jointRows.js.map +1 -0
  35. package/dist/joints.d.ts +10 -1
  36. package/dist/joints.d.ts.map +1 -1
  37. package/dist/joints.js +176 -54
  38. package/dist/joints.js.map +1 -1
  39. package/dist/massProperties.d.ts.map +1 -1
  40. package/dist/massProperties.js +25 -0
  41. package/dist/massProperties.js.map +1 -1
  42. package/dist/nodeSync.d.ts +3 -0
  43. package/dist/nodeSync.d.ts.map +1 -0
  44. package/dist/nodeSync.js +12 -0
  45. package/dist/nodeSync.js.map +1 -0
  46. package/dist/step.d.ts +4 -1
  47. package/dist/step.d.ts.map +1 -1
  48. package/dist/step.js +116 -27
  49. package/dist/step.js.map +1 -1
  50. package/dist/stepValidation.d.ts.map +1 -1
  51. package/dist/stepValidation.js +40 -4
  52. package/dist/stepValidation.js.map +1 -1
  53. package/dist/world.d.ts +3 -3
  54. package/dist/world.d.ts.map +1 -1
  55. package/dist/world.js +6 -3
  56. package/dist/world.js.map +1 -1
  57. package/dist/worldQueries.d.ts +4 -2
  58. package/dist/worldQueries.d.ts.map +1 -1
  59. package/dist/worldQueries.js +128 -15
  60. package/dist/worldQueries.js.map +1 -1
  61. package/package.json +8 -5
  62. package/src/colliderTransform.test.ts +27 -2
  63. package/src/debugGeometry.test.ts +37 -0
  64. package/src/enablePhysics2DGuards.test.ts +277 -0
  65. package/src/explainPhysics2DCollision.test.ts +61 -0
  66. package/src/islands.test.ts +4 -0
  67. package/src/jointFactories.test.ts +2 -0
  68. package/src/jointReactions.test.ts +366 -0
  69. package/src/jointRegistry.test.ts +2 -0
  70. package/src/jointRows.test.ts +99 -0
  71. package/src/joints.test.ts +237 -0
  72. package/src/massProperties.test.ts +82 -2
  73. package/src/nodeSync.test.ts +41 -0
  74. package/src/step.test.ts +603 -11
  75. package/src/stepValidation.test.ts +2 -0
  76. package/src/world.test.ts +4 -0
  77. package/src/worldQueries.test.ts +212 -0
@@ -0,0 +1,366 @@
1
+ import type { Physics2DJoint, Physics2DJointReaction, Physics2DWorld, RigidBody2D } from '@flighthq/types/contract';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { createPhysics2DJointReaction, writePhysics2DJointReaction } from './jointReactions';
5
+ import { addPhysics2DJoint } from './jointRegistry';
6
+ import {
7
+ Physics2DDistanceJointKind,
8
+ Physics2DGearJointKind,
9
+ Physics2DMouseJointKind,
10
+ Physics2DPrismaticJointKind,
11
+ Physics2DPulleyJointKind,
12
+ Physics2DRevoluteJointKind,
13
+ Physics2DRopeJointKind,
14
+ Physics2DWeldJointKind,
15
+ Physics2DWheelJointKind,
16
+ } from './joints';
17
+ import { registerBuiltInPhysics2DJointSolvers } from './registerBuiltInPhysics2DJointSolvers';
18
+ import { stepPhysics2D } from './step';
19
+ import { addPhysics2DBody, createPhysics2DCollider, createPhysics2DWorld, createRigidBody2D } from './world';
20
+
21
+ // Every assertion in this file is a STATICS check, not a comparison against the formula under test. A
22
+ // joint holding a weight still must push up with exactly that weight, and a joint holding an off-centre
23
+ // weight still must supply exactly the moment gravity applies about its anchor. Those are facts about
24
+ // the world rather than about the code, so a sign error, a missing ratio, or a mistaken state slot fails
25
+ // them — and none of them could be satisfied by an implementation that is merely self-consistent.
26
+ //
27
+ // The timestep is deliberately small and the settle deliberately long: the reaction reports what the
28
+ // solver CONVERGED on, so a coarse step reports its own residual error rather than the analytic answer.
29
+
30
+ const GRAVITY = -10;
31
+ const DT = 1 / 240;
32
+ const SETTLE_STEPS = 4000;
33
+ const MATERIAL = { density: 1, friction: 0, restitution: 0 };
34
+
35
+ function ball(world: Physics2DWorld, type: RigidBody2D['type'], x: number, y: number, offsetX = 0): RigidBody2D {
36
+ const body = createRigidBody2D(type, x, y);
37
+ body.colliders.push(createPhysics2DCollider({ kind: 'circle', x: offsetX, y: 0, radius: 0.4 }, MATERIAL));
38
+ return addPhysics2DBody(world, body);
39
+ }
40
+
41
+ function base(kind: string, bodyA: number, bodyB: number): Physics2DJoint {
42
+ return {
43
+ kind,
44
+ bodyA,
45
+ bodyB,
46
+ localAnchorAX: 0,
47
+ localAnchorAY: 0,
48
+ localAnchorBX: 0,
49
+ localAnchorBY: 0,
50
+ collideConnected: false,
51
+ breakForce: Number.POSITIVE_INFINITY,
52
+ breakTorque: Number.POSITIVE_INFINITY,
53
+ impulse0: 0,
54
+ impulse1: 0,
55
+ impulse2: 0,
56
+ rAX: 0,
57
+ rAY: 0,
58
+ rBX: 0,
59
+ rBY: 0,
60
+ };
61
+ }
62
+
63
+ function settledWorld(): Physics2DWorld {
64
+ const world = createPhysics2DWorld(0, GRAVITY);
65
+ registerBuiltInPhysics2DJointSolvers(world);
66
+ return world;
67
+ }
68
+
69
+ function settle(world: Physics2DWorld, joint: Physics2DJoint, steps = SETTLE_STEPS): Physics2DJointReaction {
70
+ addPhysics2DJoint(world, joint);
71
+ for (let step = 0; step < steps; step++) stepPhysics2D(world, DT);
72
+ const out = createPhysics2DJointReaction();
73
+ expect(writePhysics2DJointReaction(world, joint, DT, out)).toBe(true);
74
+ return out;
75
+ }
76
+
77
+ describe('createPhysics2DJointReaction', () => {
78
+ it('starts at rest', () => {
79
+ expect(createPhysics2DJointReaction()).toEqual({ forceX: 0, forceY: 0, torque: 0 });
80
+ });
81
+
82
+ it('allocates a fresh object each call, so two readings do not alias', () => {
83
+ const first = createPhysics2DJointReaction();
84
+ const second = createPhysics2DJointReaction();
85
+ first.forceY = 5;
86
+ expect(second.forceY).toBe(0);
87
+ });
88
+ });
89
+
90
+ describe('writePhysics2DJointReaction', () => {
91
+ it('reports a hanging weight for every kind that carries one along its axis', () => {
92
+ // Five kinds, one law. Each holds the same bob against the same gravity by a different mechanism —
93
+ // a rigid rod, a taut rope, a hinge, a weld, and a slider's perpendicular lane — and every one of
94
+ // them must report the identical upward force, because the bob's weight does not care how it is held.
95
+ for (const kind of [
96
+ Physics2DDistanceJointKind,
97
+ Physics2DRopeJointKind,
98
+ Physics2DRevoluteJointKind,
99
+ Physics2DWeldJointKind,
100
+ Physics2DPrismaticJointKind,
101
+ ]) {
102
+ const world = settledWorld();
103
+ const anchor = ball(world, 'static', 0, 0);
104
+ const bob = ball(world, 'dynamic', 0, -2);
105
+ const joint = base(kind, anchor.index, bob.index);
106
+ if (kind === Physics2DDistanceJointKind) Object.assign(joint, { length: 2, frequencyHz: 0, dampingRatio: 0 });
107
+ if (kind === Physics2DRopeJointKind) Object.assign(joint, { maxLength: 2 });
108
+ if (kind === Physics2DRevoluteJointKind) {
109
+ Object.assign(joint, {
110
+ referenceAngle: 0,
111
+ enableMotor: false,
112
+ motorSpeed: 0,
113
+ maxMotorTorque: 0,
114
+ enableLimit: false,
115
+ lowerAngle: 0,
116
+ upperAngle: 0,
117
+ motorImpulse: 0,
118
+ });
119
+ }
120
+ if (kind === Physics2DWeldJointKind) Object.assign(joint, { referenceAngle: 0 });
121
+ if (kind === Physics2DPrismaticJointKind) {
122
+ Object.assign(joint, {
123
+ localAxisAX: 1,
124
+ localAxisAY: 0,
125
+ referenceAngle: 0,
126
+ enableMotor: false,
127
+ motorSpeed: 0,
128
+ maxMotorForce: 0,
129
+ enableLimit: false,
130
+ lowerTranslation: 0,
131
+ upperTranslation: 0,
132
+ motorImpulse: 0,
133
+ });
134
+ }
135
+
136
+ const reaction = settle(world, joint);
137
+
138
+ expect(reaction.forceY, kind).toBeCloseTo(bob.mass * -GRAVITY, 9);
139
+ expect(reaction.forceX, kind).toBeCloseTo(0, 9);
140
+ // Anchored through the centre of mass, so gravity has no lever arm and none of these five twists.
141
+ expect(reaction.torque, kind).toBeCloseTo(0, 9);
142
+ }
143
+ });
144
+
145
+ it('reports the suspension load a wheel spring carries', () => {
146
+ const world = settledWorld();
147
+ const hub = ball(world, 'static', 0, 0);
148
+ const wheel = ball(world, 'dynamic', 0, -2);
149
+ const reaction = settle(
150
+ world,
151
+ Object.assign(base(Physics2DWheelJointKind, hub.index, wheel.index), {
152
+ localAxisAX: 0,
153
+ localAxisAY: 1,
154
+ restTranslation: -2,
155
+ frequencyHz: 4,
156
+ dampingRatio: 0.7,
157
+ enableMotor: false,
158
+ motorSpeed: 0,
159
+ maxMotorTorque: 0,
160
+ motorImpulse: 0,
161
+ }),
162
+ );
163
+
164
+ // A SPRING, so it settles slightly compressed rather than exactly at rest length, and the load it
165
+ // reports is the weight to within that sag. Demanding exactness here would be demanding that a
166
+ // suspension not compress.
167
+ expect(reaction.forceY).toBeCloseTo(wheel.mass * -GRAVITY, 4);
168
+ expect(reaction.forceX).toBeCloseTo(0, 9);
169
+ });
170
+
171
+ it('reports the tension in a pulley segment, which pulls toward its own ground anchor', () => {
172
+ // A pulley is the one built-in whose two ends do NOT act on each other: each is pulled toward its
173
+ // own ground point and the difference goes into the ground. Two equal weights balance, so each
174
+ // segment carries exactly one weight.
175
+ const world = settledWorld();
176
+ const left = ball(world, 'dynamic', -1, -2);
177
+ const right = ball(world, 'dynamic', 1, -2);
178
+ const reaction = settle(
179
+ world,
180
+ Object.assign(base(Physics2DPulleyJointKind, left.index, right.index), {
181
+ groundAnchorAX: -1,
182
+ groundAnchorAY: 0,
183
+ groundAnchorBX: 1,
184
+ groundAnchorBY: 0,
185
+ ratio: 1,
186
+ constant: 4,
187
+ }),
188
+ );
189
+
190
+ expect(reaction.forceY).toBeCloseTo(right.mass * -GRAVITY, 9);
191
+ expect(reaction.forceX).toBeCloseTo(0, 9);
192
+ });
193
+
194
+ it('reports the drag force a mouse joint spends holding a body against gravity', () => {
195
+ const world = settledWorld();
196
+ const bob = ball(world, 'dynamic', 0, 0);
197
+ const reaction = settle(
198
+ world,
199
+ Object.assign(base(Physics2DMouseJointKind, bob.index, bob.index), {
200
+ targetX: 0,
201
+ targetY: 0,
202
+ maxForce: 1000,
203
+ frequencyHz: 20,
204
+ dampingRatio: 1,
205
+ }),
206
+ );
207
+
208
+ expect(reaction.forceY).toBeCloseTo(bob.mass * -GRAVITY, 6);
209
+ });
210
+
211
+ it('reports the couple a weld supplies against an off-centre weight', () => {
212
+ // The torque lane, checked by moment balance rather than by the formula. The arm's centre of mass
213
+ // sits 1.5 to the right of the anchor, so gravity applies m*g*1.5 about it and the weld must supply
214
+ // exactly the negative of that — while ALSO reporting the full weight as a force. Getting one lane
215
+ // right and the other wrong fails here, which is the point of testing them together.
216
+ const world = settledWorld();
217
+ const post = ball(world, 'static', 0, 0);
218
+ const arm = ball(world, 'dynamic', 0, 0, 1.5);
219
+ const reaction = settle(
220
+ world,
221
+ Object.assign(base(Physics2DWeldJointKind, post.index, arm.index), { referenceAngle: 0 }),
222
+ );
223
+
224
+ expect(reaction.torque).toBeCloseTo(-(arm.mass * GRAVITY * 1.5), 6);
225
+ expect(reaction.forceY).toBeCloseTo(arm.mass * -GRAVITY, 6);
226
+ });
227
+
228
+ it('reports the motor torque a revolute joint spends holding an off-centre weight level', () => {
229
+ const world = settledWorld();
230
+ const post = ball(world, 'static', 0, 0);
231
+ const arm = ball(world, 'dynamic', 0, 0, 1.5);
232
+ const reaction = settle(
233
+ world,
234
+ Object.assign(base(Physics2DRevoluteJointKind, post.index, arm.index), {
235
+ referenceAngle: 0,
236
+ enableMotor: true,
237
+ motorSpeed: 0,
238
+ maxMotorTorque: 1000,
239
+ enableLimit: false,
240
+ lowerAngle: 0,
241
+ upperAngle: 0,
242
+ motorImpulse: 0,
243
+ }),
244
+ );
245
+
246
+ expect(reaction.torque).toBeCloseTo(-(arm.mass * GRAVITY * 1.5), 4);
247
+ expect(reaction.forceY).toBeCloseTo(arm.mass * -GRAVITY, 6);
248
+ });
249
+
250
+ it('reports the torque a gear transmits, which is a real reaction and not an absent one', () => {
251
+ // Two identical bodies geared 1:1 with a steady torque on the driver. The constraint makes them
252
+ // counter-rotate, so the pair shares the torque evenly and the gear passes exactly half of it on —
253
+ // negative, because B turns the other way.
254
+ const world = createPhysics2DWorld(0, 0);
255
+ registerBuiltInPhysics2DJointSolvers(world);
256
+ const driver = ball(world, 'dynamic', 0, 0);
257
+ const driven = ball(world, 'dynamic', 5, 0);
258
+ const joint = Object.assign(base(Physics2DGearJointKind, driver.index, driven.index), {
259
+ coordinateA: 'angular',
260
+ coordinateB: 'angular',
261
+ axisAX: 1,
262
+ axisAY: 0,
263
+ axisBX: 1,
264
+ axisBY: 0,
265
+ ratio: 1,
266
+ constant: 0,
267
+ });
268
+ addPhysics2DJoint(world, joint);
269
+ for (let step = 0; step < 200; step++) {
270
+ driver.torque = 10;
271
+ stepPhysics2D(world, DT);
272
+ }
273
+
274
+ const out = createPhysics2DJointReaction();
275
+ expect(writePhysics2DJointReaction(world, joint, DT, out)).toBe(true);
276
+ expect(out.torque).toBeCloseTo(-5, 6);
277
+ expect(out.forceX).toBeCloseTo(0, 9);
278
+ expect(out.forceY).toBeCloseTo(0, 9);
279
+ expect(driven.angularVelocity).toBeCloseTo(-driver.angularVelocity, 6);
280
+ });
281
+
282
+ it('reports nothing for a rope that is slack, because a slack rope carries no load', () => {
283
+ const world = settledWorld();
284
+ const anchor = ball(world, 'static', 0, 0);
285
+ const bob = ball(world, 'dynamic', 0, -1);
286
+ // Rope longer than the separation and a floor to land on, so it never goes taut.
287
+ const floor = createRigidBody2D('static', 0, -2);
288
+ floor.colliders.push(createPhysics2DCollider({ kind: 'aabb', minX: -5, minY: -1, maxX: 5, maxY: 0 }, MATERIAL));
289
+ addPhysics2DBody(world, floor);
290
+ const reaction = settle(
291
+ world,
292
+ Object.assign(base(Physics2DRopeJointKind, anchor.index, bob.index), { maxLength: 8 }),
293
+ );
294
+
295
+ // Closeness rather than identity only because a zero impulse times a negative axis component is
296
+ // negative zero, which is the same force and a different value.
297
+ expect(reaction.forceX).toBeCloseTo(0, 12);
298
+ expect(reaction.forceY).toBeCloseTo(0, 12);
299
+ expect(reaction.torque).toBeCloseTo(0, 12);
300
+ });
301
+
302
+ it('declines before the first step, when there is nothing yet to report', () => {
303
+ const world = settledWorld();
304
+ const anchor = ball(world, 'static', 0, 0);
305
+ const bob = ball(world, 'dynamic', 0, -2);
306
+ const joint = Object.assign(base(Physics2DDistanceJointKind, anchor.index, bob.index), {
307
+ length: 2,
308
+ frequencyHz: 0,
309
+ dampingRatio: 0,
310
+ });
311
+ addPhysics2DJoint(world, joint);
312
+
313
+ const out = createPhysics2DJointReaction();
314
+ expect(writePhysics2DJointReaction(world, joint, DT, out)).toBe(false);
315
+ expect(out).toEqual({ forceX: 0, forceY: 0, torque: 0 });
316
+ });
317
+
318
+ it('declines for a kind whose solver was never registered', () => {
319
+ const world = createPhysics2DWorld(0, GRAVITY);
320
+ const anchor = ball(world, 'static', 0, 0);
321
+ const bob = ball(world, 'dynamic', 0, -2);
322
+ const joint = base('acme.Unregistered', anchor.index, bob.index);
323
+ addPhysics2DJoint(world, joint);
324
+ stepPhysics2D(world, DT);
325
+
326
+ const out = createPhysics2DJointReaction();
327
+ expect(writePhysics2DJointReaction(world, joint, DT, out)).toBe(false);
328
+ });
329
+
330
+ it('declines for a timestep that cannot express a force, and zeroes the output first', () => {
331
+ // The conversion is a division by dt, so a zero or non-finite step has no answer rather than an
332
+ // infinite one. `out` is cleared before the check so a caller reusing a scratch reaction cannot read
333
+ // the previous joint's numbers back as this one's.
334
+ const world = settledWorld();
335
+ const anchor = ball(world, 'static', 0, 0);
336
+ const bob = ball(world, 'dynamic', 0, -2);
337
+ const joint = Object.assign(base(Physics2DDistanceJointKind, anchor.index, bob.index), {
338
+ length: 2,
339
+ frequencyHz: 0,
340
+ dampingRatio: 0,
341
+ });
342
+ const out = settle(world, joint, 200);
343
+ expect(out.forceY).toBeGreaterThan(0);
344
+
345
+ for (const bad of [0, -1, Number.NaN, Number.POSITIVE_INFINITY]) {
346
+ expect(writePhysics2DJointReaction(world, joint, bad, out)).toBe(false);
347
+ expect(out).toEqual({ forceX: 0, forceY: 0, torque: 0 });
348
+ }
349
+ });
350
+
351
+ it('scales with the timestep it is told, since a force is an impulse per unit time', () => {
352
+ const world = settledWorld();
353
+ const anchor = ball(world, 'static', 0, 0);
354
+ const bob = ball(world, 'dynamic', 0, -2);
355
+ const joint = Object.assign(base(Physics2DDistanceJointKind, anchor.index, bob.index), {
356
+ length: 2,
357
+ frequencyHz: 0,
358
+ dampingRatio: 0,
359
+ });
360
+ const atStepSize = settle(world, joint);
361
+
362
+ const halved = createPhysics2DJointReaction();
363
+ writePhysics2DJointReaction(world, joint, DT * 2, halved);
364
+ expect(halved.forceY).toBeCloseTo(atStepSize.forceY / 2, 9);
365
+ });
366
+ });
@@ -35,6 +35,8 @@ function joint(kind: string, bodyA: number, bodyB: number, anchorAX = 0, anchorB
35
35
  localAnchorBX: anchorBX,
36
36
  localAnchorBY: 0,
37
37
  collideConnected: false,
38
+ breakForce: Number.POSITIVE_INFINITY,
39
+ breakTorque: Number.POSITIVE_INFINITY,
38
40
  impulse0: 0,
39
41
  impulse1: 0,
40
42
  impulse2: 0,
@@ -0,0 +1,99 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { writePhysics2DSoftRowParameters } from './jointRows';
4
+
5
+ const TAU = 2 * Math.PI;
6
+
7
+ // What a correct derivation must produce, written independently of the implementation from the two
8
+ // definitions it is built on: critical damping is `2 * m * zeta * omega`, spring rate is `m * omega^2`.
9
+ function expectedSoftRow(mass: number, frequencyHz: number, dampingRatio: number, dt: number): number[] {
10
+ const omega = TAU * frequencyHz;
11
+ const damping = 2 * mass * dampingRatio * omega;
12
+ const stiffness = mass * omega * omega;
13
+ const gamma = 1 / (dt * (damping + dt * stiffness));
14
+ return [1 / (1 / mass + gamma), dt * stiffness * gamma, gamma];
15
+ }
16
+
17
+ describe('writePhysics2DSoftRowParameters', () => {
18
+ it('returns the hard parameters unchanged when no frequency was authored', () => {
19
+ const out = [9, 9, 9];
20
+ writePhysics2DSoftRowParameters(4, 0, 0.7, 1 / 60, 12, out);
21
+ expect(out).toEqual([4, 12, 0]);
22
+ });
23
+
24
+ it('returns the hard parameters unchanged for a negative or NaN frequency', () => {
25
+ const out = [9, 9, 9];
26
+ writePhysics2DSoftRowParameters(4, -3, 0.7, 1 / 60, 12, out);
27
+ expect(out).toEqual([4, 12, 0]);
28
+ writePhysics2DSoftRowParameters(4, Number.NaN, 0.7, 1 / 60, 12, out);
29
+ expect(out).toEqual([4, 12, 0]);
30
+ });
31
+
32
+ it('returns the hard parameters unchanged for a non-positive timestep', () => {
33
+ // A step of no duration cannot express a rate, so the spring degrades to the stop it replaced
34
+ // rather than to a row that silently does nothing.
35
+ const out = [9, 9, 9];
36
+ writePhysics2DSoftRowParameters(4, 2, 0.7, 0, 12, out);
37
+ expect(out).toEqual([4, 12, 0]);
38
+ });
39
+
40
+ it('matches the stiffness-and-damping derivation it stands for', () => {
41
+ const out = [0, 0, 0];
42
+ writePhysics2DSoftRowParameters(2.5, 3, 0.4, 1 / 120, 0, out);
43
+ const expected = expectedSoftRow(2.5, 3, 0.4, 1 / 120);
44
+ expect(out[0]).toBeCloseTo(expected[0], 12);
45
+ expect(out[1]).toBeCloseTo(expected[1], 12);
46
+ expect(out[2]).toBeCloseTo(expected[2], 12);
47
+ });
48
+
49
+ it('produces a bias factor that does not depend on the mass being moved', () => {
50
+ // The defect this function exists to prevent is invisible here and only here: mass cancels out of
51
+ // the bias factor entirely, so an implementation fed an INVERSE mass by mistake still authors the
52
+ // right-looking frequency and gets the damping and the softness wrong instead.
53
+ const light = [0, 0, 0];
54
+ const heavy = [0, 0, 0];
55
+ writePhysics2DSoftRowParameters(0.25, 2, 0.3, 1 / 60, 0, light);
56
+ writePhysics2DSoftRowParameters(400, 2, 0.3, 1 / 60, 0, heavy);
57
+ expect(light[1]).toBeCloseTo(heavy[1], 12);
58
+
59
+ // The closed form the two must agree on, with no mass in it at all.
60
+ const omega = TAU * 2;
61
+ expect(light[1]).toBeCloseTo((omega * omega) / (2 * 0.3 * omega + (omega * omega) / 60), 12);
62
+ });
63
+
64
+ it('scales gamma and the softened mass with the mass, which the bias factor hides', () => {
65
+ // The other two outputs are where a mass-versus-inverse-mass mix-up actually shows, and they move
66
+ // in opposite directions: compliance falls as mass rises, softened mass rises with it.
67
+ const light = [0, 0, 0];
68
+ const heavy = [0, 0, 0];
69
+ writePhysics2DSoftRowParameters(1, 2, 0.3, 1 / 60, 0, light);
70
+ writePhysics2DSoftRowParameters(4, 2, 0.3, 1 / 60, 0, heavy);
71
+ expect(heavy[2]).toBeCloseTo(light[2] / 4, 12);
72
+ expect(heavy[0]).toBeGreaterThan(light[0]);
73
+ });
74
+
75
+ it('softens rather than stiffens, since compliance adds to the inverse mass', () => {
76
+ const out = [0, 0, 0];
77
+ writePhysics2DSoftRowParameters(5, 1.5, 0.5, 1 / 60, 0, out);
78
+ expect(out[0]).toBeGreaterThan(0);
79
+ expect(out[0]).toBeLessThan(5);
80
+ expect(out[2]).toBeGreaterThan(0);
81
+ });
82
+
83
+ it('yields a fully compliant row for a massless constraint instead of dividing by zero', () => {
84
+ const out = [9, 9, 9];
85
+ writePhysics2DSoftRowParameters(0, 2, 0.3, 1 / 60, 7, out);
86
+ expect(out).toEqual([0, 0, 0]);
87
+ });
88
+
89
+ it('overwrites every slot of a reused out array', () => {
90
+ // Callers keep one scratch triple for every joint prepared in a step, so a slot left behind from
91
+ // the previous joint would be read as this one's.
92
+ const out = [0, 0, 0];
93
+ writePhysics2DSoftRowParameters(3, 4, 0.6, 1 / 60, 0, out);
94
+ const soft = [...out];
95
+ writePhysics2DSoftRowParameters(3, 0, 0.6, 1 / 60, 11, out);
96
+ expect(out).toEqual([3, 11, 0]);
97
+ expect(soft[2]).toBeGreaterThan(0);
98
+ });
99
+ });