@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,277 @@
1
+ import {
2
+ addLogSink,
3
+ clearLogOnceKeys,
4
+ createMemoryLogSink,
5
+ getMemoryLogSinkEntries,
6
+ removeLogSink,
7
+ } from '@flighthq/log/contract';
8
+ import type { LogEntry, Physics2DDistanceJoint, Physics2DWorld, RigidBody2D } from '@flighthq/types/contract';
9
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
10
+
11
+ import { arePhysics2DGuardsEnabled, disablePhysics2DGuards, enablePhysics2DGuards } from './enablePhysics2DGuards';
12
+ import { addPhysics2DJoint } from './jointRegistry';
13
+ import { Physics2DDistanceJointKind } from './joints';
14
+ import { registerBuiltInPhysics2DJointSolvers } from './registerBuiltInPhysics2DJointSolvers';
15
+ import { stepPhysics2D } from './step';
16
+ import { addPhysics2DBody, createPhysics2DCollider, createPhysics2DWorld, createRigidBody2D } from './world';
17
+
18
+ const STONE = { density: 1, friction: 0.3, restitution: 0 };
19
+
20
+ function captureLog(run: () => void): readonly LogEntry[] {
21
+ const sink = createMemoryLogSink(8);
22
+ addLogSink(sink.sink);
23
+ try {
24
+ run();
25
+ return getMemoryLogSinkEntries(sink);
26
+ } finally {
27
+ removeLogSink(sink.sink);
28
+ }
29
+ }
30
+
31
+ function ball(world: Physics2DWorld, type: RigidBody2D['type'], x: number, y: number): RigidBody2D {
32
+ const body = createRigidBody2D(type, x, y);
33
+ body.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.5 }, STONE));
34
+ return addPhysics2DBody(world, body);
35
+ }
36
+
37
+ // A world with one distance joint and NO solver registered for it — the exact shape of the mistake the
38
+ // joint guard exists for, since registration is opt-in so that unused solvers tree-shake away.
39
+ function unregisteredJointWorld(): Physics2DWorld {
40
+ const world = createPhysics2DWorld(0, -10);
41
+ const anchor = ball(world, 'static', 0, 0);
42
+ const bob = ball(world, 'dynamic', 0, -3);
43
+ const joint: Physics2DDistanceJoint = {
44
+ kind: Physics2DDistanceJointKind,
45
+ bodyA: anchor.index,
46
+ bodyB: bob.index,
47
+ localAnchorAX: 0,
48
+ localAnchorAY: 0,
49
+ localAnchorBX: 0,
50
+ localAnchorBY: 0,
51
+ collideConnected: false,
52
+ breakForce: Number.POSITIVE_INFINITY,
53
+ breakTorque: Number.POSITIVE_INFINITY,
54
+ impulse0: 0,
55
+ impulse1: 0,
56
+ impulse2: 0,
57
+ rAX: 0,
58
+ rAY: 0,
59
+ rBX: 0,
60
+ rBY: 0,
61
+ length: 3,
62
+ frequencyHz: 0,
63
+ dampingRatio: 0,
64
+ };
65
+ addPhysics2DJoint(world, joint);
66
+ return world;
67
+ }
68
+
69
+ beforeEach(() => {
70
+ // `logOnce` keys are process-global and this file deliberately provokes the same faults repeatedly.
71
+ clearLogOnceKeys();
72
+ });
73
+
74
+ afterEach(() => {
75
+ disablePhysics2DGuards();
76
+ });
77
+
78
+ describe('arePhysics2DGuardsEnabled', () => {
79
+ it('reports the current module guard state', () => {
80
+ expect(arePhysics2DGuardsEnabled()).toBe(false);
81
+ enablePhysics2DGuards();
82
+ expect(arePhysics2DGuardsEnabled()).toBe(true);
83
+ disablePhysics2DGuards();
84
+ expect(arePhysics2DGuardsEnabled()).toBe(false);
85
+ });
86
+ });
87
+
88
+ describe('disablePhysics2DGuards', () => {
89
+ it('removes both seams so a declined step and an unsolved joint are silent again', () => {
90
+ enablePhysics2DGuards();
91
+ disablePhysics2DGuards();
92
+ const declining = createPhysics2DWorld(0, -10);
93
+ declining.config.positionIterations = -3;
94
+
95
+ const entries = captureLog(() => {
96
+ stepPhysics2D(declining, 1 / 60);
97
+ stepPhysics2D(unregisteredJointWorld(), 1 / 60);
98
+ });
99
+
100
+ expect(entries).toHaveLength(0);
101
+ });
102
+ });
103
+
104
+ describe('enablePhysics2DGuards', () => {
105
+ it('reports an area-less collider used as a body shape', () => {
106
+ // The third seam. A segment on a rigid body is a modelling mistake the simulation cannot show you:
107
+ // nothing fails, nothing is slow, the body simply never collides with anything.
108
+ enablePhysics2DGuards();
109
+ const world = createPhysics2DWorld(0, -10);
110
+ const body = createRigidBody2D('dynamic', 0, 0);
111
+ body.colliders.push(createPhysics2DCollider({ kind: 'segment', x0: 0, y0: 0, x1: 1, y1: 0 }, STONE));
112
+ addPhysics2DBody(world, body);
113
+
114
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
115
+
116
+ expect(entries).toHaveLength(1);
117
+ const data = entries[0].data as { kinds: string[]; message: string; status: string };
118
+ expect(data.status).toBe('missing-contact-support');
119
+ expect(data.kinds).toEqual(['segment']);
120
+ expect(data.message).toContain('no area');
121
+ });
122
+
123
+ it('says nothing about a capsule, which the dispatcher answers for', () => {
124
+ // This test is the inverse of one that used to assert the opposite. The capsule was named by this
125
+ // guard while its pair functions were missing and stopped being named when they landed, which is the
126
+ // seam working in both directions rather than a list someone edits by hand.
127
+ enablePhysics2DGuards();
128
+ const world = createPhysics2DWorld(0, -10);
129
+ const body = createRigidBody2D('dynamic', 0, 0);
130
+ body.colliders.push(createPhysics2DCollider({ kind: 'capsule', x0: 0, y0: 0, x1: 1, y1: 0, radius: 0.4 }, STONE));
131
+ addPhysics2DBody(world, body);
132
+
133
+ expect(captureLog(() => stepPhysics2D(world, 1 / 60))).toHaveLength(0);
134
+ });
135
+
136
+ it('says nothing about colliders the dispatcher does answer for', () => {
137
+ enablePhysics2DGuards();
138
+ const world = createPhysics2DWorld(0, -10);
139
+ const body = createRigidBody2D('dynamic', 0, 0);
140
+ body.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.5 }, STONE));
141
+ addPhysics2DBody(world, body);
142
+
143
+ expect(captureLog(() => stepPhysics2D(world, 1 / 60))).toHaveLength(0);
144
+ });
145
+
146
+ it('says nothing while a healthy world steps', () => {
147
+ enablePhysics2DGuards();
148
+ const world = createPhysics2DWorld(0, -10);
149
+ ball(world, 'static', 0, 0);
150
+ ball(world, 'dynamic', 0, 2);
151
+
152
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
153
+
154
+ // A guard that fired on a healthy step would be worse than none: the message is supposed to mean
155
+ // something has stopped working.
156
+ expect(entries).toHaveLength(0);
157
+ });
158
+
159
+ it('says nothing about joints once their solvers are registered', () => {
160
+ enablePhysics2DGuards();
161
+ const world = unregisteredJointWorld();
162
+ registerBuiltInPhysics2DJointSolvers(world);
163
+
164
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
165
+
166
+ expect(entries).toHaveLength(0);
167
+ });
168
+
169
+ it('names every failing precondition at once, not only the first', () => {
170
+ // The step's own condition short-circuits, so surfacing one fault per frame would cost a round trip
171
+ // per fault. The explain seam exists to avoid exactly that and the guard must not reintroduce it.
172
+ enablePhysics2DGuards();
173
+ const world = createPhysics2DWorld(Number.NaN, -10);
174
+ world.config.positionIterations = -3;
175
+
176
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
177
+
178
+ expect(entries).toHaveLength(1);
179
+ const data = entries[0].data as { failing: string[]; status: string };
180
+ expect(data.status).toBe('invalid-step');
181
+ expect(data.failing).toContain('gravityValid');
182
+ expect(data.failing).toContain('positionIterationsValid');
183
+ });
184
+
185
+ it('reports a joint whose kind has no registered solver, on a step that otherwise succeeds', () => {
186
+ // The case the whole second seam is for. The world steps, the bob falls, nothing fails — and the
187
+ // rope it is supposed to be hanging from is simply not there.
188
+ enablePhysics2DGuards();
189
+ const world = unregisteredJointWorld();
190
+
191
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
192
+
193
+ expect(entries).toHaveLength(1);
194
+ const data = entries[0].data as { faults: string[]; status: string };
195
+ expect(data.status).toBe('unresolved-joints');
196
+ expect(data.faults).toEqual(['Distance:solver-unregistered']);
197
+ expect(String(entries[0].data && (entries[0].data as { message: string }).message)).toContain(
198
+ 'registerBuiltInPhysics2DJointSolvers',
199
+ );
200
+ // The step really did run — this is a warning about a world that is otherwise working, which is what
201
+ // makes it hard to notice without the guard.
202
+ expect(world.bodies[1].velocityY).toBeLessThan(0);
203
+ });
204
+
205
+ it('reports a joint whose endpoint body is gone with endpoint advice, not registration advice', () => {
206
+ enablePhysics2DGuards();
207
+ const world = unregisteredJointWorld();
208
+ registerBuiltInPhysics2DJointSolvers(world);
209
+ world.joints[0].bodyB = 9999;
210
+
211
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
212
+
213
+ expect(entries).toHaveLength(1);
214
+ const data = entries[0].data as { faults: string[]; message: string };
215
+ expect(data.faults).toEqual(['Distance:body-b-missing']);
216
+ expect(data.message).toContain('removed with the body');
217
+ expect(data.message).not.toContain('registerBuiltInPhysics2DJointSolvers');
218
+ });
219
+
220
+ it('collapses many identically broken joints into one report', () => {
221
+ // A ragdoll with a dozen unregistered joints of the same kind is ONE mistake. Listing twelve indices
222
+ // would read as twelve, and re-keying per index would defeat logOnce entirely.
223
+ enablePhysics2DGuards();
224
+ const world = unregisteredJointWorld();
225
+ for (let extra = 0; extra < 11; extra++) {
226
+ addPhysics2DJoint(world, { ...world.joints[0], impulse0: 0 });
227
+ }
228
+ expect(world.joints).toHaveLength(12);
229
+
230
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
231
+
232
+ expect(entries).toHaveLength(1);
233
+ expect((entries[0].data as { faults: string[] }).faults).toEqual(['Distance:solver-unregistered']);
234
+ expect((entries[0].data as { message: string }).message).toContain('12 of 12');
235
+ });
236
+
237
+ it('warns once for a repeated fault rather than once per frame', () => {
238
+ enablePhysics2DGuards();
239
+ const world = unregisteredJointWorld();
240
+
241
+ const entries = captureLog(() => {
242
+ for (let step = 0; step < 60; step++) stepPhysics2D(world, 1 / 60);
243
+ });
244
+
245
+ expect(entries).toHaveLength(1);
246
+ });
247
+
248
+ it('still speaks when a second, different fault appears after the first', () => {
249
+ // The counterpart risk of keying on the fault set: a world that develops a new problem must not be
250
+ // silenced by having already reported the old one.
251
+ enablePhysics2DGuards();
252
+ const world = unregisteredJointWorld();
253
+
254
+ const entries = captureLog(() => {
255
+ stepPhysics2D(world, 1 / 60);
256
+ registerBuiltInPhysics2DJointSolvers(world);
257
+ world.joints[0].bodyA = 9999;
258
+ stepPhysics2D(world, 1 / 60);
259
+ });
260
+
261
+ expect(entries).toHaveLength(2);
262
+ expect((entries[1].data as { faults: string[] }).faults).toEqual(['Distance:body-a-missing']);
263
+ });
264
+
265
+ it('does not add the joint complaint on top of a declined step', () => {
266
+ // A declined step advanced nothing at all, so its own message is the whole story. Repeating the joint
267
+ // finding underneath it would bury the reason the world is frozen.
268
+ enablePhysics2DGuards();
269
+ const world = unregisteredJointWorld();
270
+ world.config.velocityIterations = -1;
271
+
272
+ const entries = captureLog(() => stepPhysics2D(world, 1 / 60));
273
+
274
+ expect(entries).toHaveLength(1);
275
+ expect((entries[0].data as { status: string }).status).toBe('invalid-step');
276
+ });
277
+ });
@@ -0,0 +1,61 @@
1
+ import type { Physics2DWorld } from '@flighthq/types/contract';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { updatePhysics2DColliderWorldShape } from './colliderTransform';
5
+ import { explainPhysics2DCollision } from './explainPhysics2DCollision';
6
+ import { addPhysics2DBody, createPhysics2DCollider, createPhysics2DWorld, createRigidBody2D } from './world';
7
+
8
+ const STONE = { density: 1, friction: 0.3, restitution: 0 };
9
+
10
+ function worldWith(...shapes: Parameters<typeof createPhysics2DCollider>[0][]): Physics2DWorld {
11
+ const world = createPhysics2DWorld(0, -10);
12
+ for (const shape of shapes) {
13
+ const body = createRigidBody2D('dynamic', 0, 0);
14
+ const collider = createPhysics2DCollider(shape, STONE);
15
+ body.colliders.push(collider);
16
+ addPhysics2DBody(world, body);
17
+ updatePhysics2DColliderWorldShape(collider, body);
18
+ }
19
+ return world;
20
+ }
21
+
22
+ describe('explainPhysics2DCollision', () => {
23
+ it('reports ready for a world of shapes the dispatcher answers for', () => {
24
+ const world = worldWith(
25
+ { kind: 'circle', x: 0, y: 0, radius: 1 },
26
+ { kind: 'aabb', minX: -1, minY: -1, maxX: 1, maxY: 1 },
27
+ { kind: 'capsule', x0: 0, y0: 0, x1: 1, y1: 0, radius: 0.5 },
28
+ { kind: 'polygon', points: [0, 0, 1, 0, 1, 1] },
29
+ );
30
+ expect(explainPhysics2DCollision(world)).toEqual({ status: 'ready', unsupportedKinds: [] });
31
+ });
32
+
33
+ it('reports ready for an empty world, which has nothing that can fail', () => {
34
+ expect(explainPhysics2DCollision(createPhysics2DWorld(0, -10)).status).toBe('ready');
35
+ });
36
+
37
+ it('reports ready for a capsule, which the dispatcher now answers for', () => {
38
+ // This test previously asserted the opposite, and the change is the point: the capsule was named
39
+ // here while its pair functions were missing, and it stopped being named when they landed. That is
40
+ // the seam doing its job in both directions rather than a list someone has to remember to edit.
41
+ const world = worldWith({ kind: 'capsule', x0: 0, y0: 0, x1: 1, y1: 0, radius: 0.5 });
42
+ expect(explainPhysics2DCollision(world)).toEqual({ status: 'ready', unsupportedKinds: [] });
43
+ });
44
+
45
+ it('names the area-less kinds too, since they are equally invisible to the solver', () => {
46
+ const world = worldWith({ kind: 'segment', x0: 0, y0: 0, x1: 1, y1: 0 }, { kind: 'point', x: 0, y: 0 });
47
+ expect(explainPhysics2DCollision(world).unsupportedKinds).toEqual(['point', 'segment']);
48
+ });
49
+
50
+ it('reports each kind once however many colliders carry it, and in a stable order', () => {
51
+ // A level with four hundred capsules is one mistake. Sorted so the answer does not depend on the
52
+ // order bodies were added, which would make the same world report differently twice.
53
+ const world = worldWith(
54
+ { kind: 'point', x: 0, y: 0 },
55
+ { kind: 'segment', x0: 0, y0: 0, x1: 1, y1: 0 },
56
+ { kind: 'segment', x0: 2, y0: 0, x1: 3, y1: 0 },
57
+ { kind: 'circle', x: 0, y: 0, radius: 1 },
58
+ );
59
+ expect(explainPhysics2DCollision(world).unsupportedKinds).toEqual(['point', 'segment']);
60
+ });
61
+ });
@@ -259,6 +259,8 @@ describe('updatePhysics2DSleep', () => {
259
259
  bodyA: shoved.index,
260
260
  bodyB: settled.index,
261
261
  collideConnected: false,
262
+ breakForce: Number.POSITIVE_INFINITY,
263
+ breakTorque: Number.POSITIVE_INFINITY,
262
264
  } as never);
263
265
 
264
266
  updatePhysics2DSleep(world, world.config.timeToSleep + 0.01);
@@ -282,6 +284,8 @@ describe('updatePhysics2DSleep', () => {
282
284
  bodyA: unused.index,
283
285
  bodyB: driven.index,
284
286
  collideConnected: false,
287
+ breakForce: Number.POSITIVE_INFINITY,
288
+ breakTorque: Number.POSITIVE_INFINITY,
285
289
  } as never);
286
290
 
287
291
  updatePhysics2DSleep(world, world.config.timeToSleep + 0.01);
@@ -23,6 +23,8 @@ describe('createPhysics2DDistanceJoint', () => {
23
23
  localAnchorBX: 0,
24
24
  localAnchorBY: 0,
25
25
  collideConnected: false,
26
+ breakForce: Number.POSITIVE_INFINITY,
27
+ breakTorque: Number.POSITIVE_INFINITY,
26
28
  impulse0: 0,
27
29
  impulse1: 0,
28
30
  impulse2: 0,