@flighthq/physics2d 0.3.0-next.1032.e2b99fc → 0.3.0-next.1162.e90e518

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/src/step.test.ts CHANGED
@@ -2,6 +2,8 @@ import { createUniformGridSpatialBackend } from '@flighthq/spatial/contract';
2
2
  import type { Physics2DWorld, RigidBody2D, SpatialIndexBackend, SpatialPair } from '@flighthq/types/contract';
3
3
  import { describe, expect, it } from 'vitest';
4
4
 
5
+ import { addPhysics2DJoint, registerPhysics2DJointSolver } from './jointRegistry';
6
+ import { physics2DDistanceJointSolver } from './joints';
5
7
  import { stepPhysics2D } from './step';
6
8
  import { addPhysics2DBody, createPhysics2DCollider, createPhysics2DWorld, createRigidBody2D } from './world';
7
9
 
@@ -234,3 +236,138 @@ describe('stepPhysics2D', () => {
234
236
  expect(crate.y).toBe(2);
235
237
  });
236
238
  });
239
+
240
+ describe('stepPhysics2D contact events', () => {
241
+ it('reports a contact beginning and ending, read off the cache transitions', () => {
242
+ const world = createPhysics2DWorld();
243
+ ground(world);
244
+ const crate = box(world, 0, 3);
245
+ runSteps(world, 1);
246
+ expect(world.events.began).toHaveLength(0);
247
+
248
+ // Fall until it lands: the step that creates the contact is the begin.
249
+ let began = 0;
250
+ for (let i = 0; i < 200 && began === 0; i++) {
251
+ stepPhysics2D(world, 1 / 60);
252
+ began += world.events.began.length;
253
+ }
254
+ expect(began).toBe(1);
255
+
256
+ // Teleport it away: the step that drops the contact is the end.
257
+ crate.y = 50;
258
+ crate.velocityY = 0;
259
+ stepPhysics2D(world, 1 / 60);
260
+ expect(world.events.ended).toHaveLength(1);
261
+ expect(world.contacts).toHaveLength(0);
262
+ });
263
+
264
+ it('clears its event buffers each step rather than accumulating', () => {
265
+ const world = createPhysics2DWorld();
266
+ ground(world);
267
+ box(world, 0, 0.4);
268
+ runSteps(world, 5);
269
+ expect(world.events.began).toHaveLength(0);
270
+ expect(world.events.ended).toHaveLength(0);
271
+ });
272
+ });
273
+
274
+ describe('stepPhysics2D with joints', () => {
275
+ const DISTANCE = 'Distance';
276
+
277
+ function jointedWorld(index?: SpatialIndexBackend) {
278
+ const world = createPhysics2DWorld(0, -9.81, index);
279
+ registerPhysics2DJointSolver(world, DISTANCE, physics2DDistanceJointSolver);
280
+ ground(world);
281
+ // Created static, not mutated to static after the fact: mass properties are derived at insertion, so
282
+ // flipping the type afterwards leaves a nonzero inverse mass on a body that never integrates its
283
+ // position — it accumulates velocity forever and drags whatever is jointed to it out of the world.
284
+ const anchorBody = createRigidBody2D('static', 0, 4);
285
+ anchorBody.colliders.push(
286
+ createPhysics2DCollider({ kind: 'aabb', minX: -0.5, minY: -0.5, maxX: 0.5, maxY: 0.5 }, STONE),
287
+ );
288
+ const anchor = addPhysics2DBody(world, anchorBody);
289
+ const left = box(world, -1, 2);
290
+ const right = box(world, 1, 2);
291
+ for (const bob of [left, right]) {
292
+ addPhysics2DJoint(world, {
293
+ kind: DISTANCE,
294
+ bodyA: anchor.index,
295
+ bodyB: bob.index,
296
+ localAnchorAX: 0,
297
+ localAnchorAY: 0,
298
+ localAnchorBX: 0,
299
+ localAnchorBY: 0,
300
+ collideConnected: false,
301
+ impulse0: 0,
302
+ impulse1: 0,
303
+ impulse2: 0,
304
+ rAX: 0,
305
+ rAY: 0,
306
+ rBX: 0,
307
+ rBY: 0,
308
+ length: 2,
309
+ stiffness: 0,
310
+ damping: 0,
311
+ } as never);
312
+ }
313
+ return { left, right, world };
314
+ }
315
+
316
+ it('produces the same trace when the broadphase reports its pairs in the opposite order', () => {
317
+ // OBLIGATION 2 EXTENDED TO P2. Joints share the contact list's iteration loop, so the contact sort has
318
+ // to keep holding once joints are also constraining the same bodies — a scene whose contacts reorder
319
+ // now perturbs the joint solve too.
320
+ const plain = jointedWorld();
321
+ const reversed = jointedWorld(createReversedPairBackend());
322
+ runSteps(plain.world, 120);
323
+ runSteps(reversed.world, 120);
324
+ expect(traceWorld(reversed.world)).toBe(traceWorld(plain.world));
325
+ });
326
+
327
+ it('orders every contact pair by body index with joints present', () => {
328
+ // OBLIGATION 1 EXTENDED TO P2.
329
+ const { world } = jointedWorld(createSwappedPairBackend());
330
+ runSteps(world, 120);
331
+ for (const contact of world.contacts) expect(contact.bodyA).toBeLessThan(contact.bodyB);
332
+ for (const joint of world.joints) expect(joint.bodyA).toBeLessThan(joint.bodyB);
333
+ });
334
+
335
+ it('is bit-for-bit repeatable with joints in the solve list', () => {
336
+ const first = jointedWorld();
337
+ const second = jointedWorld();
338
+ runSteps(first.world, 90);
339
+ runSteps(second.world, 90);
340
+ expect(traceWorld(second.world)).toBe(traceWorld(first.world));
341
+ });
342
+
343
+ it('suppresses the contact between jointed bodies unless the joint asks for it', () => {
344
+ // A jointed pair almost always overlaps at the anchor, and resolving that contact fights the
345
+ // constraint holding them together.
346
+ const world = createPhysics2DWorld(0, 0);
347
+ registerPhysics2DJointSolver(world, DISTANCE, physics2DDistanceJointSolver);
348
+ const a = box(world, 0, 0);
349
+ const b = box(world, 0.2, 0);
350
+ addPhysics2DJoint(world, {
351
+ kind: DISTANCE,
352
+ bodyA: a.index,
353
+ bodyB: b.index,
354
+ localAnchorAX: 0,
355
+ localAnchorAY: 0,
356
+ localAnchorBX: 0,
357
+ localAnchorBY: 0,
358
+ collideConnected: false,
359
+ impulse0: 0,
360
+ impulse1: 0,
361
+ impulse2: 0,
362
+ rAX: 0,
363
+ rAY: 0,
364
+ rBX: 0,
365
+ rBY: 0,
366
+ length: 0.2,
367
+ stiffness: 0,
368
+ damping: 0,
369
+ } as never);
370
+ runSteps(world, 10);
371
+ expect(world.contacts).toHaveLength(0);
372
+ });
373
+ });