@margoengine/physics2d 0.1.0 → 1.0.0
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/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +566 -521
- package/dist/body.d.ts +19 -7
- package/dist/composites.d.ts +161 -0
- package/dist/composites.js +183 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +446 -101
- package/dist/joint.d.ts +129 -2
- package/dist/{kernel-D6zM0ozJ.js → kernel-DkjP8dvA.js} +1 -1
- package/dist/kernel.d.ts +5 -0
- package/dist/physics.d.ts +68 -0
- package/dist/physics2d-scalar.wasm +0 -0
- package/dist/physics2d.wasm +0 -0
- package/dist/shapes.d.ts +7 -0
- package/package.json +64 -60
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
3
|
|
|
4
4
|
import { MargoError as e } from "@margoengine/math/errors";
|
|
@@ -581,8 +581,14 @@ var O = (e, t) => {
|
|
|
581
581
|
if (!Number.isFinite(e)) throw RangeError(`${t} must be a finite number, got ${e}; fix the value.`);
|
|
582
582
|
return e;
|
|
583
583
|
}, k = class {
|
|
584
|
-
constructor(e, t, n, r, i, a) {
|
|
585
|
-
this.owner = e, this.body = t, this.settings = r, this.offset = i, this.turn = a, this.described = null, this.options = null, this.removed = !1, this.enabled = !0, this.savedGroups = 0, this.massProps = null, this.shapes = n;
|
|
584
|
+
constructor(e, t, n, r, i, a, o = 0) {
|
|
585
|
+
this.owner = e, this.body = t, this.settings = r, this.offset = i, this.turn = a, this.described = null, this.options = null, this.removed = !1, this.enabled = !0, this.savedGroups = 0, this.explicitMass = null, this.massProps = null, this.shapes = n, this.uid = o;
|
|
586
|
+
}
|
|
587
|
+
get rawSettings() {
|
|
588
|
+
return this.settings;
|
|
589
|
+
}
|
|
590
|
+
reapplyMass() {
|
|
591
|
+
this.massProps ? this.setMassProperties(this.massProps.mass, this.massProps.centre, this.massProps.inertia) : this.explicitMass !== null && this.setMass(this.explicitMass);
|
|
586
592
|
}
|
|
587
593
|
get handle() {
|
|
588
594
|
return this.shapes[0];
|
|
@@ -608,14 +614,13 @@ var O = (e, t) => {
|
|
|
608
614
|
offset: this.offset,
|
|
609
615
|
rotation: this.turn
|
|
610
616
|
}, i);
|
|
611
|
-
|
|
612
|
-
n.io(a[0].geometry.length).u.set(a[0].geometry, 0), n.ok(n.exports.shape_set_core(r, this.shapes[0], a[0].kind)), this.described = {
|
|
617
|
+
this.shapes.length === 1 && a.length === 1 && a[0].kind <= 4 ? (n.io(a[0].geometry.length).u.set(a[0].geometry, 0), n.ok(n.exports.shape_set_core(r, this.shapes[0], a[0].kind)), this.described = {
|
|
613
618
|
...a[0],
|
|
614
619
|
settings: this.settings
|
|
615
620
|
}, this.options &&= {
|
|
616
621
|
...this.options,
|
|
617
622
|
...t
|
|
618
|
-
};
|
|
623
|
+
}) : this.owner.replaceColliderShapes(this, t, a);
|
|
619
624
|
}
|
|
620
625
|
isSensor() {
|
|
621
626
|
return !!(this.settings[5] & 65536);
|
|
@@ -638,6 +643,15 @@ var O = (e, t) => {
|
|
|
638
643
|
setFriction(e) {
|
|
639
644
|
new Float32Array(this.settings.buffer)[1] = O(e, "friction"), this.push();
|
|
640
645
|
}
|
|
646
|
+
setRollingResistance(e) {
|
|
647
|
+
if (!(e >= 0 && e < Infinity)) throw RangeError(`A collider's rolling resistance is a finite number of 0 or more (got ${e}); pass one.`);
|
|
648
|
+
let { kernel: t, handle: n } = this.owner;
|
|
649
|
+
for (let r of this.shapes) t.ok(t.exports.shape_set_rolling_resistance(n, r, e));
|
|
650
|
+
}
|
|
651
|
+
rollingResistance() {
|
|
652
|
+
let { kernel: e, handle: t } = this.owner;
|
|
653
|
+
return e.ok(e.exports.shape_rolling_resistance(t, this.shapes[0])), e.io(1).f[0];
|
|
654
|
+
}
|
|
641
655
|
setRestitution(e) {
|
|
642
656
|
new Float32Array(this.settings.buffer)[2] = O(e, "restitution"), this.push();
|
|
643
657
|
}
|
|
@@ -654,14 +668,14 @@ var O = (e, t) => {
|
|
|
654
668
|
return this.settings[5] >>> 24 & 7;
|
|
655
669
|
}
|
|
656
670
|
setDensity(e) {
|
|
657
|
-
new Float32Array(this.settings.buffer)[0] = O(e, "density"), this.push(), this.massProps = null;
|
|
671
|
+
new Float32Array(this.settings.buffer)[0] = O(e, "density"), this.push(), this.massProps = null, this.explicitMass = null;
|
|
658
672
|
let { kernel: t, handle: n } = this.owner;
|
|
659
673
|
for (let e of this.shapes) t.ok(t.exports.shape_set_mass(n, e, 0));
|
|
660
674
|
}
|
|
661
675
|
setMass(e) {
|
|
662
676
|
if (this.live(), !(e >= 0) || !Number.isFinite(e)) throw RangeError(`A collider's mass is a finite number of kilograms, 0 or more (got ${e}); pass one.`);
|
|
663
677
|
let { kernel: t, handle: n } = this.owner, r = this.shapes.map((e) => (t.ok(t.exports.shape_mass(n, e)), t.io(5).f[4])), i = r.reduce((e, t) => e + t, 0);
|
|
664
|
-
this.massProps = null, this.shapes.forEach((a, o) => {
|
|
678
|
+
this.massProps = null, this.explicitMass = e, this.shapes.forEach((a, o) => {
|
|
665
679
|
let s = t.io(1);
|
|
666
680
|
s.f[0] = i > 0 ? e * r[o] / i : e / this.shapes.length, t.ok(t.exports.shape_set_mass(n, a, 1));
|
|
667
681
|
});
|
|
@@ -676,7 +690,7 @@ var O = (e, t) => {
|
|
|
676
690
|
]) if (!Number.isFinite(r)) throw RangeError(`A collider's ${i} is a finite number (got ${r}); pass one.`);
|
|
677
691
|
if (e < 0 || n < 0) throw RangeError(`A collider's mass and inertia are 0 or more (got ${e}, ${n}); pass them.`);
|
|
678
692
|
let { kernel: r, handle: i, scale: a } = this.owner, o = Math.cos(this.turn), s = Math.sin(this.turn);
|
|
679
|
-
this.massProps = {
|
|
693
|
+
this.explicitMass = null, this.massProps = {
|
|
680
694
|
mass: e,
|
|
681
695
|
centre: [t[0], t[1]],
|
|
682
696
|
inertia: n
|
|
@@ -1196,7 +1210,7 @@ function N(e, t) {
|
|
|
1196
1210
|
}
|
|
1197
1211
|
//#endregion
|
|
1198
1212
|
//#region src/character.ts
|
|
1199
|
-
var P = 1e-5,
|
|
1213
|
+
var P = 1e-5, ee = 8, F = class {
|
|
1200
1214
|
constructor(e, t, n, r) {
|
|
1201
1215
|
this.physics = e, this.body = t, this.options = n, this.onDispose = r, this.disposed = !1, this.filter = {};
|
|
1202
1216
|
let i = n.up ?? [0, -1], a = Math.hypot(i[0], i[1]);
|
|
@@ -1223,7 +1237,7 @@ var P = 1e-5, F = 8, I = class {
|
|
|
1223
1237
|
if (!Number.isFinite(e[0]) || !Number.isFinite(e[1])) throw TypeError("delta must be two finite numbers [x, y].");
|
|
1224
1238
|
this.filter = this.filterFor(t);
|
|
1225
1239
|
let n = this.physics.scale, r = 1 / n, i = [this.body.position[0] * n, this.body.position[1] * n], a = [i[0], i[1]], o = [e[0] * n, e[1] * n], s = o, c = !1, l = 0, u = [], d = this.up, f = (e, t) => e[0] * t[0] + e[1] * t[1];
|
|
1226
|
-
for (let e = 0; e <
|
|
1240
|
+
for (let e = 0; e < ee; e++) {
|
|
1227
1241
|
let e = Math.hypot(o[0], o[1]);
|
|
1228
1242
|
if (e < P) break;
|
|
1229
1243
|
let t = [o[0] / e, o[1] / e], r = this.cast(a, t, e + this.offset);
|
|
@@ -1306,35 +1320,34 @@ var P = 1e-5, F = 8, I = class {
|
|
|
1306
1320
|
this.disposed || (this.disposed = !0, this.onDispose(this));
|
|
1307
1321
|
}
|
|
1308
1322
|
};
|
|
1309
|
-
|
|
1323
|
+
F.prototype[Symbol.dispose] = F.prototype.dispose;
|
|
1310
1324
|
//#endregion
|
|
1311
1325
|
//#region src/joint.ts
|
|
1312
|
-
var
|
|
1326
|
+
var I = (e, t) => {
|
|
1313
1327
|
if (!Number.isFinite(e)) throw RangeError(`${t} must be a finite number, got ${e}; fix the value.`);
|
|
1314
1328
|
return e;
|
|
1315
1329
|
};
|
|
1316
|
-
function
|
|
1330
|
+
function L(e, t, n) {
|
|
1317
1331
|
let r = e.io(32);
|
|
1318
1332
|
r.u.fill(0, 0, 32);
|
|
1319
1333
|
let i = t.anchorA ?? [0, 0], a = t.anchorB ?? [0, 0];
|
|
1320
1334
|
r.f.set([
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1335
|
+
I(i[0], "anchorA x") * n,
|
|
1336
|
+
I(i[1], "anchorA y") * n,
|
|
1337
|
+
I(a[0], "anchorB x") * n,
|
|
1338
|
+
I(a[1], "anchorB y") * n
|
|
1325
1339
|
], 1);
|
|
1326
|
-
let o = t.limits, s = t.type === "prismatic" ? n : 1;
|
|
1327
|
-
o && (r.u[5] = 1, r.f[6] =
|
|
1328
|
-
let
|
|
1329
|
-
switch (
|
|
1340
|
+
let o = t.limits, s = t.type === "prismatic" || t.type === "distance" ? n : 1, c = t.type === "wheel" ? n : s;
|
|
1341
|
+
o && (r.u[5] = 1, r.f[6] = I(o[0], "joint limit") * c, r.f[7] = I(o[1], "joint limit") * c);
|
|
1342
|
+
let l = t.motor;
|
|
1343
|
+
switch (l && (R(r.f, r.u, 8, l, s), r.u[25] = +(l.model === "acceleration")), t.type) {
|
|
1330
1344
|
case "revolute":
|
|
1331
1345
|
r.u[0] = 0;
|
|
1332
1346
|
break;
|
|
1333
1347
|
case "prismatic":
|
|
1334
|
-
r.u[0] = 1, r.f[14] =
|
|
1348
|
+
r.u[0] = 1, r.f[14] = I(t.axis[0], "axis x"), r.f[15] = I(t.axis[1], "axis y");
|
|
1335
1349
|
break;
|
|
1336
1350
|
case "fixed": {
|
|
1337
|
-
r.u[0] = 2;
|
|
1338
1351
|
let [e, n] = [t.angleA ?? 0, t.angleB ?? 0];
|
|
1339
1352
|
r.f.set([
|
|
1340
1353
|
Math.cos(e),
|
|
@@ -1342,11 +1355,19 @@ function R(e, t, n) {
|
|
|
1342
1355
|
Math.cos(n),
|
|
1343
1356
|
Math.sin(n)
|
|
1344
1357
|
], 16);
|
|
1358
|
+
let i = [
|
|
1359
|
+
I(t.linearHertz ?? 0, "linearHertz"),
|
|
1360
|
+
I(t.linearDampingRatio ?? 1, "linearDampingRatio"),
|
|
1361
|
+
I(t.angularHertz ?? 0, "angularHertz"),
|
|
1362
|
+
I(t.angularDampingRatio ?? 1, "angularDampingRatio")
|
|
1363
|
+
];
|
|
1364
|
+
if (i.some((e) => e < 0)) throw RangeError("A weld has hertz and damping ratios of 0 or more.");
|
|
1365
|
+
r.u[0] = i[0] > 0 || i[2] > 0 ? 11 : 2, r.f.set(i, 20);
|
|
1345
1366
|
break;
|
|
1346
1367
|
}
|
|
1347
1368
|
case "generic": {
|
|
1348
1369
|
r.u[0] = 5, r.u[24] = +!!t.lockX | (t.lockY ? 2 : 0) | (t.lockAngle ? 4 : 0);
|
|
1349
|
-
let [e, n] = [
|
|
1370
|
+
let [e, n] = [I(t.frameA ?? 0, "frameA"), I(t.frameB ?? 0, "frameB")];
|
|
1350
1371
|
r.f.set([
|
|
1351
1372
|
Math.cos(e),
|
|
1352
1373
|
Math.sin(e),
|
|
@@ -1357,7 +1378,7 @@ function R(e, t, n) {
|
|
|
1357
1378
|
}
|
|
1358
1379
|
case "slot": {
|
|
1359
1380
|
r.u[0] = 5, r.u[24] = 2;
|
|
1360
|
-
let e = Math.atan2(
|
|
1381
|
+
let e = Math.atan2(I(t.axis[1], "axis y"), I(t.axis[0], "axis x"));
|
|
1361
1382
|
r.f.set([
|
|
1362
1383
|
Math.cos(e),
|
|
1363
1384
|
Math.sin(e),
|
|
@@ -1367,34 +1388,106 @@ function R(e, t, n) {
|
|
|
1367
1388
|
break;
|
|
1368
1389
|
}
|
|
1369
1390
|
case "rope":
|
|
1370
|
-
r.u[0] = 3, r.f[20] =
|
|
1391
|
+
r.u[0] = 3, r.f[20] = I(t.length, "rope length") * n;
|
|
1371
1392
|
break;
|
|
1372
1393
|
case "spring":
|
|
1373
|
-
r.u[0] = 4, r.f[20] =
|
|
1394
|
+
r.u[0] = 4, r.f[20] = I(t.restLength, "spring restLength") * n, r.f[21] = I(t.stiffness, "spring stiffness"), r.f[22] = I(t.damping ?? 0, "spring damping");
|
|
1395
|
+
break;
|
|
1396
|
+
case "target": {
|
|
1397
|
+
r.u[0] = 6;
|
|
1398
|
+
let e = I(t.hertz ?? 5, "target hertz"), n = I(t.damping ?? .7, "target damping"), i = t.maxForce ?? Infinity;
|
|
1399
|
+
if (!(e > 0) || n < 0 || !(i > 0)) throw RangeError("A target joint has hertz above 0, damping of 0 or more and maxForce above 0.");
|
|
1400
|
+
r.f.set([
|
|
1401
|
+
e,
|
|
1402
|
+
n,
|
|
1403
|
+
i
|
|
1404
|
+
], 20);
|
|
1405
|
+
break;
|
|
1406
|
+
}
|
|
1407
|
+
case "wheel": {
|
|
1408
|
+
r.u[0] = 8;
|
|
1409
|
+
let e = I(t.hertz ?? 1, "wheel hertz"), n = I(t.damping ?? .7, "wheel damping");
|
|
1410
|
+
if (e < 0 || n < 0) throw RangeError("A wheel joint has hertz and damping of 0 or more.");
|
|
1411
|
+
r.f[14] = I(t.axis[0], "axis x"), r.f[15] = I(t.axis[1], "axis y"), r.f.set([e, n], 20);
|
|
1412
|
+
break;
|
|
1413
|
+
}
|
|
1414
|
+
case "pulley": {
|
|
1415
|
+
r.u[0] = 9;
|
|
1416
|
+
let e = I(t.ratio ?? 1, "pulley ratio"), i = I(t.length ?? NaN, "pulley length");
|
|
1417
|
+
if (!(e > 0) || !(i > 0)) throw RangeError("A pulley joint has a ratio and a length above 0.");
|
|
1418
|
+
r.f.set([
|
|
1419
|
+
I(t.groundA[0], "groundA x") * n,
|
|
1420
|
+
I(t.groundA[1], "groundA y") * n,
|
|
1421
|
+
I(t.groundB[0], "groundB x") * n,
|
|
1422
|
+
I(t.groundB[1], "groundB y") * n
|
|
1423
|
+
], 14), r.f.set([i * n, e], 20);
|
|
1424
|
+
break;
|
|
1425
|
+
}
|
|
1426
|
+
case "distance": {
|
|
1427
|
+
r.u[0] = 12;
|
|
1428
|
+
let e = I(t.length ?? NaN, "distance length");
|
|
1429
|
+
if (!(e > 0)) throw RangeError(`A distance joint's length is above 0 (world units); got ${t.length}.`);
|
|
1430
|
+
if (r.f[20] = e * n, t.spring) {
|
|
1431
|
+
let e = I(t.spring.hertz, "spring hertz"), n = I(t.spring.damping ?? .7, "spring damping");
|
|
1432
|
+
if (e < 0 || n < 0) throw RangeError("A distance spring has hertz and damping of 0 or more.");
|
|
1433
|
+
r.u[24] = 1, r.f.set([e, n], 21);
|
|
1434
|
+
}
|
|
1435
|
+
break;
|
|
1436
|
+
}
|
|
1437
|
+
case "gear":
|
|
1438
|
+
r.u[0] = 10;
|
|
1374
1439
|
break;
|
|
1375
|
-
|
|
1440
|
+
case "motor":
|
|
1441
|
+
case "friction": {
|
|
1442
|
+
r.u[0] = 7;
|
|
1443
|
+
let e = t.maxForce, i = t.maxTorque, a = t.type === "motor" ? t.correctionFactor ?? .3 : 0;
|
|
1444
|
+
if (!(e >= 0) || !(i >= 0) || !(a >= 0 && a <= 1)) throw RangeError(`A ${t.type} joint has maxForce and maxTorque of 0 or more and a correctionFactor from 0 to 1.`);
|
|
1445
|
+
if (t.type === "motor") {
|
|
1446
|
+
let [e, i] = t.linearOffset ?? [0, 0];
|
|
1447
|
+
r.f.set([
|
|
1448
|
+
I(e, "linearOffset x") * n,
|
|
1449
|
+
I(i, "linearOffset y") * n,
|
|
1450
|
+
0,
|
|
1451
|
+
0
|
|
1452
|
+
], 1);
|
|
1453
|
+
}
|
|
1454
|
+
r.f.set([
|
|
1455
|
+
t.type === "motor" ? I(t.angularOffset ?? 0, "angularOffset") : 0,
|
|
1456
|
+
e,
|
|
1457
|
+
i,
|
|
1458
|
+
a
|
|
1459
|
+
], 20);
|
|
1460
|
+
break;
|
|
1461
|
+
}
|
|
1462
|
+
default: throw TypeError(`Unknown joint type "${t.type}"; use revolute, prismatic, fixed, generic, slot, rope, spring, distance, target, motor, friction, wheel, pulley or gear.`);
|
|
1376
1463
|
}
|
|
1377
1464
|
return s;
|
|
1378
1465
|
}
|
|
1379
|
-
function
|
|
1466
|
+
function R(e, t, n, r, i) {
|
|
1380
1467
|
if (r.model !== void 0 && r.model !== "force" && r.model !== "acceleration") throw RangeError(`Motor model must be 'force' or 'acceleration', got ${String(r.model)}; pass one of them.`);
|
|
1381
1468
|
t[n] = 1;
|
|
1382
1469
|
let a = r.targetVelocity !== void 0, o = r.targetPosition !== void 0;
|
|
1383
|
-
e[n + 1] = o ?
|
|
1470
|
+
e[n + 1] = o ? I(r.targetPosition, "motor targetPosition") * i : 0, e[n + 2] = a ? I(r.targetVelocity, "motor targetVelocity") * i : 0, e[n + 3] = o ? I(r.stiffness ?? 1, "motor stiffness") : 0, e[n + 4] = o ? I(r.damping ?? 0, "motor damping") : I(r.factor ?? 1, "motor factor"), o && a && r.damping === void 0 && (e[n + 4] = I(r.factor ?? 1, "motor factor")), e[n + 5] = r.maxForce === void 0 ? Infinity : I(r.maxForce, "motor maxForce");
|
|
1384
1471
|
}
|
|
1385
|
-
var
|
|
1386
|
-
constructor(e, t, n, r, i, a, o, s) {
|
|
1387
|
-
this.kernel = e, this.world = t, this.handle = n, this.bodyA = r, this.bodyB = i, this.type = a, this.unit = o, this.onRemove = s, this.removed = !1;
|
|
1472
|
+
var z = class {
|
|
1473
|
+
constructor(e, t, n, r, i, a, o, s, c = 1, l) {
|
|
1474
|
+
this.kernel = e, this.world = t, this.handle = n, this.bodyA = r, this.bodyB = i, this.type = a, this.unit = o, this.onRemove = s, this.scale = c, this.spec = l, this.removed = !1;
|
|
1475
|
+
}
|
|
1476
|
+
setTarget(t) {
|
|
1477
|
+
if (this.type !== "target") throw TypeError("Only a target joint has a target; add one with {type: \"target\"}.");
|
|
1478
|
+
if (this.removed) throw new e("state", "Joint2D was removed; add a new joint with Physics2D.addJoint.");
|
|
1479
|
+
let [n, r] = V(this.bodyA, I(t[0], "target x"), I(t[1], "target y"));
|
|
1480
|
+
return this.kernel.ok(this.kernel.exports.joint_set_anchor_a(this.world, this.handle, n * this.scale, r * this.scale)), this;
|
|
1388
1481
|
}
|
|
1389
1482
|
get limited() {
|
|
1390
|
-
if (this.type !== "revolute" && this.type !== "prismatic") throw TypeError("Only revolute and
|
|
1483
|
+
if (this.type !== "revolute" && this.type !== "prismatic" && this.type !== "wheel" && this.type !== "distance") throw TypeError("Only revolute, prismatic, wheel and distance joints have limits and motors; use one of those types.");
|
|
1391
1484
|
if (this.removed) throw new e("state", "Joint2D was removed; add a new joint with Physics2D.addJoint.");
|
|
1392
1485
|
return !0;
|
|
1393
1486
|
}
|
|
1394
1487
|
setLimits(e, t) {
|
|
1395
1488
|
this.limited;
|
|
1396
|
-
let n = this.unit;
|
|
1397
|
-
return this.kernel.ok(this.kernel.exports.joint_set_limits(this.world, this.handle, 1,
|
|
1489
|
+
let n = this.type === "wheel" ? this.scale : this.unit;
|
|
1490
|
+
return this.kernel.ok(this.kernel.exports.joint_set_limits(this.world, this.handle, 1, I(e, "limit") * n, I(t, "limit") * n)), this;
|
|
1398
1491
|
}
|
|
1399
1492
|
clearLimits() {
|
|
1400
1493
|
return this.limited, this.kernel.ok(this.kernel.exports.joint_set_limits(this.world, this.handle, 0, 0, 0)), this;
|
|
@@ -1402,18 +1495,49 @@ var B = class {
|
|
|
1402
1495
|
setMotor(e) {
|
|
1403
1496
|
this.limited;
|
|
1404
1497
|
let t = this.kernel.io(8);
|
|
1405
|
-
return
|
|
1498
|
+
return R(t.f, t.u, 0, e, this.unit), t.f.copyWithin(0, 1, 6), t.f[5] = +(e.model === "acceleration"), this.kernel.ok(this.kernel.exports.joint_set_motor(this.world, this.handle, 1)), this;
|
|
1406
1499
|
}
|
|
1407
1500
|
clearMotor() {
|
|
1408
1501
|
return this.limited, this.kernel.ok(this.kernel.exports.joint_set_motor(this.world, this.handle, 0)), this;
|
|
1409
1502
|
}
|
|
1503
|
+
get force() {
|
|
1504
|
+
let [e, t] = this.reaction();
|
|
1505
|
+
return [e, t];
|
|
1506
|
+
}
|
|
1507
|
+
get torque() {
|
|
1508
|
+
return this.reaction()[2];
|
|
1509
|
+
}
|
|
1510
|
+
reaction() {
|
|
1511
|
+
if (this.removed) throw new e("state", "Joint2D was removed; add a new joint with Physics2D.addJoint.");
|
|
1512
|
+
this.kernel.ok(this.kernel.exports.joint_force(this.world, this.handle));
|
|
1513
|
+
let t = this.kernel.io(3);
|
|
1514
|
+
return [
|
|
1515
|
+
t.f[0],
|
|
1516
|
+
t.f[1],
|
|
1517
|
+
t.f[2]
|
|
1518
|
+
];
|
|
1519
|
+
}
|
|
1520
|
+
get isRemoved() {
|
|
1521
|
+
return this.removed;
|
|
1522
|
+
}
|
|
1410
1523
|
remove() {
|
|
1411
1524
|
this.onRemove(this);
|
|
1412
1525
|
}
|
|
1413
1526
|
free() {
|
|
1414
1527
|
this.removed || (this.removed = !0, this.kernel.exports.joint_remove(this.world, this.handle));
|
|
1415
1528
|
}
|
|
1416
|
-
}
|
|
1529
|
+
};
|
|
1530
|
+
function B(e, t, n) {
|
|
1531
|
+
let [r, i] = e.position, a = e.rotation;
|
|
1532
|
+
return [r + Math.cos(a) * t - Math.sin(a) * n, i + Math.sin(a) * t + Math.cos(a) * n];
|
|
1533
|
+
}
|
|
1534
|
+
function V(e, t, n) {
|
|
1535
|
+
let [r, i] = e.position, a = e.rotation, o = t - r, s = n - i;
|
|
1536
|
+
return [Math.cos(a) * o + Math.sin(a) * s, -Math.sin(a) * o + Math.cos(a) * s];
|
|
1537
|
+
}
|
|
1538
|
+
//#endregion
|
|
1539
|
+
//#region src/physics.ts
|
|
1540
|
+
var H = [
|
|
1417
1541
|
"fixed",
|
|
1418
1542
|
"awake",
|
|
1419
1543
|
"sleeping",
|
|
@@ -1427,7 +1551,7 @@ var B = class {
|
|
|
1427
1551
|
"axisY",
|
|
1428
1552
|
"solverPoint",
|
|
1429
1553
|
"solverNormal"
|
|
1430
|
-
],
|
|
1554
|
+
], U = {
|
|
1431
1555
|
fixed: [
|
|
1432
1556
|
.5,
|
|
1433
1557
|
.5,
|
|
@@ -1506,16 +1630,16 @@ var B = class {
|
|
|
1506
1630
|
.3,
|
|
1507
1631
|
1
|
|
1508
1632
|
]
|
|
1509
|
-
},
|
|
1510
|
-
function
|
|
1633
|
+
}, W = () => {};
|
|
1634
|
+
function te(e) {
|
|
1511
1635
|
e(this);
|
|
1512
1636
|
}
|
|
1513
|
-
var G = Math.PI * 2, K =
|
|
1637
|
+
var G = Math.PI * 2, K = 12, q = 4294967295, J = new i(({ wasm: e, simd: t }, n) => import("./kernel-DkjP8dvA.js").then((r) => r.startKernel(e ?? r.defaultWasm(!(t ?? r.hasSimd())), n))), Y = {
|
|
1514
1638
|
dynamic: 0,
|
|
1515
1639
|
fixed: 1,
|
|
1516
1640
|
kinematicPosition: 2,
|
|
1517
1641
|
kinematicVelocity: 2
|
|
1518
|
-
},
|
|
1642
|
+
}, X = [
|
|
1519
1643
|
["substeps", "count"],
|
|
1520
1644
|
["iterations", "count"],
|
|
1521
1645
|
["contactHertz", "amount"],
|
|
@@ -1539,19 +1663,19 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1539
1663
|
["predictionDistance", "depth"],
|
|
1540
1664
|
["staticContactHertz", "spring"],
|
|
1541
1665
|
["staticContactDamping", "spring"]
|
|
1542
|
-
],
|
|
1666
|
+
], Z = (e) => e === "speed" || e === "length" || e === "depth", Q = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
1543
1667
|
static async load({ signal: e, wasm: t, simd: n } = {}) {
|
|
1544
|
-
await
|
|
1668
|
+
await J.load({
|
|
1545
1669
|
wasm: t,
|
|
1546
1670
|
simd: n
|
|
1547
1671
|
}, e);
|
|
1548
1672
|
}
|
|
1549
|
-
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("
|
|
1673
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
1550
1674
|
return new t(e, n);
|
|
1551
1675
|
}
|
|
1552
1676
|
constructor(t, n = {}) {
|
|
1553
|
-
this.stepCount = 0, this.engine = null, this.accumulator = 0, this.lastAlpha = 1, this.disposed = !1, this.bodies = /* @__PURE__ */ new Set(), this.movers = null, this.byShape = /* @__PURE__ */ new Map(), this.joints = /* @__PURE__ */ new Set(), this.controllers = /* @__PURE__ */ new Set(), this.listeners = /* @__PURE__ */ new Map(), this.events = [], this.dispatching = !1, this.syncAlpha = 1, this.hookSet = null, this.anchor = -1;
|
|
1554
|
-
let r =
|
|
1677
|
+
this.stepCount = 0, this.engine = null, this.accumulator = 0, this.lastAlpha = 1, this.disposed = !1, this.bodies = /* @__PURE__ */ new Set(), this.movers = null, this.byShape = /* @__PURE__ */ new Map(), this.nextColliderUid = 1, this.touchingPairs = /* @__PURE__ */ new Map(), this.carriedPairs = /* @__PURE__ */ new Map(), this.joints = /* @__PURE__ */ new Set(), this.controllers = /* @__PURE__ */ new Set(), this.listeners = /* @__PURE__ */ new Map(), this.events = [], this.breakable = /* @__PURE__ */ new Map(), this.gears = [], this.dispatching = !1, this.syncAlpha = 1, this.hookSet = null, this.anchor = -1, this.asleep = null;
|
|
1678
|
+
let r = J.value;
|
|
1555
1679
|
if (!r) throw new e("state", "Physics2D is not loaded; call await Physics2D.load() once before new Physics2D().");
|
|
1556
1680
|
this.kernel = r, this.name = n.name ?? "physics2d";
|
|
1557
1681
|
let i = n.pixelsPerMeter ?? 32;
|
|
@@ -1634,14 +1758,14 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1634
1758
|
get solver() {
|
|
1635
1759
|
this.live();
|
|
1636
1760
|
let { kernel: e } = this, t = e.io(e.ok(e.exports.world_settings(this.handle))).f, n = {};
|
|
1637
|
-
return
|
|
1638
|
-
n[e] = r === "flag" ? t[i] !== 0 : r === "spring" && t[i] === -1 ? null :
|
|
1761
|
+
return X.forEach(([e, r], i) => {
|
|
1762
|
+
n[e] = r === "flag" ? t[i] !== 0 : r === "spring" && t[i] === -1 ? null : Z(r) ? t[i] / this.scale : t[i];
|
|
1639
1763
|
}), n;
|
|
1640
1764
|
}
|
|
1641
1765
|
setSolver(e) {
|
|
1642
1766
|
this.live();
|
|
1643
1767
|
let { kernel: t } = this;
|
|
1644
|
-
for (let [t, n] of
|
|
1768
|
+
for (let [t, n] of X) {
|
|
1645
1769
|
let r = e[t];
|
|
1646
1770
|
if (n === "flag" || r === void 0) continue;
|
|
1647
1771
|
if (r === null) {
|
|
@@ -1679,9 +1803,9 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1679
1803
|
];
|
|
1680
1804
|
if (!a) throw RangeError(`Solver ${t} must be ${o}; got ${r}. Give ${s}.`);
|
|
1681
1805
|
}
|
|
1682
|
-
return
|
|
1806
|
+
return X.forEach(([n, r], i) => {
|
|
1683
1807
|
let a = e[n];
|
|
1684
|
-
a !== void 0 && t.ok(t.exports.world_set_setting(this.handle, i, a === null ? -1 : Number(a) * (
|
|
1808
|
+
a !== void 0 && t.ok(t.exports.world_set_setting(this.handle, i, a === null ? -1 : Number(a) * (Z(r) ? this.scale : 1)));
|
|
1685
1809
|
}), this;
|
|
1686
1810
|
}
|
|
1687
1811
|
getBodies() {
|
|
@@ -1731,7 +1855,7 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1731
1855
|
}
|
|
1732
1856
|
this.stepCount++, this.readPoses();
|
|
1733
1857
|
let n = this.dispatching, r = n ? [] : this.events;
|
|
1734
|
-
this.collectEvents(r), this.dispatching = !0;
|
|
1858
|
+
this.collectEvents(r), this.breakJoints(r), this.asleep && this.sleepEvents(r), this.dispatching = !0;
|
|
1735
1859
|
try {
|
|
1736
1860
|
for (let [e, t] of r) this.emit(e, t);
|
|
1737
1861
|
} finally {
|
|
@@ -1748,24 +1872,56 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1748
1872
|
let i = n.io(5 * t).f, a = 1 / this.scale;
|
|
1749
1873
|
for (let n = 0; n < t; n++) e[n].store(i[t + 4 * n], i[t + 4 * n + 1], i[t + 4 * n + 2], i[t + 4 * n + 3], a);
|
|
1750
1874
|
}
|
|
1875
|
+
pairKey(e, t) {
|
|
1876
|
+
return e.uid < t.uid ? `${e.uid}:${t.uid}` : `${t.uid}:${e.uid}`;
|
|
1877
|
+
}
|
|
1878
|
+
pushCollisionEvent(e, t, n, r, i) {
|
|
1879
|
+
let a = i ? r ? "intersectionstart" : "intersectionend" : r ? "contactstart" : "contactend", o = a === "contactstart" ? this.contactPair(t, n) ?? void 0 : void 0;
|
|
1880
|
+
e.push([a, {
|
|
1881
|
+
type: a,
|
|
1882
|
+
bodyA: t.body,
|
|
1883
|
+
bodyB: n.body,
|
|
1884
|
+
colliderA: t,
|
|
1885
|
+
colliderB: n,
|
|
1886
|
+
sensor: i,
|
|
1887
|
+
...o ? { contact: o } : {}
|
|
1888
|
+
}]);
|
|
1889
|
+
}
|
|
1751
1890
|
collectEvents(e) {
|
|
1752
1891
|
let { kernel: t } = this, n = t.ok(t.exports.events_read(this.handle));
|
|
1753
1892
|
if (n) {
|
|
1754
|
-
let r = t.io(3 * n).u;
|
|
1893
|
+
let r = t.io(3 * n).u.slice(0, 3 * n);
|
|
1755
1894
|
for (let t = 0; t < n; t++) {
|
|
1756
1895
|
let n = this.byShape.get(r[3 * t]), i = this.byShape.get(r[3 * t + 1]);
|
|
1757
|
-
if (!n || !i) continue;
|
|
1758
|
-
let a = r[3 * t + 2], o = (a & 3) == 1, s = !!(a & 4);
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1896
|
+
if (!n || !i || n === i) continue;
|
|
1897
|
+
let a = r[3 * t + 2], o = (a & 3) == 1, s = !!(a & 4), c = this.pairKey(n, i);
|
|
1898
|
+
if (o) {
|
|
1899
|
+
if (this.carriedPairs.delete(c)) {
|
|
1900
|
+
this.touchingPairs.set(c, {
|
|
1901
|
+
count: 1,
|
|
1902
|
+
sensor: s,
|
|
1903
|
+
a: n,
|
|
1904
|
+
b: i
|
|
1905
|
+
});
|
|
1906
|
+
continue;
|
|
1907
|
+
}
|
|
1908
|
+
let t = this.touchingPairs.get(c);
|
|
1909
|
+
t ? t.count++ : (this.touchingPairs.set(c, {
|
|
1910
|
+
count: 1,
|
|
1911
|
+
sensor: s,
|
|
1912
|
+
a: n,
|
|
1913
|
+
b: i
|
|
1914
|
+
}), this.pushCollisionEvent(e, n, i, !0, s));
|
|
1915
|
+
} else {
|
|
1916
|
+
let t = this.touchingPairs.get(c);
|
|
1917
|
+
t ? (t.count--, t.count <= 0 && (this.touchingPairs.delete(c), this.pushCollisionEvent(e, n, i, !1, s))) : this.carriedPairs.has(c) || this.pushCollisionEvent(e, n, i, !1, s);
|
|
1918
|
+
}
|
|
1767
1919
|
}
|
|
1768
1920
|
}
|
|
1921
|
+
if (this.carriedPairs.size) {
|
|
1922
|
+
for (let t of this.carriedPairs.values()) this.pushCollisionEvent(e, t.a, t.b, !1, t.sensor);
|
|
1923
|
+
this.carriedPairs.clear();
|
|
1924
|
+
}
|
|
1769
1925
|
let r = t.ok(t.exports.force_events_read(this.handle));
|
|
1770
1926
|
if (r) {
|
|
1771
1927
|
let n = t.io(5 * r);
|
|
@@ -1799,7 +1955,7 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1799
1955
|
addBody(e, t = {}) {
|
|
1800
1956
|
this.live();
|
|
1801
1957
|
let n = t.type ?? "dynamic", r = this.scale;
|
|
1802
|
-
if (!(n in
|
|
1958
|
+
if (!(n in Y)) throw TypeError(`Unknown body type "${n}"; use dynamic, fixed, kinematicPosition or kinematicVelocity.`);
|
|
1803
1959
|
let i = t.position ?? [e?.x ?? 0, e?.y ?? 0];
|
|
1804
1960
|
this.checkVec(i, "Body position");
|
|
1805
1961
|
let a = t.rotation ?? e?.rotation ?? 0;
|
|
@@ -1821,7 +1977,7 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1821
1977
|
t.angularDamping ?? 0,
|
|
1822
1978
|
t.gravityScale ?? 1
|
|
1823
1979
|
], 0), new Int32Array(c.u.buffer, c.u.byteOffset, 16)[10] = s;
|
|
1824
|
-
let l = (t.canSleep ?? !0 ? 1 : 0) | (t.lockRotation ? 2 : 0) | (t.ccd ? 4 : 0), u = this.kernel.ok(this.kernel.exports.body_create(this.handle,
|
|
1980
|
+
let l = (t.canSleep ?? !0 ? 1 : 0) | (t.lockRotation ? 2 : 0) | (t.ccd ? 4 : 0), u = this.kernel.ok(this.kernel.exports.body_create(this.handle, Y[n], l)), d = new A(this, u, e, n);
|
|
1825
1981
|
if (d.userData = t.userData, d.turnLocked = t.lockRotation === !0, d.params = {
|
|
1826
1982
|
linearDamping: t.linearDamping ?? 0,
|
|
1827
1983
|
angularDamping: t.angularDamping ?? 0,
|
|
@@ -1850,7 +2006,7 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1850
2006
|
this.bodies.add(d), this.movers = null;
|
|
1851
2007
|
try {
|
|
1852
2008
|
let n = t.colliders;
|
|
1853
|
-
n === void 0 && (n =
|
|
2009
|
+
n === void 0 && (n = ne(e));
|
|
1854
2010
|
for (let e of Array.isArray(n) ? n : n ? [n] : []) this.addCollider(d, e);
|
|
1855
2011
|
} catch (e) {
|
|
1856
2012
|
throw this.removeBody(d), e;
|
|
@@ -1887,11 +2043,49 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1887
2043
|
for (let e of r) this.kernel.exports.shape_remove(this.handle, e);
|
|
1888
2044
|
throw e;
|
|
1889
2045
|
}
|
|
1890
|
-
let i = Uint32Array.from(n[0].settings), a = new k(this, e, r, i, t.offset ?? [0, 0], t.rotation ?? 0);
|
|
1891
|
-
a.described = n[0], a.options = t, t.mass !== void 0 && a.setMass(t.mass);
|
|
2046
|
+
let i = Uint32Array.from(n[0].settings), a = new k(this, e, r, i, t.offset ?? [0, 0], t.rotation ?? 0, this.nextColliderUid++);
|
|
2047
|
+
a.described = n[0], a.options = t, t.mass !== void 0 && a.setMass(t.mass), t.rollingResistance !== void 0 && a.setRollingResistance(t.rollingResistance);
|
|
1892
2048
|
for (let e of r) this.byShape.set(e, a);
|
|
1893
2049
|
return e.colliders.push(a), a;
|
|
1894
2050
|
}
|
|
2051
|
+
replaceColliderShapes(e, t, n) {
|
|
2052
|
+
this.live();
|
|
2053
|
+
let { body: r } = e, i = e.rawSettings, a = [];
|
|
2054
|
+
try {
|
|
2055
|
+
let e = [];
|
|
2056
|
+
for (let t of n) {
|
|
2057
|
+
let n = this.createShapes(r, {
|
|
2058
|
+
...t,
|
|
2059
|
+
settings: i
|
|
2060
|
+
});
|
|
2061
|
+
a.push(...n), t.seam && e.push(...n);
|
|
2062
|
+
}
|
|
2063
|
+
if (e.length > 1) {
|
|
2064
|
+
let { kernel: t } = this;
|
|
2065
|
+
t.io(e.length).u.set(e, 0), t.ok(t.exports.shapes_join_seams(this.handle, e.length));
|
|
2066
|
+
}
|
|
2067
|
+
} catch (e) {
|
|
2068
|
+
for (let e of a) this.kernel.exports.shape_remove(this.handle, e);
|
|
2069
|
+
throw e;
|
|
2070
|
+
}
|
|
2071
|
+
for (let [t, n] of this.touchingPairs) (n.a === e || n.b === e) && (this.carriedPairs.set(t, {
|
|
2072
|
+
a: n.a,
|
|
2073
|
+
b: n.b,
|
|
2074
|
+
sensor: n.sensor
|
|
2075
|
+
}), this.touchingPairs.delete(t));
|
|
2076
|
+
let o = e.rollingResistance();
|
|
2077
|
+
if (o > 0) for (let e of a) this.kernel.ok(this.kernel.exports.shape_set_rolling_resistance(this.handle, e, o));
|
|
2078
|
+
for (let t of e.shapes) this.byShape.delete(t), this.kernel.exports.shape_remove(this.handle, t);
|
|
2079
|
+
e.shapes.splice(0, e.shapes.length, ...a);
|
|
2080
|
+
for (let t of a) this.byShape.set(t, e);
|
|
2081
|
+
e.described = {
|
|
2082
|
+
...n[0],
|
|
2083
|
+
settings: i
|
|
2084
|
+
}, e.options &&= {
|
|
2085
|
+
...e.options,
|
|
2086
|
+
...t
|
|
2087
|
+
}, e.reapplyMass();
|
|
2088
|
+
}
|
|
1895
2089
|
createShapes(e, t) {
|
|
1896
2090
|
let { kernel: n } = this, r = n.io(8 + t.geometry.length);
|
|
1897
2091
|
r.u.set(t.settings, 0), r.u.set(t.geometry, 8);
|
|
@@ -1900,6 +2094,8 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1900
2094
|
}
|
|
1901
2095
|
removeCollider(e) {
|
|
1902
2096
|
if (e.isRemoved) return;
|
|
2097
|
+
for (let [t, n] of this.touchingPairs) (n.a === e || n.b === e) && this.touchingPairs.delete(t);
|
|
2098
|
+
for (let [t, n] of this.carriedPairs) (n.a === e || n.b === e) && this.carriedPairs.delete(t);
|
|
1903
2099
|
for (let t of e.shapes) this.byShape.delete(t), this.kernel.exports.shape_remove(this.handle, t);
|
|
1904
2100
|
e.markRemoved();
|
|
1905
2101
|
let t = e.body.colliders;
|
|
@@ -1910,6 +2106,8 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1910
2106
|
for (let t of [...this.joints]) (t.bodyA === e || t.bodyB === e) && this.removeJoint(t);
|
|
1911
2107
|
for (let t of [...this.controllers]) t.body === e && t.dispose();
|
|
1912
2108
|
for (let t of e.colliders) {
|
|
2109
|
+
for (let [e, n] of this.touchingPairs) (n.a === t || n.b === t) && this.touchingPairs.delete(e);
|
|
2110
|
+
for (let [e, n] of this.carriedPairs) (n.a === t || n.b === t) && this.carriedPairs.delete(e);
|
|
1913
2111
|
for (let e of t.shapes) this.byShape.delete(e);
|
|
1914
2112
|
t.markRemoved();
|
|
1915
2113
|
}
|
|
@@ -1950,8 +2148,8 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1950
2148
|
} : null;
|
|
1951
2149
|
}
|
|
1952
2150
|
retype(e, t) {
|
|
1953
|
-
if (this.live(), !(t in
|
|
1954
|
-
this.kernel.ok(this.kernel.exports.body_set_kind(this.handle, e.handle,
|
|
2151
|
+
if (this.live(), !(t in Y)) throw TypeError(`Unknown body type "${t}"; use dynamic, fixed, kinematicPosition or kinematicVelocity.`);
|
|
2152
|
+
this.kernel.ok(this.kernel.exports.body_set_kind(this.handle, e.handle, Y[t])), this.movers = null, e.snapshot(), e.pose.copyWithin(0, 3, 6);
|
|
1955
2153
|
}
|
|
1956
2154
|
projectPoint(e, t, n = !0) {
|
|
1957
2155
|
this.live(), this.checkVec(e, "Point");
|
|
@@ -1971,6 +2169,27 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1971
2169
|
}
|
|
1972
2170
|
return null;
|
|
1973
2171
|
}
|
|
2172
|
+
explode(e) {
|
|
2173
|
+
this.live(), this.checkVec(e.position, "Explosion position");
|
|
2174
|
+
let t = e.falloff ?? 0;
|
|
2175
|
+
for (let [n, r] of [[e.radius, "radius"], [t, "falloff"]]) if (!(n >= 0 && n < Infinity)) throw RangeError(`An explosion's ${r} is a finite number of 0 or more; got ${n}.`);
|
|
2176
|
+
if (!Number.isFinite(e.impulsePerLength)) throw RangeError(`impulsePerLength must be a finite number; got ${e.impulsePerLength}.`);
|
|
2177
|
+
this.writeFilter(e.filter);
|
|
2178
|
+
let n = this.scale;
|
|
2179
|
+
this.kernel.io(9).f.set([
|
|
2180
|
+
e.position[0] * n,
|
|
2181
|
+
e.position[1] * n,
|
|
2182
|
+
e.radius * n,
|
|
2183
|
+
t * n,
|
|
2184
|
+
e.impulsePerLength
|
|
2185
|
+
], 4);
|
|
2186
|
+
let r = this.kernel.ok(this.kernel.exports.explosion(this.handle)), i = this.kernel.io(5 * r), a = Array.from({ length: r }, (e, t) => [i.u[5 * t], ...i.f.subarray(5 * t + 1, 5 * t + 5)]), o = [];
|
|
2187
|
+
for (let [t, n, r, i, s] of a) {
|
|
2188
|
+
let a = this.hitOf(t, e.filter);
|
|
2189
|
+
a?.collider && (this.kernel.ok(this.kernel.exports.body_impulse(this.handle, a.body.handle, n, r, 1, i, s)), o.push(a.collider));
|
|
2190
|
+
}
|
|
2191
|
+
return o;
|
|
2192
|
+
}
|
|
1974
2193
|
intersectionsWithBox(e, t, n) {
|
|
1975
2194
|
this.live(), this.checkVec(e, "Box min"), this.checkVec(t, "Box max"), this.writeFilter(n);
|
|
1976
2195
|
let r = this.scale;
|
|
@@ -1985,29 +2204,31 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
1985
2204
|
}
|
|
1986
2205
|
contactPairs(t) {
|
|
1987
2206
|
if (this.live(), t.isRemoved) throw new e("state", "Collider2D was removed; ask for the contacts of a live collider.");
|
|
1988
|
-
let n =
|
|
2207
|
+
let n = /* @__PURE__ */ new Map(), r = 1 / this.scale;
|
|
1989
2208
|
for (let e of t.shapes) {
|
|
1990
|
-
let i = this.kernel.ok(this.kernel.exports.contacts_of(this.handle, e)), { f: a, u: o } = this.kernel.io(
|
|
2209
|
+
let i = this.kernel.ok(this.kernel.exports.contacts_of(this.handle, e)), { f: a, u: o } = this.kernel.io(K * i);
|
|
1991
2210
|
for (let e = 0; e < i; e++) {
|
|
1992
|
-
let i = this.byShape.get(o[
|
|
1993
|
-
if (!i) continue;
|
|
1994
|
-
let s = o[
|
|
1995
|
-
for (let t = 0; t < c; t++)
|
|
1996
|
-
point: [a[
|
|
1997
|
-
distance: a[
|
|
2211
|
+
let i = this.byShape.get(o[K * e]);
|
|
2212
|
+
if (!i || i === t) continue;
|
|
2213
|
+
let s = o[K * e + 1], c = s >> 2, l = !!(s & 1), u = !!(s & 2), d = a[K * e + 10], f = a[K * e + 11] * r, p = [a[K * e + 2], a[K * e + 3]], m = [];
|
|
2214
|
+
for (let t = 0; t < c; t++) m.push({
|
|
2215
|
+
point: [a[K * e + 4 + 3 * t] * r, a[K * e + 5 + 3 * t] * r],
|
|
2216
|
+
distance: a[K * e + 6 + 3 * t] * r
|
|
1998
2217
|
});
|
|
1999
|
-
n.
|
|
2218
|
+
let h = n.get(i);
|
|
2219
|
+
h ? (h.points.push(...m), h.approachSpeed = Math.max(h.approachSpeed, f), (!h.touching && u || u && d > h.impulse) && (h.normal = p), h.impulse += d, h.solverActive = h.solverActive || l, h.touching = h.touching || u) : n.set(i, {
|
|
2000
2220
|
collider: t,
|
|
2001
2221
|
other: i,
|
|
2002
|
-
normal:
|
|
2003
|
-
points:
|
|
2004
|
-
impulse:
|
|
2005
|
-
|
|
2006
|
-
|
|
2222
|
+
normal: p,
|
|
2223
|
+
points: m,
|
|
2224
|
+
impulse: d,
|
|
2225
|
+
approachSpeed: f,
|
|
2226
|
+
solverActive: l,
|
|
2227
|
+
touching: u
|
|
2007
2228
|
});
|
|
2008
2229
|
}
|
|
2009
2230
|
}
|
|
2010
|
-
return n;
|
|
2231
|
+
return [...n.values()];
|
|
2011
2232
|
}
|
|
2012
2233
|
contactPair(e, t) {
|
|
2013
2234
|
return this.contactPairs(e).find((e) => e.other === t) ?? null;
|
|
@@ -2047,11 +2268,98 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
2047
2268
|
}
|
|
2048
2269
|
addJoint(t, n, r) {
|
|
2049
2270
|
if (this.live(), !this.bodies.has(t) || !this.bodies.has(n)) throw new e("state", "Both joint bodies must belong to this Physics2D and not be removed.");
|
|
2050
|
-
let i =
|
|
2051
|
-
|
|
2271
|
+
let i = (e, t, n) => {
|
|
2272
|
+
let [r, i] = B(e, t?.[0] ?? 0, t?.[1] ?? 0);
|
|
2273
|
+
return Math.hypot(r - n[0], i - n[1]);
|
|
2274
|
+
}, a = r.type === "gear" ? this.gearSides(t, n, r.jointA, r.jointB) : void 0, o = a === void 0 ? r.type === "target" ? {
|
|
2275
|
+
...r,
|
|
2276
|
+
anchorA: V(t, r.target[0], r.target[1])
|
|
2277
|
+
} : r.type === "distance" && r.length === void 0 ? (() => {
|
|
2278
|
+
let [e, i] = B(t, r.anchorA?.[0] ?? 0, r.anchorA?.[1] ?? 0), [a, o] = B(n, r.anchorB?.[0] ?? 0, r.anchorB?.[1] ?? 0);
|
|
2279
|
+
return {
|
|
2280
|
+
...r,
|
|
2281
|
+
length: Math.hypot(a - e, o - i)
|
|
2282
|
+
};
|
|
2283
|
+
})() : r.type === "pulley" && r.length === void 0 ? {
|
|
2284
|
+
...r,
|
|
2285
|
+
length: i(t, r.anchorA, r.groundA) + (r.ratio ?? 1) * i(n, r.anchorB, r.groundB)
|
|
2286
|
+
} : r : {
|
|
2287
|
+
...r,
|
|
2288
|
+
anchorA: a.a.anchor,
|
|
2289
|
+
anchorB: a.b.anchor
|
|
2290
|
+
}, s = L(this.kernel, o, this.scale);
|
|
2291
|
+
if (a !== void 0 && r.type === "gear") {
|
|
2292
|
+
let e = r.ratio ?? 1;
|
|
2293
|
+
if (!Number.isFinite(e) || e === 0) throw RangeError(`A gear ratio is a finite number other than 0; got ${e}.`);
|
|
2294
|
+
let t = this.kernel.io(32);
|
|
2295
|
+
t.f.set([
|
|
2296
|
+
...a.a.axis,
|
|
2297
|
+
...a.b.axis,
|
|
2298
|
+
...a.a.anchorC,
|
|
2299
|
+
e * a.a.unit / a.b.unit,
|
|
2300
|
+
...a.b.anchorC
|
|
2301
|
+
], 14), t.u[23] = a.a.carrier.handle, t.u[24] = a.b.carrier.handle;
|
|
2302
|
+
}
|
|
2303
|
+
let c = this.kernel.ok(this.kernel.exports.joint_create(this.handle, t.handle, n.handle, +(r.collide === !0))), l = new z(this.kernel, this.handle, c, t, n, r.type, s, (e) => this.removeJoint(e), this.scale, o);
|
|
2304
|
+
this.joints.add(l), r.type === "gear" && this.gears.push([
|
|
2305
|
+
l,
|
|
2306
|
+
r.jointA,
|
|
2307
|
+
r.jointB
|
|
2308
|
+
]);
|
|
2309
|
+
let u = r.breakForce ?? Infinity, d = r.breakTorque ?? Infinity;
|
|
2310
|
+
if (!(u > 0) || !(d > 0)) throw RangeError("breakForce and breakTorque are above 0 (N, N m); leave them out for a joint that holds.");
|
|
2311
|
+
return (u < Infinity || d < Infinity) && this.breakable.set(l, [u, d]), l;
|
|
2312
|
+
}
|
|
2313
|
+
breakJoints(e) {
|
|
2314
|
+
for (let [t, [n, r]] of this.breakable) {
|
|
2315
|
+
if (t.isRemoved) {
|
|
2316
|
+
this.breakable.delete(t);
|
|
2317
|
+
continue;
|
|
2318
|
+
}
|
|
2319
|
+
let [i, a, o] = t.reaction();
|
|
2320
|
+
Math.hypot(i, a) <= n && Math.abs(o) <= r || (this.breakable.delete(t), this.removeJoint(t), e.push(["jointbreak", {
|
|
2321
|
+
joint: t,
|
|
2322
|
+
force: [i, a],
|
|
2323
|
+
torque: o
|
|
2324
|
+
}]));
|
|
2325
|
+
}
|
|
2052
2326
|
}
|
|
2053
2327
|
removeJoint(e) {
|
|
2054
|
-
this.joints.delete(e)
|
|
2328
|
+
if (!this.joints.delete(e)) return;
|
|
2329
|
+
e.free();
|
|
2330
|
+
let t = this.gears.filter(([t, n, r]) => t === e || n === e || r === e);
|
|
2331
|
+
if (t.length !== 0) {
|
|
2332
|
+
this.gears = this.gears.filter((e) => !t.includes(e));
|
|
2333
|
+
for (let [e] of t) this.removeJoint(e);
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
gearSides(t, n, r, i) {
|
|
2337
|
+
let a = (t, n, r) => {
|
|
2338
|
+
let i = t.spec;
|
|
2339
|
+
if (!this.joints.has(t) || i === void 0) throw new e("state", `${r} must be a joint of this Physics2D and not removed.`);
|
|
2340
|
+
if (i.type !== "revolute" && i.type !== "prismatic") throw TypeError(`${r} is a ${i.type} joint; a gear gears revolute and prismatic joints.`);
|
|
2341
|
+
if (t.bodyB !== n) throw TypeError(`${r}'s body B must be the gear's own body ${r === "jointA" ? "A" : "B"}.`);
|
|
2342
|
+
let a = this.scale, o = i.anchorB ?? [0, 0], [s, c] = i.anchorA ?? [0, 0];
|
|
2343
|
+
if (i.type === "revolute") return {
|
|
2344
|
+
anchor: o,
|
|
2345
|
+
carrier: t.bodyA,
|
|
2346
|
+
axis: [0, 0],
|
|
2347
|
+
anchorC: [s * a, c * a],
|
|
2348
|
+
unit: 1
|
|
2349
|
+
};
|
|
2350
|
+
let [l, u] = i.axis, d = Math.hypot(l, u);
|
|
2351
|
+
return {
|
|
2352
|
+
anchor: o,
|
|
2353
|
+
carrier: t.bodyA,
|
|
2354
|
+
axis: [l / d, u / d],
|
|
2355
|
+
anchorC: [s * a, c * a],
|
|
2356
|
+
unit: a
|
|
2357
|
+
};
|
|
2358
|
+
};
|
|
2359
|
+
return {
|
|
2360
|
+
a: a(r, t, "jointA"),
|
|
2361
|
+
b: a(i, n, "jointB")
|
|
2362
|
+
};
|
|
2055
2363
|
}
|
|
2056
2364
|
addArticulation(t, n) {
|
|
2057
2365
|
if (this.live(), !this.bodies.has(t)) throw new e("state", "The root must be a body of this Physics2D and not removed.");
|
|
@@ -2060,12 +2368,12 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
2060
2368
|
}
|
|
2061
2369
|
createCharacterController(e, t = {}) {
|
|
2062
2370
|
if (this.live(), e.type !== "kinematicPosition") throw TypeError("Character bodies must be 'kinematicPosition'; add the body with {type: 'kinematicPosition'}.");
|
|
2063
|
-
let n = new
|
|
2371
|
+
let n = new F(this, e, t, (e) => this.controllers.delete(e));
|
|
2064
2372
|
return this.controllers.add(n), n;
|
|
2065
2373
|
}
|
|
2066
2374
|
writeFilter(e = {}) {
|
|
2067
2375
|
let t = this.kernel.io(4);
|
|
2068
|
-
t.u[0] = e.groups ?? 4294967295, t.u[1] = +!!e.excludeSensors | (e.excludeFixed ? 2 : 0) | (e.excludeKinematic ? 4 : 0) | (e.excludeDynamic ? 8 : 0), t.u[2] =
|
|
2376
|
+
t.u[0] = e.groups ?? 4294967295, t.u[1] = +!!e.excludeSensors | (e.excludeFixed ? 2 : 0) | (e.excludeKinematic ? 4 : 0) | (e.excludeDynamic ? 8 : 0), t.u[2] = q, t.u[3] = e.exclude && this.bodies.has(e.exclude) ? e.exclude.handle : q;
|
|
2069
2377
|
}
|
|
2070
2378
|
hitOf(e, t) {
|
|
2071
2379
|
let n = this.byShape.get(e);
|
|
@@ -2203,6 +2511,26 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
2203
2511
|
new Uint8Array(s.u.buffer, s.u.byteOffset, a.length).set(a), o.ok(o.exports.world_restore(this.handle, a.length)), this.gravityValue = [i.gravity[0], i.gravity[1]], this.accumulator = 0;
|
|
2204
2512
|
let c = 0;
|
|
2205
2513
|
for (let e of this.bodies) e.snapshot(), e.pose.copyWithin(0, 3, 6), e.dirty = !0, e.restoreAdditionalMass(i.extra[c++] ?? 0);
|
|
2514
|
+
this.resyncTouchingPairs();
|
|
2515
|
+
}
|
|
2516
|
+
resyncTouchingPairs() {
|
|
2517
|
+
this.touchingPairs.clear(), this.carriedPairs.clear();
|
|
2518
|
+
for (let [e, t] of this.byShape) {
|
|
2519
|
+
let n = this.kernel.ok(this.kernel.exports.contacts_of(this.handle, e)), { u: r } = this.kernel.io(K * n);
|
|
2520
|
+
for (let i = 0; i < n; i++) {
|
|
2521
|
+
let n = r[K * i];
|
|
2522
|
+
if (e >= n || !(r[K * i + 1] & 2)) continue;
|
|
2523
|
+
let a = this.byShape.get(n);
|
|
2524
|
+
if (!a || a === t) continue;
|
|
2525
|
+
let o = this.pairKey(t, a), s = this.touchingPairs.get(o);
|
|
2526
|
+
s ? s.count++ : this.touchingPairs.set(o, {
|
|
2527
|
+
count: 1,
|
|
2528
|
+
sensor: t.isSensor() || a.isSensor(),
|
|
2529
|
+
a: t,
|
|
2530
|
+
b: a
|
|
2531
|
+
});
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2206
2534
|
}
|
|
2207
2535
|
debugRender(e = {}) {
|
|
2208
2536
|
this.live();
|
|
@@ -2212,7 +2540,7 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
2212
2540
|
let c = o.ok(o.exports.debug_lines(this.handle, a, s.length)), l = o.io(5 * c).f, u = 1 / this.scale, d = new Float32Array(4 * c), f = new Uint8Array(c), p = new Float32Array(4 * c);
|
|
2213
2541
|
for (let e = 0; e < c; e++) {
|
|
2214
2542
|
for (let t = 0; t < 4; t++) d[4 * e + t] = l[5 * e + t] * u;
|
|
2215
|
-
f[e] = l[5 * e + 4], p.set(t?.[
|
|
2543
|
+
f[e] = l[5 * e + 4], p.set(t?.[H[f[e]]] ?? U[H[f[e]]], 4 * e);
|
|
2216
2544
|
}
|
|
2217
2545
|
return {
|
|
2218
2546
|
vertices: d,
|
|
@@ -2221,16 +2549,33 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
2221
2549
|
};
|
|
2222
2550
|
}
|
|
2223
2551
|
on(e, t) {
|
|
2224
|
-
if (this.disposed) return
|
|
2552
|
+
if (this.disposed) return W;
|
|
2225
2553
|
let n = this.listeners.get(e);
|
|
2226
2554
|
n || this.listeners.set(e, n = /* @__PURE__ */ new Set());
|
|
2227
2555
|
let r = n;
|
|
2228
|
-
|
|
2556
|
+
if (r.add(t), (e === "sleep" || e === "wake") && !this.asleep) {
|
|
2557
|
+
this.asleep = /* @__PURE__ */ new Map();
|
|
2558
|
+
for (let e of this.bodies) e.type === "dynamic" && this.asleep.set(e, e.sleeping);
|
|
2559
|
+
}
|
|
2560
|
+
return () => {
|
|
2229
2561
|
r.delete(t);
|
|
2230
2562
|
};
|
|
2231
2563
|
}
|
|
2564
|
+
sleepEvents(e) {
|
|
2565
|
+
if (!this.listeners.get("sleep")?.size && !this.listeners.get("wake")?.size) {
|
|
2566
|
+
this.asleep = null;
|
|
2567
|
+
return;
|
|
2568
|
+
}
|
|
2569
|
+
let t = this.asleep;
|
|
2570
|
+
for (let n of this.bodies) {
|
|
2571
|
+
if (n.type !== "dynamic") continue;
|
|
2572
|
+
let r = n.sleeping, i = t.get(n);
|
|
2573
|
+
i !== void 0 && i !== r && e.push([r ? "sleep" : "wake", { body: n }]), t.set(n, r);
|
|
2574
|
+
}
|
|
2575
|
+
for (let e of t.keys()) this.bodies.has(e) || t.delete(e);
|
|
2576
|
+
}
|
|
2232
2577
|
emit(e, t) {
|
|
2233
|
-
this.listeners.get(e)?.forEach(
|
|
2578
|
+
this.listeners.get(e)?.forEach(te, t);
|
|
2234
2579
|
}
|
|
2235
2580
|
dispose() {
|
|
2236
2581
|
if (this.disposed) return;
|
|
@@ -2245,8 +2590,8 @@ var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => impo
|
|
|
2245
2590
|
this.listeners.clear(), this.kernel.exports.world_destroy(this.handle), this.disposed = !0, this.engine = null;
|
|
2246
2591
|
}
|
|
2247
2592
|
};
|
|
2248
|
-
|
|
2249
|
-
function
|
|
2593
|
+
Q.prototype[Symbol.dispose] = Q.prototype.dispose;
|
|
2594
|
+
function ne(e) {
|
|
2250
2595
|
if (!e?.rect) return;
|
|
2251
2596
|
let t = Math.abs(e.rect[2] * (e.scaleX ?? 1)), n = Math.abs(e.rect[3] * (e.scaleY ?? 1));
|
|
2252
2597
|
if (t > 0 && n > 0) return {
|
|
@@ -2261,7 +2606,7 @@ function Q(e) {
|
|
|
2261
2606
|
var $ = (e, t) => {
|
|
2262
2607
|
if (!Number.isFinite(e)) throw RangeError(`${t} must be a finite number, got ${e}; fix the value.`);
|
|
2263
2608
|
return e;
|
|
2264
|
-
},
|
|
2609
|
+
}, re = class {
|
|
2265
2610
|
constructor({ kp: e = 1, ki: t = 0, kd: n = 0 } = {}) {
|
|
2266
2611
|
this.axes = [!0, !0], this.sum = [0, 0], this.angleSum = 0, this.kp = $(e, "kp"), this.ki = $(t, "ki"), this.kd = $(n, "kd");
|
|
2267
2612
|
}
|
|
@@ -2292,4 +2637,4 @@ var $ = (e, t) => {
|
|
|
2292
2637
|
}
|
|
2293
2638
|
};
|
|
2294
2639
|
//#endregion
|
|
2295
|
-
export { M as Articulation2D, A as Body2D,
|
|
2640
|
+
export { M as Articulation2D, A as Body2D, F as CharacterController2D, v as CoefficientCombineRule2D, k as Collider2D, z as Joint2D, Q as Physics2D, re as PidController2D, m as interactionGroups, f as tileMapColliders };
|