@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/src/joints.test.ts
CHANGED
|
@@ -54,6 +54,8 @@ function baseJoint(kind: string, bodyA: number, bodyB: number) {
|
|
|
54
54
|
localAnchorBX: 0,
|
|
55
55
|
localAnchorBY: 0,
|
|
56
56
|
collideConnected: false,
|
|
57
|
+
breakForce: Number.POSITIVE_INFINITY,
|
|
58
|
+
breakTorque: Number.POSITIVE_INFINITY,
|
|
57
59
|
impulse0: 0,
|
|
58
60
|
impulse1: 0,
|
|
59
61
|
impulse2: 0,
|
|
@@ -270,6 +272,8 @@ function jointWithoutAccumulators(kind: string, bodyA: number, bodyB: number, ov
|
|
|
270
272
|
localAnchorBX: 0,
|
|
271
273
|
localAnchorBY: 0,
|
|
272
274
|
collideConnected: false,
|
|
275
|
+
breakForce: Number.POSITIVE_INFINITY,
|
|
276
|
+
breakTorque: Number.POSITIVE_INFINITY,
|
|
273
277
|
rAX: 0,
|
|
274
278
|
rAY: 0,
|
|
275
279
|
rBX: 0,
|
|
@@ -419,6 +423,60 @@ describe('joints built without their impulse accumulators', () => {
|
|
|
419
423
|
});
|
|
420
424
|
|
|
421
425
|
describe('physics2DDistanceJointSolver', () => {
|
|
426
|
+
// A hanging spring whose only dynamic body's mass is set by `density`. Everything else — the authored
|
|
427
|
+
// frequency, the damping ratio, the stretch it starts from — is held identical between instances, so
|
|
428
|
+
// any difference in how it moves is a difference the mass caused.
|
|
429
|
+
function springWorld(density: number): { world: Physics2DWorld; bob: ReturnType<typeof box> } {
|
|
430
|
+
const material = { density, friction: 0, restitution: 0 };
|
|
431
|
+
const world = createPhysics2DWorld(0, 0);
|
|
432
|
+
registerPhysics2DJointSolver(world, Physics2DDistanceJointKind, physics2DDistanceJointSolver);
|
|
433
|
+
const anchor = createRigidBody2D('static', 0, 0);
|
|
434
|
+
anchor.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.1 }, material));
|
|
435
|
+
addPhysics2DBody(world, anchor);
|
|
436
|
+
const bob = createRigidBody2D('dynamic', 0, -2);
|
|
437
|
+
bob.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.5 }, material));
|
|
438
|
+
addPhysics2DBody(world, bob);
|
|
439
|
+
const joint: Physics2DDistanceJoint = {
|
|
440
|
+
...baseJoint(Physics2DDistanceJointKind, anchor.index, bob.index),
|
|
441
|
+
length: 1,
|
|
442
|
+
frequencyHz: 2,
|
|
443
|
+
dampingRatio: 0.2,
|
|
444
|
+
};
|
|
445
|
+
addPhysics2DJoint(world, joint);
|
|
446
|
+
return { world, bob };
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
it('oscillates an authored spring at the same rate whatever mass hangs off it', () => {
|
|
450
|
+
// Authoring a spring by frequency instead of by stiffness is only worth anything if the frequency
|
|
451
|
+
// survives the mass changing — otherwise it is a stiffness under another name, and every tuned
|
|
452
|
+
// spring in a game retunes itself the moment an artist resizes the body.
|
|
453
|
+
const light = springWorld(1);
|
|
454
|
+
const heavy = springWorld(4);
|
|
455
|
+
expect(heavy.bob.mass).toBeCloseTo(light.bob.mass * 4, 12);
|
|
456
|
+
|
|
457
|
+
stepPhysics2D(light.world, 1 / 60);
|
|
458
|
+
stepPhysics2D(heavy.world, 1 / 60);
|
|
459
|
+
// Exactly equal, not close: mass has to cancel out of this completely, not mostly.
|
|
460
|
+
expect(heavy.bob.velocityY).toBe(light.bob.velocityY);
|
|
461
|
+
|
|
462
|
+
// Half a period, measured as the swing back through rest to the far side.
|
|
463
|
+
const halfPeriod = (spring: { world: Physics2DWorld; bob: ReturnType<typeof box> }): number => {
|
|
464
|
+
for (let step = 1; step < 600; step++) {
|
|
465
|
+
stepPhysics2D(spring.world, 1 / 60);
|
|
466
|
+
if (step > 2 && spring.bob.velocityY <= 0) return step;
|
|
467
|
+
}
|
|
468
|
+
return -1;
|
|
469
|
+
};
|
|
470
|
+
const lightHalfPeriod = halfPeriod(light);
|
|
471
|
+
expect(halfPeriod(heavy)).toBe(lightHalfPeriod);
|
|
472
|
+
|
|
473
|
+
// And it is the RIGHT rate, not merely a consistent one: a 2 Hz spring at damping ratio 0.2 has a
|
|
474
|
+
// damped half-period of 1 / (2 * 2 * sqrt(1 - 0.04)) seconds, which is 15.3 steps at 60 Hz. Pinning
|
|
475
|
+
// only the agreement between the two would pass just as well for two identically wrong springs.
|
|
476
|
+
expect(lightHalfPeriod).toBeGreaterThan(14);
|
|
477
|
+
expect(lightHalfPeriod).toBeLessThan(17);
|
|
478
|
+
});
|
|
479
|
+
|
|
422
480
|
it('keeps prepared axis and mass local when a joint getter prepares another world', () => {
|
|
423
481
|
const world = createPhysics2DWorld(0, 0);
|
|
424
482
|
const anchor = box(world, 'static', 0, 0);
|
|
@@ -905,6 +963,98 @@ describe('physics2DMouseJointSolver', () => {
|
|
|
905
963
|
});
|
|
906
964
|
|
|
907
965
|
describe('physics2DPrismaticJointSolver', () => {
|
|
966
|
+
// A vertical rail with a stop below it, so a slider falls onto the stop and how far it sinks past is
|
|
967
|
+
// the compliance. Same shape as the hinge's test, one dimension over.
|
|
968
|
+
const RAIL_LOWER = -1;
|
|
969
|
+
const RAIL_DT = 1 / 480;
|
|
970
|
+
|
|
971
|
+
function limitedSlider(density: number, frequencyHz: number, enableLimitSpring: boolean) {
|
|
972
|
+
const material = { density, friction: 0, restitution: 0 };
|
|
973
|
+
const world = createPhysics2DWorld(0, -10);
|
|
974
|
+
registerPhysics2DJointSolver(world, Physics2DPrismaticJointKind, physics2DPrismaticJointSolver);
|
|
975
|
+
const rail = createRigidBody2D('static', 0, 0);
|
|
976
|
+
rail.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.1 }, material));
|
|
977
|
+
addPhysics2DBody(world, rail);
|
|
978
|
+
const slider = createRigidBody2D('dynamic', 0, -0.5);
|
|
979
|
+
slider.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.3 }, material));
|
|
980
|
+
addPhysics2DBody(world, slider);
|
|
981
|
+
const joint: Physics2DPrismaticJoint = {
|
|
982
|
+
...baseJoint(Physics2DPrismaticJointKind, rail.index, slider.index),
|
|
983
|
+
localAxisAX: 0,
|
|
984
|
+
localAxisAY: 1,
|
|
985
|
+
referenceAngle: 0,
|
|
986
|
+
enableMotor: false,
|
|
987
|
+
motorSpeed: 0,
|
|
988
|
+
maxMotorForce: 0,
|
|
989
|
+
motorImpulse: 0,
|
|
990
|
+
enableLimit: true,
|
|
991
|
+
lowerTranslation: RAIL_LOWER,
|
|
992
|
+
upperTranslation: 1,
|
|
993
|
+
enableLimitSpring,
|
|
994
|
+
limitFrequencyHz: frequencyHz,
|
|
995
|
+
limitDampingRatio: 1,
|
|
996
|
+
};
|
|
997
|
+
addPhysics2DJoint(world, joint);
|
|
998
|
+
for (let step = 0; step < 2400; step++) stepPhysics2D(world, RAIL_DT);
|
|
999
|
+
// Statics again, on the linear axis: weight over a spring rate of `m * (2*pi*f)^2`, so mass cancels
|
|
1000
|
+
// and the sink depth is `g / omega^2`.
|
|
1001
|
+
const predictedSink = 10 / (2 * Math.PI * frequencyHz) ** 2;
|
|
1002
|
+
return { sink: RAIL_LOWER - slider.y, predictedSink, slider };
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
it('arrests the slider at a hard travel stop', () => {
|
|
1006
|
+
expect(limitedSlider(1, 0, false).sink).toBeLessThan(0.02);
|
|
1007
|
+
});
|
|
1008
|
+
|
|
1009
|
+
it('sinks past a compliant travel stop by the depth its authored frequency predicts', () => {
|
|
1010
|
+
for (const frequency of [20, 40]) {
|
|
1011
|
+
const soft = limitedSlider(1, frequency, true);
|
|
1012
|
+
expect(soft.sink, `${String(frequency)} Hz`).toBeCloseTo(soft.predictedSink, 3);
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
it('sinks the same depth whatever the slider weighs', () => {
|
|
1017
|
+
const light = limitedSlider(1, 20, true);
|
|
1018
|
+
const heavy = limitedSlider(4, 20, true);
|
|
1019
|
+
expect(heavy.slider.mass).toBeCloseTo(light.slider.mass * 4, 9);
|
|
1020
|
+
expect(heavy.sink).toBe(light.sink);
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
it('leaves a pinned rail hard, because a zero-width range has no side to yield toward', () => {
|
|
1024
|
+
// Coincident bounds make the axis row a two-sided equality rather than a stop, and a spring there
|
|
1025
|
+
// would be a rest length on a coordinate that is not allowed to move at all.
|
|
1026
|
+
const material = { density: 1, friction: 0, restitution: 0 };
|
|
1027
|
+
const world = createPhysics2DWorld(0, -10);
|
|
1028
|
+
registerPhysics2DJointSolver(world, Physics2DPrismaticJointKind, physics2DPrismaticJointSolver);
|
|
1029
|
+
const rail = createRigidBody2D('static', 0, 0);
|
|
1030
|
+
rail.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.1 }, material));
|
|
1031
|
+
addPhysics2DBody(world, rail);
|
|
1032
|
+
const slider = createRigidBody2D('dynamic', 0, -0.5);
|
|
1033
|
+
slider.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.3 }, material));
|
|
1034
|
+
addPhysics2DBody(world, slider);
|
|
1035
|
+
const pinned: Physics2DPrismaticJoint = {
|
|
1036
|
+
...baseJoint(Physics2DPrismaticJointKind, rail.index, slider.index),
|
|
1037
|
+
localAxisAX: 0,
|
|
1038
|
+
localAxisAY: 1,
|
|
1039
|
+
referenceAngle: 0,
|
|
1040
|
+
enableMotor: false,
|
|
1041
|
+
motorSpeed: 0,
|
|
1042
|
+
maxMotorForce: 0,
|
|
1043
|
+
motorImpulse: 0,
|
|
1044
|
+
enableLimit: true,
|
|
1045
|
+
lowerTranslation: -0.5,
|
|
1046
|
+
upperTranslation: -0.5,
|
|
1047
|
+
enableLimitSpring: true,
|
|
1048
|
+
limitFrequencyHz: 20,
|
|
1049
|
+
limitDampingRatio: 1,
|
|
1050
|
+
};
|
|
1051
|
+
addPhysics2DJoint(world, pinned);
|
|
1052
|
+
|
|
1053
|
+
for (let step = 0; step < 1200; step++) stepPhysics2D(world, RAIL_DT);
|
|
1054
|
+
|
|
1055
|
+
expect(slider.y).toBeCloseTo(-0.5, 3);
|
|
1056
|
+
});
|
|
1057
|
+
|
|
908
1058
|
it('allows motion along its axis while removing perpendicular motion and relative rotation', () => {
|
|
909
1059
|
const world = createPhysics2DWorld(0, 0);
|
|
910
1060
|
registerPhysics2DJointSolver(world, Physics2DPrismaticJointKind, physics2DPrismaticJointSolver);
|
|
@@ -1126,6 +1276,93 @@ describe('physics2DPulleyJointSolver', () => {
|
|
|
1126
1276
|
});
|
|
1127
1277
|
|
|
1128
1278
|
describe('physics2DRevoluteJointSolver', () => {
|
|
1279
|
+
// A hinged arm whose mass hangs 1.5 to the side, limited below at LIMIT_ANGLE, so gravity drives it
|
|
1280
|
+
// into the lower stop and how far past it settles is the compliance under test. The frequencies are
|
|
1281
|
+
// deliberately stiff relative to this load: a spring soft enough to be overwhelmed does not settle at
|
|
1282
|
+
// all, and measuring one at an arbitrary moment reads a swinging pendulum as a sag.
|
|
1283
|
+
const LIMIT_ANGLE = -0.05;
|
|
1284
|
+
const LIMIT_DT = 1 / 480;
|
|
1285
|
+
|
|
1286
|
+
function limitedArm(density: number, frequencyHz: number, enableLimitSpring: boolean) {
|
|
1287
|
+
const material = { density, friction: 0, restitution: 0 };
|
|
1288
|
+
const world = createPhysics2DWorld(0, -10);
|
|
1289
|
+
registerPhysics2DJointSolver(world, Physics2DRevoluteJointKind, physics2DRevoluteJointSolver);
|
|
1290
|
+
const post = createRigidBody2D('static', 0, 0);
|
|
1291
|
+
post.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 0, y: 0, radius: 0.1 }, material));
|
|
1292
|
+
addPhysics2DBody(world, post);
|
|
1293
|
+
const arm = createRigidBody2D('dynamic', 0, 0);
|
|
1294
|
+
arm.colliders.push(createPhysics2DCollider({ kind: 'circle', x: 1.5, y: 0, radius: 0.3 }, material));
|
|
1295
|
+
addPhysics2DBody(world, arm);
|
|
1296
|
+
const joint: Physics2DRevoluteJoint = {
|
|
1297
|
+
...baseJoint(Physics2DRevoluteJointKind, post.index, arm.index),
|
|
1298
|
+
enableMotor: false,
|
|
1299
|
+
motorSpeed: 0,
|
|
1300
|
+
maxMotorTorque: 0,
|
|
1301
|
+
motorImpulse: 0,
|
|
1302
|
+
enableLimit: true,
|
|
1303
|
+
lowerAngle: LIMIT_ANGLE,
|
|
1304
|
+
upperAngle: 1,
|
|
1305
|
+
referenceAngle: 0,
|
|
1306
|
+
enableLimitSpring,
|
|
1307
|
+
limitFrequencyHz: frequencyHz,
|
|
1308
|
+
limitDampingRatio: 1,
|
|
1309
|
+
};
|
|
1310
|
+
addPhysics2DJoint(world, joint);
|
|
1311
|
+
for (let step = 0; step < 2400; step++) stepPhysics2D(world, LIMIT_DT);
|
|
1312
|
+
// Statics: the arm's weight applies this moment about the hinge, and an angular spring of frequency
|
|
1313
|
+
// `f` resists it with stiffness `I * (2*pi*f)^2`. Mass cancels from the ratio, which is what makes
|
|
1314
|
+
// the authored frequency mean something.
|
|
1315
|
+
const predictedSag = (arm.mass * 10 * 1.5) / (arm.inertia * (2 * Math.PI * frequencyHz) ** 2);
|
|
1316
|
+
return { sag: LIMIT_ANGLE - arm.angle, predictedSag, arm };
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
it('arrests the arm at a hard limit, which is what a joint that sets no spring still gets', () => {
|
|
1320
|
+
const hard = limitedArm(1, 0, false);
|
|
1321
|
+
expect(hard.sag).toBeLessThan(0.001);
|
|
1322
|
+
});
|
|
1323
|
+
|
|
1324
|
+
it('lets a compliant limit sag by the deflection its authored frequency predicts', () => {
|
|
1325
|
+
// Checked against statics rather than against a recorded number, so a change to how the row is
|
|
1326
|
+
// derived has to still describe a spring of the frequency the caller asked for.
|
|
1327
|
+
for (const frequency of [20, 40]) {
|
|
1328
|
+
const soft = limitedArm(1, frequency, true);
|
|
1329
|
+
expect(soft.sag, `${String(frequency)} Hz`).toBeCloseTo(soft.predictedSag, 3);
|
|
1330
|
+
}
|
|
1331
|
+
});
|
|
1332
|
+
|
|
1333
|
+
it('sags four times as far when the limit spring is half the frequency', () => {
|
|
1334
|
+
// Deflection goes as 1 / omega^2, so halving the frequency quadruples it. A row that merely got
|
|
1335
|
+
// "softer with a smaller number" would pass a monotonicity check and fail this one.
|
|
1336
|
+
const stiff = limitedArm(1, 40, true);
|
|
1337
|
+
const soft = limitedArm(1, 20, true);
|
|
1338
|
+
expect(soft.sag / stiff.sag).toBeCloseTo(4, 1);
|
|
1339
|
+
});
|
|
1340
|
+
|
|
1341
|
+
it('sags the same distance whatever the arm weighs', () => {
|
|
1342
|
+
// The point of authoring a stop by frequency. Both the driving moment and the stiffness scale with
|
|
1343
|
+
// mass, so it cancels — and it must cancel EXACTLY, not approximately.
|
|
1344
|
+
const light = limitedArm(1, 20, true);
|
|
1345
|
+
const heavy = limitedArm(4, 20, true);
|
|
1346
|
+
expect(heavy.arm.mass).toBeCloseTo(light.arm.mass * 4, 9);
|
|
1347
|
+
expect(heavy.sag).toBe(light.sag);
|
|
1348
|
+
});
|
|
1349
|
+
|
|
1350
|
+
it('stays one-sided when softened, and never pulls the arm back inside the range', () => {
|
|
1351
|
+
// Softening changes how hard a stop resists being crossed, never whether it may pull. A row that
|
|
1352
|
+
// gained a rest length would hoist the arm back above the limit it is resting against.
|
|
1353
|
+
const soft = limitedArm(1, 20, true);
|
|
1354
|
+
expect(soft.sag).toBeGreaterThan(0);
|
|
1355
|
+
expect(soft.arm.angle).toBeLessThan(LIMIT_ANGLE);
|
|
1356
|
+
});
|
|
1357
|
+
|
|
1358
|
+
it('behaves exactly as a hard stop when the spring is enabled with no frequency', () => {
|
|
1359
|
+
// The degenerate authoring case — a caller flips the flag and forgets the number. It must degrade to
|
|
1360
|
+
// the stop it replaced rather than to a constraint that does nothing.
|
|
1361
|
+
const hard = limitedArm(1, 0, false);
|
|
1362
|
+
const frequencyless = limitedArm(1, 0, true);
|
|
1363
|
+
expect(frequencyless.arm.angle).toBe(hard.arm.angle);
|
|
1364
|
+
});
|
|
1365
|
+
|
|
1129
1366
|
it('keeps the pinned anchors together while leaving rotation free', () => {
|
|
1130
1367
|
const world = createPhysics2DWorld();
|
|
1131
1368
|
registerPhysics2DJointSolver(world, Physics2DRevoluteJointKind, physics2DRevoluteJointSolver);
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
CollisionBuiltInShape2D,
|
|
3
|
+
Physics2DCollider,
|
|
4
|
+
Physics2DMassData,
|
|
5
|
+
RigidBody2D,
|
|
6
|
+
} from '@flighthq/types/contract';
|
|
2
7
|
import { describe, expect, it } from 'vitest';
|
|
3
8
|
|
|
4
9
|
import { computePhysics2DColliderMassData, updateRigidBody2DMassData } from './massProperties';
|
|
5
10
|
import { createPhysics2DCollider, createRigidBody2D } from './world';
|
|
6
11
|
|
|
7
|
-
function collider(local:
|
|
12
|
+
function collider(local: CollisionBuiltInShape2D, density = 1): Physics2DCollider {
|
|
8
13
|
return createPhysics2DCollider(local, { density, friction: 0.2, restitution: 0 });
|
|
9
14
|
}
|
|
10
15
|
|
|
@@ -21,7 +26,82 @@ function massData(): Physics2DMassData {
|
|
|
21
26
|
return { mass: 0, inertia: 0, centerX: 0, centerY: 0 };
|
|
22
27
|
}
|
|
23
28
|
|
|
29
|
+
// Grid integration over the capsule's own containment predicate. It shares no algebra with the closed
|
|
30
|
+
// form, so agreement between the two is evidence rather than restatement — and the capsule's inertia is
|
|
31
|
+
// exactly the kind of derivation where a dropped term reads as plausible.
|
|
32
|
+
function integrateCapsule(x0: number, y0: number, x1: number, y1: number, radius: number) {
|
|
33
|
+
const CELLS = 1200;
|
|
34
|
+
const minX = Math.min(x0, x1) - radius;
|
|
35
|
+
const minY = Math.min(y0, y1) - radius;
|
|
36
|
+
const stepX = (Math.max(x0, x1) + radius - minX) / CELLS;
|
|
37
|
+
const stepY = (Math.max(y0, y1) + radius - minY) / CELLS;
|
|
38
|
+
const cellArea = stepX * stepY;
|
|
39
|
+
const inside: number[] = [];
|
|
40
|
+
let area = 0;
|
|
41
|
+
let sumX = 0;
|
|
42
|
+
let sumY = 0;
|
|
43
|
+
for (let ix = 0; ix < CELLS; ix++) {
|
|
44
|
+
const px = minX + (ix + 0.5) * stepX;
|
|
45
|
+
for (let iy = 0; iy < CELLS; iy++) {
|
|
46
|
+
const py = minY + (iy + 0.5) * stepY;
|
|
47
|
+
const dx = x1 - x0;
|
|
48
|
+
const dy = y1 - y0;
|
|
49
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
50
|
+
let t = 0;
|
|
51
|
+
if (lengthSquared > 0) {
|
|
52
|
+
t = ((px - x0) * dx + (py - y0) * dy) / lengthSquared;
|
|
53
|
+
t = t < 0 ? 0 : t > 1 ? 1 : t;
|
|
54
|
+
}
|
|
55
|
+
const cx = px - (x0 + t * dx);
|
|
56
|
+
const cy = py - (y0 + t * dy);
|
|
57
|
+
if (cx * cx + cy * cy > radius * radius) continue;
|
|
58
|
+
inside.push(px, py);
|
|
59
|
+
area += cellArea;
|
|
60
|
+
sumX += px * cellArea;
|
|
61
|
+
sumY += py * cellArea;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const centerX = sumX / area;
|
|
65
|
+
const centerY = sumY / area;
|
|
66
|
+
let second = 0;
|
|
67
|
+
for (let at = 0; at < inside.length; at += 2) {
|
|
68
|
+
const rx = inside[at] - centerX;
|
|
69
|
+
const ry = inside[at + 1] - centerY;
|
|
70
|
+
second += (rx * rx + ry * ry) * cellArea;
|
|
71
|
+
}
|
|
72
|
+
return { area, centerX, centerY, second };
|
|
73
|
+
}
|
|
74
|
+
|
|
24
75
|
describe('computePhysics2DColliderMassData', () => {
|
|
76
|
+
it('derives a capsule to match a numerical integration of its own area', () => {
|
|
77
|
+
// Four capsules covering the cases the closed form could get wrong in different ways: axis-aligned,
|
|
78
|
+
// degenerate to a disc, diagonal, and long-and-thin where the end-cap cross term dominates.
|
|
79
|
+
for (const [x0, y0, x1, y1, radius] of [
|
|
80
|
+
[-1, 0, 1, 0, 0.5],
|
|
81
|
+
[0, 0, 0, 0, 0.7],
|
|
82
|
+
[-2, -1, 3, 2, 0.4],
|
|
83
|
+
[0, -3, 0, 3, 1.2],
|
|
84
|
+
]) {
|
|
85
|
+
const out = massData();
|
|
86
|
+
computePhysics2DColliderMassData(collider({ kind: 'capsule', x0, y0, x1, y1, radius }), out);
|
|
87
|
+
const truth = integrateCapsule(x0, y0, x1, y1, radius);
|
|
88
|
+
const label = `capsule ${String(x0)},${String(y0)}-${String(x1)},${String(y1)} r${String(radius)}`;
|
|
89
|
+
// A tenth of a percent, which is the grid's own resolution rather than a tolerance for the formula.
|
|
90
|
+
expect(Math.abs(out.mass - truth.area) / truth.area, label).toBeLessThan(1e-3);
|
|
91
|
+
expect(Math.abs(out.inertia - truth.second) / truth.second, label).toBeLessThan(1e-3);
|
|
92
|
+
expect(out.centerX, label).toBeCloseTo(truth.centerX, 3);
|
|
93
|
+
expect(out.centerY, label).toBeCloseTo(truth.centerY, 3);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('reduces a zero-length capsule to the disc it is', () => {
|
|
98
|
+
const capsule = massData();
|
|
99
|
+
const disc = massData();
|
|
100
|
+
computePhysics2DColliderMassData(collider({ kind: 'capsule', x0: 2, y0: 5, x1: 2, y1: 5, radius: 0.9 }), capsule);
|
|
101
|
+
computePhysics2DColliderMassData(collider({ kind: 'circle', x: 2, y: 5, radius: 0.9 }), disc);
|
|
102
|
+
expect(capsule).toEqual(disc);
|
|
103
|
+
});
|
|
104
|
+
|
|
25
105
|
it('derives a disc from its area and half its mass-radius-squared', () => {
|
|
26
106
|
const out = massData();
|
|
27
107
|
computePhysics2DColliderMassData(collider({ kind: 'circle', x: 3, y: -2, radius: 2 }, 4), out);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createDisplayObject } from '@flighthq/scene2d/contract';
|
|
2
|
+
import type { RigidBody2D } from '@flighthq/types/contract';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { syncPhysics2DBodyToNode2D } from './nodeSync';
|
|
6
|
+
|
|
7
|
+
function stubBody(x: number, y: number, angle: number): RigidBody2D {
|
|
8
|
+
return { x, y, angle } as RigidBody2D;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe('syncPhysics2DBodyToNode2D', () => {
|
|
12
|
+
it('copies position from body to node', () => {
|
|
13
|
+
const body = stubBody(5, -3, 0);
|
|
14
|
+
const node = createDisplayObject();
|
|
15
|
+
syncPhysics2DBodyToNode2D(body, node);
|
|
16
|
+
expect(node.x).toBe(5);
|
|
17
|
+
expect(node.y).toBe(-3);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('converts angle from radians to degrees', () => {
|
|
21
|
+
const body = stubBody(0, 0, Math.PI);
|
|
22
|
+
const node = createDisplayObject();
|
|
23
|
+
syncPhysics2DBodyToNode2D(body, node);
|
|
24
|
+
expect(node.rotation).toBeCloseTo(180, 10);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('converts a quarter turn', () => {
|
|
28
|
+
const body = stubBody(0, 0, Math.PI / 2);
|
|
29
|
+
const node = createDisplayObject();
|
|
30
|
+
syncPhysics2DBodyToNode2D(body, node);
|
|
31
|
+
expect(node.rotation).toBeCloseTo(90, 10);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('handles zero angle', () => {
|
|
35
|
+
const body = stubBody(1, 2, 0);
|
|
36
|
+
const node = createDisplayObject();
|
|
37
|
+
node.rotation = 45;
|
|
38
|
+
syncPhysics2DBodyToNode2D(body, node);
|
|
39
|
+
expect(node.rotation).toBe(0);
|
|
40
|
+
});
|
|
41
|
+
});
|