@margoengine/physics2d 0.0.0-stage → 0.1.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 -0
- package/NOTICE +4 -0
- package/README.md +521 -3
- package/dist/articulation.d.ts +103 -0
- package/dist/body.d.ts +376 -0
- package/dist/character.d.ts +86 -0
- package/dist/clock.d.ts +2 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +2295 -0
- package/dist/joint.d.ts +102 -0
- package/dist/kernel-D6zM0ozJ.js +159 -0
- package/dist/kernel.d.ts +102 -0
- package/dist/mesh-clean.d.ts +17 -0
- package/dist/outline.d.ts +55 -0
- package/dist/outline.js +222 -0
- package/dist/physics.d.ts +548 -0
- package/dist/physics2d-scalar.wasm +0 -0
- package/dist/physics2d.wasm +0 -0
- package/dist/pid.d.ts +36 -0
- package/dist/shapes.d.ts +208 -0
- package/dist/tilemap.d.ts +84 -0
- package/dist/vehicle.d.ts +121 -0
- package/dist/vehicle.js +178 -0
- package/dist/wasm-load.d.ts +21 -0
- package/package.json +60 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,2295 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region ../shared/errors.js
|
|
6
|
+
var t = (e, t) => Object.assign(Error(t), {
|
|
7
|
+
name: "MargoError",
|
|
8
|
+
code: e
|
|
9
|
+
});
|
|
10
|
+
function n(e) {
|
|
11
|
+
return !/^(Compile|Eval|Runtime)Error$/.test(e?.name) || !/wasm-unsafe-eval|content security policy|\bCSP\b|disallowed by embedder/i.test(e.message) ? e : Object.assign(t("failed", "Wasm compilation blocked by CSP; add 'wasm-unsafe-eval' to script-src; see https://margoengine.com/llms/docs/csp.md"), { cause: e });
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region ../shared/shared-load.js
|
|
15
|
+
var r = () => {}, i = class {
|
|
16
|
+
value = null;
|
|
17
|
+
#e;
|
|
18
|
+
#t = null;
|
|
19
|
+
constructor(e) {
|
|
20
|
+
this.#e = e;
|
|
21
|
+
}
|
|
22
|
+
#n(e) {
|
|
23
|
+
let t = new AbortController(), i = this.#e(e, t.signal).then((e) => this.value = e, (e) => {
|
|
24
|
+
throw n(e);
|
|
25
|
+
}).finally(() => {
|
|
26
|
+
this.#t?.promise === i && (this.#t = null);
|
|
27
|
+
});
|
|
28
|
+
return i.catch(r), {
|
|
29
|
+
promise: i,
|
|
30
|
+
controller: t,
|
|
31
|
+
waiting: 0
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async load(e, t) {
|
|
35
|
+
if (t?.throwIfAborted(), this.value !== null) return this.value;
|
|
36
|
+
let n = this.#t ??= this.#n(e);
|
|
37
|
+
if (!t) return n.waiting = Infinity, n.promise;
|
|
38
|
+
n.waiting++;
|
|
39
|
+
let r, i = new Promise((e, i) => {
|
|
40
|
+
r = () => {
|
|
41
|
+
--n.waiting === 0 && n.controller.abort(t.reason), i(t.reason);
|
|
42
|
+
}, t.addEventListener("abort", r, { once: !0 });
|
|
43
|
+
});
|
|
44
|
+
try {
|
|
45
|
+
return await Promise.race([n.promise, i]);
|
|
46
|
+
} finally {
|
|
47
|
+
t.aborted || n.waiting--, t.removeEventListener("abort", r);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/mesh-clean.ts
|
|
53
|
+
function a(e, t, n) {
|
|
54
|
+
let r = e.map((e) => [e[0], e[1]]), i = Array.from(t);
|
|
55
|
+
if (n.mergeDuplicateVertices) {
|
|
56
|
+
let e = /* @__PURE__ */ new Map(), t = [], n = [];
|
|
57
|
+
for (let [i, a] of r) {
|
|
58
|
+
let r = `${i + 0},${a + 0}`, o = Number.isNaN(i) || Number.isNaN(a) ? void 0 : e.get(r);
|
|
59
|
+
o === void 0 && (o = n.length, e.set(r, o), n.push([i + 0, a + 0])), t.push(o);
|
|
60
|
+
}
|
|
61
|
+
r = n, i = i.map((e) => t[e]);
|
|
62
|
+
}
|
|
63
|
+
let a = (e, t, n) => {
|
|
64
|
+
let [i, a, o] = [
|
|
65
|
+
r[e],
|
|
66
|
+
r[t],
|
|
67
|
+
r[n]
|
|
68
|
+
];
|
|
69
|
+
return (a[0] - i[0]) * (o[1] - i[1]) - (o[0] - i[0]) * (a[1] - i[1]);
|
|
70
|
+
}, o = (e) => {
|
|
71
|
+
let t = [];
|
|
72
|
+
for (let n = 0; n < i.length; n += 3) {
|
|
73
|
+
let [r, a, o] = [
|
|
74
|
+
i[n],
|
|
75
|
+
i[n + 1],
|
|
76
|
+
i[n + 2]
|
|
77
|
+
];
|
|
78
|
+
e(r, a, o) && t.push(r, a, o);
|
|
79
|
+
}
|
|
80
|
+
i = t;
|
|
81
|
+
};
|
|
82
|
+
if (n.deleteDegenerateTriangles && o((e, t, n) => e !== t && t !== n && e !== n && a(e, t, n) !== 0), n.deleteDuplicateTriangles) {
|
|
83
|
+
let e = /* @__PURE__ */ new Set();
|
|
84
|
+
o((t, n, r) => {
|
|
85
|
+
let i = [
|
|
86
|
+
t,
|
|
87
|
+
n,
|
|
88
|
+
r
|
|
89
|
+
].sort((e, t) => e - t).join(",");
|
|
90
|
+
return !e.has(i) && (e.add(i), !0);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (n.deleteBadTopologyTriangles) {
|
|
94
|
+
let e = /* @__PURE__ */ new Map(), t = (e, t) => e < t ? `${e},${t}` : `${t},${e}`;
|
|
95
|
+
o((n, r, i) => {
|
|
96
|
+
let a = [
|
|
97
|
+
t(n, r),
|
|
98
|
+
t(r, i),
|
|
99
|
+
t(i, n)
|
|
100
|
+
];
|
|
101
|
+
if (a.some((t) => (e.get(t) ?? 0) >= 2)) return !1;
|
|
102
|
+
for (let t of a) e.set(t, (e.get(t) ?? 0) + 1);
|
|
103
|
+
return !0;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
vertices: r,
|
|
108
|
+
triangles: i
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/tilemap.ts
|
|
113
|
+
var o = 2147483648, s = 1073741824, c = 536870912, l = 268435455;
|
|
114
|
+
function u(e, t, n) {
|
|
115
|
+
let r = (r, i) => r >= 0 && i >= 0 && r < t && i < n && e[i * t + r] === 1, i = /* @__PURE__ */ new Map(), a = t + 1, o = (e, t, n, r) => {
|
|
116
|
+
let o = t * a + e, s = i.get(o);
|
|
117
|
+
s ? s.push(r * a + n) : i.set(o, [r * a + n]);
|
|
118
|
+
};
|
|
119
|
+
for (let e = 0; e < n; e++) for (let n = 0; n < t; n++) r(n, e) && (r(n, e - 1) || o(n, e, n + 1, e), r(n + 1, e) || o(n + 1, e, n + 1, e + 1), r(n, e + 1) || o(n + 1, e + 1, n, e + 1), r(n - 1, e) || o(n, e + 1, n, e));
|
|
120
|
+
let s = [], c = (e) => [e % a, Math.floor(e / a)];
|
|
121
|
+
for (let [e] of i) for (; i.get(e)?.length;) {
|
|
122
|
+
let t = [e], n = e;
|
|
123
|
+
for (;;) {
|
|
124
|
+
let r = i.get(n);
|
|
125
|
+
if (!r?.length || (n = r.pop(), t.push(n), n === e && !i.get(n)?.length)) break;
|
|
126
|
+
}
|
|
127
|
+
let r = t.slice(0, -1).map(c), a = [];
|
|
128
|
+
for (let e = 0; e < r.length; e++) {
|
|
129
|
+
let t = r[(e + r.length - 1) % r.length], n = r[e], i = r[(e + 1) % r.length];
|
|
130
|
+
(n[0] - t[0]) * (i[1] - n[1]) !== (n[1] - t[1]) * (i[0] - n[0]) && a.push(n);
|
|
131
|
+
}
|
|
132
|
+
s.push(a);
|
|
133
|
+
}
|
|
134
|
+
return s;
|
|
135
|
+
}
|
|
136
|
+
function d(e) {
|
|
137
|
+
if (e.kind === "point") return null;
|
|
138
|
+
let t = (e.rotation ?? 0) * Math.PI / 180, n = Math.cos(t), r = Math.sin(t), i = (t, i) => [e.x + t * n - i * r, e.y + t * r + i * n];
|
|
139
|
+
if (e.kind === "rect") {
|
|
140
|
+
let { width: t, height: n } = e;
|
|
141
|
+
return {
|
|
142
|
+
points: [
|
|
143
|
+
i(0, 0),
|
|
144
|
+
i(t, 0),
|
|
145
|
+
i(t, n),
|
|
146
|
+
i(0, n)
|
|
147
|
+
],
|
|
148
|
+
box: !0
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (e.kind === "ellipse") {
|
|
152
|
+
let { width: t, height: n } = e;
|
|
153
|
+
if (t === n) return {
|
|
154
|
+
points: [],
|
|
155
|
+
box: !1,
|
|
156
|
+
circle: {
|
|
157
|
+
c: i(t / 2, n / 2),
|
|
158
|
+
r: t / 2
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
let r = [];
|
|
162
|
+
for (let e = 0; e < 16; e++) {
|
|
163
|
+
let a = e / 16 * Math.PI * 2;
|
|
164
|
+
r.push(i(t / 2 + t / 2 * Math.cos(a), n / 2 + n / 2 * Math.sin(a)));
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
points: r,
|
|
168
|
+
box: !1
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
points: e.points.map((e) => i(e[0], e[1])),
|
|
173
|
+
box: !1
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function f(e, t = {}) {
|
|
177
|
+
let { tileWidth: n, tileHeight: r } = e;
|
|
178
|
+
if (!(n > 0 && r > 0)) throw RangeError(`map tile size must be above 0, got ${n} x ${r}; fix tileWidth and tileHeight.`);
|
|
179
|
+
let [i, a] = t.position ?? [0, 0], f = t.layers, p = (e) => f ? typeof f == "function" ? f(e) : f.includes(e.name) : !0, m = {
|
|
180
|
+
friction: t.friction,
|
|
181
|
+
restitution: t.restitution,
|
|
182
|
+
collisionGroups: t.collisionGroups
|
|
183
|
+
}, h = [], g = /* @__PURE__ */ new Map(), _ = /* @__PURE__ */ new Map(), v = (t) => {
|
|
184
|
+
let n = _.get(t);
|
|
185
|
+
if (n) return n;
|
|
186
|
+
let r;
|
|
187
|
+
for (let n of e.tilesets) n.firstGid <= t && t < n.firstGid + n.tileCount && (!r || n.firstGid > r.firstGid) && (r = n);
|
|
188
|
+
if (!r) return null;
|
|
189
|
+
let i = r.tiles?.find((e) => e.id === t - r.firstGid), a = i?.collision?.length ? i.collision : i?.properties?.solid === !0 ? [{
|
|
190
|
+
kind: "rect",
|
|
191
|
+
x: 0,
|
|
192
|
+
y: 0,
|
|
193
|
+
width: r.tileWidth,
|
|
194
|
+
height: r.tileHeight
|
|
195
|
+
}] : null;
|
|
196
|
+
return _.set(t, n = {
|
|
197
|
+
set: r,
|
|
198
|
+
shapes: a
|
|
199
|
+
}), n;
|
|
200
|
+
};
|
|
201
|
+
for (let t of e.layers) {
|
|
202
|
+
if (t.type !== "tile" || !p(t)) continue;
|
|
203
|
+
if (t.data.length !== t.width * t.height) throw RangeError(`layer "${t.name}" has ${t.data.length} values for ${t.width} x ${t.height} tiles; resize it to width * height.`);
|
|
204
|
+
let [u, f] = t.offset ?? [0, 0], _ = `${u},${f}`, y = g.get(_);
|
|
205
|
+
if (!y) {
|
|
206
|
+
let t = Math.max(0, ...e.layers.map((e) => e.type === "tile" ? e.width : 0)), n = Math.max(0, ...e.layers.map((e) => e.type === "tile" ? e.height : 0));
|
|
207
|
+
g.set(_, y = {
|
|
208
|
+
solid: new Uint8Array(t * n),
|
|
209
|
+
width: t,
|
|
210
|
+
height: n,
|
|
211
|
+
ox: u,
|
|
212
|
+
oy: f
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
for (let e = 0; e < t.height; e++) for (let p = 0; p < t.width; p++) {
|
|
216
|
+
let g = t.data[e * t.width + p], _ = g & l;
|
|
217
|
+
if (!_) continue;
|
|
218
|
+
let b = v(_);
|
|
219
|
+
if (!b?.shapes) continue;
|
|
220
|
+
let { set: x } = b, S = x.tileWidth, C = x.tileHeight, w = i + u + p * n + (x.tileOffset?.[0] ?? 0), T = a + f + (e + 1) * r - C + (x.tileOffset?.[1] ?? 0), E = (e) => {
|
|
221
|
+
let [t, n] = e;
|
|
222
|
+
return g & c && ([t, n] = [n * S / C, t * C / S]), g & o && (t = S - t), g & s && (n = C - n), [t, n];
|
|
223
|
+
};
|
|
224
|
+
for (let t of b.shapes) {
|
|
225
|
+
let o = d(t);
|
|
226
|
+
if (!o) continue;
|
|
227
|
+
if (o.circle) {
|
|
228
|
+
let [e, t] = E(o.circle.c);
|
|
229
|
+
h.push({
|
|
230
|
+
shape: "circle",
|
|
231
|
+
radius: o.circle.r,
|
|
232
|
+
offset: [w + e, T + t],
|
|
233
|
+
...m
|
|
234
|
+
});
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
let s = o.points.map(E);
|
|
238
|
+
if (o.box) {
|
|
239
|
+
let t = s.map((e) => e[0]), o = s.map((e) => e[1]), c = Math.min(...t), l = Math.max(...t), d = Math.min(...o), g = Math.max(...o);
|
|
240
|
+
if (s.every((e) => (e[0] === c || e[0] === l) && (e[1] === d || e[1] === g))) {
|
|
241
|
+
c === 0 && d === 0 && l === n && g === r && S === n && C === r && T === a + f + e * r && w === i + u + p * n ? y.solid[e * y.width + p] = 1 : h.push({
|
|
242
|
+
shape: "box",
|
|
243
|
+
width: l - c,
|
|
244
|
+
height: g - d,
|
|
245
|
+
offset: [w + (c + l) / 2, T + (d + g) / 2],
|
|
246
|
+
...m
|
|
247
|
+
});
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
let c = s.map((e) => [w + e[0], T + e[1]]);
|
|
252
|
+
t.kind === "polyline" ? h.push({
|
|
253
|
+
shape: "polyline",
|
|
254
|
+
points: c,
|
|
255
|
+
...m
|
|
256
|
+
}) : h.push({
|
|
257
|
+
shape: "polygon",
|
|
258
|
+
points: c,
|
|
259
|
+
...m
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (let e of g.values()) for (let t of u(e.solid, e.width, e.height)) {
|
|
265
|
+
let o = t.map((t) => [i + e.ox + t[0] * n, a + e.oy + t[1] * r]);
|
|
266
|
+
o.push(o[0]), h.push({
|
|
267
|
+
shape: "polyline",
|
|
268
|
+
points: o,
|
|
269
|
+
...m
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
return h;
|
|
273
|
+
}
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/shapes.ts
|
|
276
|
+
var p = (e) => {
|
|
277
|
+
if (typeof e == "number") {
|
|
278
|
+
if (!Number.isInteger(e) || e < 0 || e > 65535) throw RangeError(`A group mask must be an integer 0..65535, got ${e}; or pass an array of group indices.`);
|
|
279
|
+
return e;
|
|
280
|
+
}
|
|
281
|
+
return e.reduce((e, t) => {
|
|
282
|
+
if (!Number.isInteger(t) || t < 0 || t > 15) throw RangeError(`A group index must be an integer 0..15, got ${t}; there are 16 groups.`);
|
|
283
|
+
return e | 1 << t;
|
|
284
|
+
}, 0);
|
|
285
|
+
};
|
|
286
|
+
function m(e, t = 65535) {
|
|
287
|
+
return (p(e) << 16 | p(t)) >>> 0;
|
|
288
|
+
}
|
|
289
|
+
var h = (e, t, n = !1) => {
|
|
290
|
+
if (!Number.isFinite(e) || n && e <= 0) throw RangeError(`${t} must be a finite number${n ? " above 0" : ""}, got ${e}; fix the value.`);
|
|
291
|
+
return e;
|
|
292
|
+
}, g = (e) => {
|
|
293
|
+
if (e === void 0) return 0;
|
|
294
|
+
if (!Number.isFinite(e) || e < 0) throw RangeError(`borderRadius must be a finite number of 0 or more, got ${e}.`);
|
|
295
|
+
return e;
|
|
296
|
+
}, _ = {
|
|
297
|
+
circle: 0,
|
|
298
|
+
capsule: 1,
|
|
299
|
+
segment: 2,
|
|
300
|
+
rect: 3,
|
|
301
|
+
polygon: 4,
|
|
302
|
+
chain: 5,
|
|
303
|
+
decomposed: 6,
|
|
304
|
+
voxels: 7
|
|
305
|
+
}, v = {
|
|
306
|
+
Average: 0,
|
|
307
|
+
Min: 1,
|
|
308
|
+
Multiply: 2,
|
|
309
|
+
Max: 3,
|
|
310
|
+
ClampedSum: 4,
|
|
311
|
+
GeometricMean: 5
|
|
312
|
+
};
|
|
313
|
+
function y(e, t) {
|
|
314
|
+
if (e === void 0) return 0;
|
|
315
|
+
if (!(Number.isInteger(e) && e >= 0 && e <= 5)) throw RangeError(`A collider's ${t} combine rule is a CoefficientCombineRule2D (Average, Min, Multiply, Max, ClampedSum or GeometricMean; got ${e}); pass one.`);
|
|
316
|
+
return e;
|
|
317
|
+
}
|
|
318
|
+
var b = class {
|
|
319
|
+
constructor(e) {
|
|
320
|
+
let t = /* @__PURE__ */ new ArrayBuffer(e * 4);
|
|
321
|
+
this.u = new Uint32Array(t), this.f = new Float32Array(t);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
function x(e) {
|
|
325
|
+
let t = 0, n = e.length / 2;
|
|
326
|
+
for (let r = 0; r < n; r++) {
|
|
327
|
+
let i = (r + 1) % n;
|
|
328
|
+
t += e[2 * r] * e[2 * i + 1] - e[2 * i] * e[2 * r + 1];
|
|
329
|
+
}
|
|
330
|
+
return Math.abs(t) / 2;
|
|
331
|
+
}
|
|
332
|
+
function S(e, t) {
|
|
333
|
+
let [n, r] = e.offset ?? [0, 0], i = e.rotation ?? 0;
|
|
334
|
+
h(n, "collider offset x"), h(r, "collider offset y"), h(i, "collider rotation");
|
|
335
|
+
let a = Math.cos(i), o = Math.sin(i), s = (e, i) => [(a * e - o * i + n) * t, (o * e + a * i + r) * t], c, l, u = 0, d = !1, f = !0;
|
|
336
|
+
switch (e.shape) {
|
|
337
|
+
case "box": {
|
|
338
|
+
let i = h(e.width, "box width", !0) * t / 2, s = h(e.height, "box height", !0) * t / 2, d = g(e.borderRadius) * t;
|
|
339
|
+
c = _.rect, l = new b(7), l.f.set([
|
|
340
|
+
i,
|
|
341
|
+
s,
|
|
342
|
+
n * t,
|
|
343
|
+
r * t,
|
|
344
|
+
a,
|
|
345
|
+
o,
|
|
346
|
+
d
|
|
347
|
+
]), u = 4 * i * s + 2 * d * (2 * i + 2 * s) + Math.PI * d * d;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
case "circle": {
|
|
351
|
+
let i = h(e.radius, "circle radius", !0) * t;
|
|
352
|
+
c = _.circle, l = new b(3), l.f.set([
|
|
353
|
+
n * t,
|
|
354
|
+
r * t,
|
|
355
|
+
i
|
|
356
|
+
]), u = Math.PI * i * i;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
case "capsule": {
|
|
360
|
+
let n = h(e.radius, "capsule radius", !0), r = Math.max(0, h(e.height, "capsule height", !0) / 2 - n), i = s(0, -r), a = s(0, r);
|
|
361
|
+
c = _.capsule, l = new b(5), l.f.set([
|
|
362
|
+
i[0],
|
|
363
|
+
i[1],
|
|
364
|
+
a[0],
|
|
365
|
+
a[1],
|
|
366
|
+
n * t
|
|
367
|
+
]), u = Math.PI * n * n * t * t + 4 * r * n * t * t;
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case "segment": {
|
|
371
|
+
let t = s(h(e.from[0], "segment from"), h(e.from[1], "segment from")), n = s(h(e.to[0], "segment to"), h(e.to[1], "segment to"));
|
|
372
|
+
c = _.segment, l = new b(4), l.f.set([
|
|
373
|
+
t[0],
|
|
374
|
+
t[1],
|
|
375
|
+
n[0],
|
|
376
|
+
n[1]
|
|
377
|
+
]);
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
case "polyline": {
|
|
381
|
+
let t = C(e.points, 1, 2, "polyline"), n = t.length / 2, r = n >= 4 && t[0] === t[2 * n - 2] && t[1] === t[2 * n - 1], i = r ? n - 1 : n;
|
|
382
|
+
c = _.chain, d = e.oneSided === !0, l = new b(2 + 2 * i), l.u[0] = +!!r, l.u[1] = i;
|
|
383
|
+
for (let e = 0; e < i; e++) l.f.set(s(t[2 * e], t[2 * e + 1]), 2 + 2 * e);
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "polygon": {
|
|
387
|
+
let n = C(e.points, 1, 3, "polygon"), r = n.length / 2, i = new Float32Array(2 * r);
|
|
388
|
+
for (let e = 0; e < r; e++) i.set(s(n[2 * e], n[2 * e + 1]), 2 * e);
|
|
389
|
+
let a = g(e.borderRadius) * t, o = 0;
|
|
390
|
+
for (let e = 0; e < r; e++) o += Math.hypot(i[2 * e] - i[2 * ((e + 1) % r)], i[2 * e + 1] - i[2 * ((e + 1) % r) + 1]);
|
|
391
|
+
if (u = x(i) + o * a + Math.PI * a * a, !(x(n) > 1e-9)) throw RangeError("polygon points are degenerate (no area); give at least three points not on a line.");
|
|
392
|
+
f = r <= 8 && w(e.points), c = f ? _.polygon : _.decomposed, l = new b(2 + 2 * r), l.f[0] = a, l.u[1] = r, l.f.set(i, 2);
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
default: throw TypeError(`Unknown collider shape "${e.shape}"; use box, circle, capsule, segment, triangle, polygon, polyline, compound, trimesh, heightfield, halfspace or voxels.`);
|
|
396
|
+
}
|
|
397
|
+
let p = new b(8), m = e.sensor === !0, v = (e.events === !1 ? 0 : 1) | (e.contactForceThreshold === void 0 ? 0 : 2), S = e.density ?? 1;
|
|
398
|
+
e.mass !== void 0 && (S = u > 0 ? h(e.mass, "collider mass") / u : 0), p.f[0] = h(S, "collider density"), p.f[1] = h(e.friction ?? .5, "collider friction"), p.f[2] = h(e.restitution ?? 0, "collider restitution"), p.u[3] = e.collisionGroups ?? 4294967295, p.u[4] = e.solverGroups ?? 4294967295;
|
|
399
|
+
let T = e.activeCollisionTypes ?? (m ? 63 : 7);
|
|
400
|
+
p.u[5] = v | (T & 255) << 8 | (m ? 65536 : 0) | (d ? 1 << 17 : 0) | (e.hooks?.filterPair ? 1 << 18 : 0) | (e.hooks?.modifyContact ? 1 << 19 : 0) | (e.hooks?.filterIntersection ? 1 << 20 : 0) | y(e.frictionCombineRule, "friction") << 21 | y(e.restitutionCombineRule, "restitution") << 24, p.f[6] = e.contactForceThreshold ?? 0;
|
|
401
|
+
let E = e.contactSkin ?? 0;
|
|
402
|
+
if (!(E >= 0 && Number.isFinite(E))) throw RangeError(`A collider's contact skin is a finite distance of 0 or more (got ${E}); pass one.`);
|
|
403
|
+
return p.f[7] = E * t, {
|
|
404
|
+
kind: c,
|
|
405
|
+
settings: p.u,
|
|
406
|
+
geometry: l.u,
|
|
407
|
+
area: u,
|
|
408
|
+
convex: f
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
function C(e, t, n, r) {
|
|
412
|
+
if (!Array.isArray(e) || e.length < n) throw TypeError(`${r} needs at least ${n} points as [x, y] pairs; pass more points.`);
|
|
413
|
+
let i = new Float32Array(e.length * 2);
|
|
414
|
+
return e.forEach((e, n) => {
|
|
415
|
+
i[2 * n] = h(e[0], `${r} x`) * t, i[2 * n + 1] = h(e[1], `${r} y`) * t;
|
|
416
|
+
}), i;
|
|
417
|
+
}
|
|
418
|
+
function w(e) {
|
|
419
|
+
let t = 0, n = e.length;
|
|
420
|
+
for (let r = 0; r < n; r++) {
|
|
421
|
+
let i = e[r], a = e[(r + 1) % n], o = e[(r + 2) % n], s = (a[0] - i[0]) * (o[1] - a[1]) - (a[1] - i[1]) * (o[0] - a[0]);
|
|
422
|
+
if (s !== 0) {
|
|
423
|
+
if (t !== 0 && Math.sign(s) !== t) return !1;
|
|
424
|
+
t = Math.sign(s);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return !0;
|
|
428
|
+
}
|
|
429
|
+
var T = 1e5;
|
|
430
|
+
function E(e, t) {
|
|
431
|
+
let n = D(e, t);
|
|
432
|
+
if (e.mass !== void 0 && n.length > 1) {
|
|
433
|
+
let t = n.reduce((e, t) => e + t.area, 0);
|
|
434
|
+
for (let r of n) new Float32Array(r.settings.buffer)[0] = t > 0 ? e.mass / t : 0;
|
|
435
|
+
}
|
|
436
|
+
return n;
|
|
437
|
+
}
|
|
438
|
+
function D(e, t) {
|
|
439
|
+
let [n, r] = e.offset ?? [0, 0], i = e.rotation ?? 0, o = (t, a = [n, r], o = i) => ({
|
|
440
|
+
...e,
|
|
441
|
+
...t,
|
|
442
|
+
offset: a,
|
|
443
|
+
rotation: o
|
|
444
|
+
});
|
|
445
|
+
switch (e.shape) {
|
|
446
|
+
case "triangle": return [S(o({
|
|
447
|
+
shape: "polygon",
|
|
448
|
+
points: [
|
|
449
|
+
e.a,
|
|
450
|
+
e.b,
|
|
451
|
+
e.c
|
|
452
|
+
],
|
|
453
|
+
borderRadius: e.borderRadius
|
|
454
|
+
}), t)];
|
|
455
|
+
case "compound": {
|
|
456
|
+
if (!Array.isArray(e.shapes) || e.shapes.length === 0) throw TypeError("A compound shape needs a non-empty shapes list; pass at least one part.");
|
|
457
|
+
let a = Math.cos(i), s = Math.sin(i), c = [];
|
|
458
|
+
for (let l of e.shapes) {
|
|
459
|
+
let [e, u] = l.offset ?? [0, 0];
|
|
460
|
+
c.push(...D(o(l, [n + a * e - s * u, r + s * e + a * u], i + (l.rotation ?? 0)), t));
|
|
461
|
+
}
|
|
462
|
+
return c;
|
|
463
|
+
}
|
|
464
|
+
case "trimesh": {
|
|
465
|
+
let { vertices: n, indices: r } = e;
|
|
466
|
+
if (!Array.isArray(n) || !r || r.length === 0 || r.length % 3 != 0) throw TypeError("A trimesh needs vertices and an index list with three indices to a triangle.");
|
|
467
|
+
for (let e = 0; e < r.length; e++) {
|
|
468
|
+
let t = r[e];
|
|
469
|
+
if (!Number.isInteger(t) || t < 0 || t >= n.length) throw RangeError(`trimesh index ${t} is outside its ${n.length} vertices; fix the indices.`);
|
|
470
|
+
}
|
|
471
|
+
let { vertices: i, triangles: s } = a(n, r, e), c = [];
|
|
472
|
+
for (let n = 0; n < s.length; n += 3) {
|
|
473
|
+
let r = [
|
|
474
|
+
i[s[n]],
|
|
475
|
+
i[s[n + 1]],
|
|
476
|
+
i[s[n + 2]]
|
|
477
|
+
];
|
|
478
|
+
if (Math.abs((r[1][0] - r[0][0]) * (r[2][1] - r[0][1]) - (r[2][0] - r[0][0]) * (r[1][1] - r[0][1])) > 1e-9) {
|
|
479
|
+
let n = S(o({
|
|
480
|
+
shape: "polygon",
|
|
481
|
+
points: r
|
|
482
|
+
}), t);
|
|
483
|
+
e.fixInternalEdges && (n.seam = !0), c.push(n);
|
|
484
|
+
} else if (!e.deleteDegenerateTriangles) {
|
|
485
|
+
let [e, n, i] = [
|
|
486
|
+
r[0],
|
|
487
|
+
r[1],
|
|
488
|
+
-1
|
|
489
|
+
];
|
|
490
|
+
for (let [t, a] of [
|
|
491
|
+
[r[0], r[1]],
|
|
492
|
+
[r[1], r[2]],
|
|
493
|
+
[r[2], r[0]]
|
|
494
|
+
]) {
|
|
495
|
+
let r = Math.hypot(a[0] - t[0], a[1] - t[1]);
|
|
496
|
+
r > i && ([e, n, i] = [
|
|
497
|
+
t,
|
|
498
|
+
a,
|
|
499
|
+
r
|
|
500
|
+
]);
|
|
501
|
+
}
|
|
502
|
+
i > 0 && c.push(S(o({
|
|
503
|
+
shape: "segment",
|
|
504
|
+
from: e,
|
|
505
|
+
to: n
|
|
506
|
+
}), t));
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
if (!c.length) throw RangeError("trimesh has no triangle left with an area or a length; give it triangles whose corners are not all one point.");
|
|
510
|
+
return c;
|
|
511
|
+
}
|
|
512
|
+
case "heightfield": {
|
|
513
|
+
let { heights: n, width: r } = e, i = n?.length ?? 0;
|
|
514
|
+
if (i < 2) throw TypeError("A heightfield needs at least two heights.");
|
|
515
|
+
h(r, "heightfield width", !0);
|
|
516
|
+
let a = [];
|
|
517
|
+
for (let e = 0; e < i; e++) a.push([-r / 2 + e * r / (i - 1), h(n[e], "heightfield height")]);
|
|
518
|
+
return [S(o({
|
|
519
|
+
shape: "polyline",
|
|
520
|
+
points: a,
|
|
521
|
+
oneSided: !1
|
|
522
|
+
}), t)];
|
|
523
|
+
}
|
|
524
|
+
case "halfspace": {
|
|
525
|
+
let [n, r] = e.normal ?? [0, -1], i = Math.hypot(n, r);
|
|
526
|
+
if (!(i > 0) || !Number.isFinite(i)) throw RangeError("halfspace normal must be a finite vector that is not zero.");
|
|
527
|
+
let a = [n / i, r / i], s = [-a[1], a[0]], c = T, l = (e, t) => [s[0] * e + a[0] * t, s[1] * e + a[1] * t];
|
|
528
|
+
return [S(o({
|
|
529
|
+
shape: "polygon",
|
|
530
|
+
points: [
|
|
531
|
+
l(c, 0),
|
|
532
|
+
l(-1e5, 0),
|
|
533
|
+
l(-1e5, -2 * c),
|
|
534
|
+
l(c, -2 * c)
|
|
535
|
+
]
|
|
536
|
+
}), t)];
|
|
537
|
+
}
|
|
538
|
+
case "voxels": {
|
|
539
|
+
let { cellWidth: a, cellHeight: s, columns: c, rows: l, solid: d } = e;
|
|
540
|
+
if (h(a, "voxels cellWidth", !0), h(s, "voxels cellHeight", !0), !Number.isInteger(c) || !Number.isInteger(l) || c < 1 || l < 1) throw RangeError(`voxels columns and rows must be positive integers, got ${c} x ${l}.`);
|
|
541
|
+
if (!d || d.length !== c * l) throw RangeError(`voxels has ${d?.length} values for ${c} x ${l} cells; give columns * rows values.`);
|
|
542
|
+
let f = new Uint8Array(c * l);
|
|
543
|
+
for (let e = 0; e < f.length; e++) f[e] = +!!d[e];
|
|
544
|
+
let p = u(f, c, l);
|
|
545
|
+
if (!p.length) throw RangeError("voxels has no solid cell; fill at least one.");
|
|
546
|
+
let m = p.map((e) => {
|
|
547
|
+
let n = e.map((e) => [e[0] * a, e[1] * s]);
|
|
548
|
+
return n.push(n[0]), S(o({
|
|
549
|
+
shape: "polyline",
|
|
550
|
+
points: n
|
|
551
|
+
}), t);
|
|
552
|
+
});
|
|
553
|
+
if (i !== 0) throw RangeError("A voxels collider cannot be rotated; turn the body instead.");
|
|
554
|
+
let g = Math.ceil(c * l / 32), v = new b(15 + g);
|
|
555
|
+
v.u[0] = c, v.u[1] = l, v.f.set([
|
|
556
|
+
a * t,
|
|
557
|
+
s * t,
|
|
558
|
+
n * t,
|
|
559
|
+
r * t
|
|
560
|
+
], 2), v.u[6] = g;
|
|
561
|
+
for (let e = 0; e < f.length; e++) f[e] && (v.u[7 + (e >> 5)] |= 1 << (e & 31));
|
|
562
|
+
let y = S(o({
|
|
563
|
+
shape: "box",
|
|
564
|
+
width: 1,
|
|
565
|
+
height: 1
|
|
566
|
+
}), t);
|
|
567
|
+
return m.push({
|
|
568
|
+
...y,
|
|
569
|
+
kind: _.voxels,
|
|
570
|
+
geometry: v.u.subarray(0, 7 + g),
|
|
571
|
+
area: 0,
|
|
572
|
+
convex: !1
|
|
573
|
+
}), m;
|
|
574
|
+
}
|
|
575
|
+
default: return [S(e, t)];
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
//#endregion
|
|
579
|
+
//#region src/body.ts
|
|
580
|
+
var O = (e, t) => {
|
|
581
|
+
if (!Number.isFinite(e)) throw RangeError(`${t} must be a finite number, got ${e}; fix the value.`);
|
|
582
|
+
return e;
|
|
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;
|
|
586
|
+
}
|
|
587
|
+
get handle() {
|
|
588
|
+
return this.shapes[0];
|
|
589
|
+
}
|
|
590
|
+
parent() {
|
|
591
|
+
return this.body;
|
|
592
|
+
}
|
|
593
|
+
get isRemoved() {
|
|
594
|
+
return this.removed;
|
|
595
|
+
}
|
|
596
|
+
markRemoved() {
|
|
597
|
+
this.removed = !0;
|
|
598
|
+
}
|
|
599
|
+
push() {
|
|
600
|
+
if (this.removed) throw new e("state", "Collider2D was removed; add a new collider with Body2D.addCollider.");
|
|
601
|
+
let { kernel: t, handle: n } = this.owner;
|
|
602
|
+
for (let e of this.shapes) t.io(8).u.set(this.settings), t.ok(t.exports.shape_set(n, e));
|
|
603
|
+
}
|
|
604
|
+
setShape(t) {
|
|
605
|
+
if (this.removed) throw new e("state", "Collider2D was removed; add a new collider with Body2D.addCollider.");
|
|
606
|
+
let { kernel: n, handle: r, scale: i } = this.owner, a = E({
|
|
607
|
+
...t,
|
|
608
|
+
offset: this.offset,
|
|
609
|
+
rotation: this.turn
|
|
610
|
+
}, i);
|
|
611
|
+
if (this.shapes.length !== 1 || a.length !== 1 || a[0].kind > 4) throw RangeError("setShape changes one convex outline (circle, box, capsule, segment, triangle or convex polygon); remove this collider and add a new one for any other.");
|
|
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 = {
|
|
613
|
+
...a[0],
|
|
614
|
+
settings: this.settings
|
|
615
|
+
}, this.options &&= {
|
|
616
|
+
...this.options,
|
|
617
|
+
...t
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
isSensor() {
|
|
621
|
+
return !!(this.settings[5] & 65536);
|
|
622
|
+
}
|
|
623
|
+
setSensor(e) {
|
|
624
|
+
e !== this.isSensor() && (this.settings[5] = e ? this.settings[5] | 65536 : this.settings[5] & -65537, this.settings[5] = this.settings[5] & -65281 | (e ? 63 : 7) << 8, this.push());
|
|
625
|
+
}
|
|
626
|
+
setActiveHooks(e) {
|
|
627
|
+
let t = (e.filterPair ? 1 << 18 : 0) | (e.modifyContact ? 1 << 19 : 0) | (e.filterIntersection ? 1 << 20 : 0);
|
|
628
|
+
this.settings[5] = this.settings[5] & -1835009 | t, this.push();
|
|
629
|
+
}
|
|
630
|
+
get activeHooks() {
|
|
631
|
+
let e = this.settings[5];
|
|
632
|
+
return {
|
|
633
|
+
filterPair: !!(e & 1 << 18),
|
|
634
|
+
filterIntersection: !!(e & 1 << 20),
|
|
635
|
+
modifyContact: !!(e & 1 << 19)
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
setFriction(e) {
|
|
639
|
+
new Float32Array(this.settings.buffer)[1] = O(e, "friction"), this.push();
|
|
640
|
+
}
|
|
641
|
+
setRestitution(e) {
|
|
642
|
+
new Float32Array(this.settings.buffer)[2] = O(e, "restitution"), this.push();
|
|
643
|
+
}
|
|
644
|
+
setFrictionCombineRule(e) {
|
|
645
|
+
this.settings[5] = this.settings[5] & -14680065 | y(e, "friction") << 21, this.push();
|
|
646
|
+
}
|
|
647
|
+
frictionCombineRule() {
|
|
648
|
+
return this.settings[5] >>> 21 & 7;
|
|
649
|
+
}
|
|
650
|
+
setRestitutionCombineRule(e) {
|
|
651
|
+
this.settings[5] = this.settings[5] & -117440513 | y(e, "restitution") << 24, this.push();
|
|
652
|
+
}
|
|
653
|
+
restitutionCombineRule() {
|
|
654
|
+
return this.settings[5] >>> 24 & 7;
|
|
655
|
+
}
|
|
656
|
+
setDensity(e) {
|
|
657
|
+
new Float32Array(this.settings.buffer)[0] = O(e, "density"), this.push(), this.massProps = null;
|
|
658
|
+
let { kernel: t, handle: n } = this.owner;
|
|
659
|
+
for (let e of this.shapes) t.ok(t.exports.shape_set_mass(n, e, 0));
|
|
660
|
+
}
|
|
661
|
+
setMass(e) {
|
|
662
|
+
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
|
+
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) => {
|
|
665
|
+
let s = t.io(1);
|
|
666
|
+
s.f[0] = i > 0 ? e * r[o] / i : e / this.shapes.length, t.ok(t.exports.shape_set_mass(n, a, 1));
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
setMassProperties(e, t = [0, 0], n = 0) {
|
|
670
|
+
this.live();
|
|
671
|
+
for (let [r, i] of [
|
|
672
|
+
[e, "mass"],
|
|
673
|
+
[n, "inertia"],
|
|
674
|
+
[t?.[0], "centre x"],
|
|
675
|
+
[t?.[1], "centre y"]
|
|
676
|
+
]) if (!Number.isFinite(r)) throw RangeError(`A collider's ${i} is a finite number (got ${r}); pass one.`);
|
|
677
|
+
if (e < 0 || n < 0) throw RangeError(`A collider's mass and inertia are 0 or more (got ${e}, ${n}); pass them.`);
|
|
678
|
+
let { kernel: r, handle: i, scale: a } = this.owner, o = Math.cos(this.turn), s = Math.sin(this.turn);
|
|
679
|
+
this.massProps = {
|
|
680
|
+
mass: e,
|
|
681
|
+
centre: [t[0], t[1]],
|
|
682
|
+
inertia: n
|
|
683
|
+
}, this.shapes.forEach((c, l) => {
|
|
684
|
+
let u = r.io(4);
|
|
685
|
+
u.f[0] = l === 0 ? e : 0, u.f[1] = (this.offset[0] + o * t[0] - s * t[1]) * a, u.f[2] = (this.offset[1] + s * t[0] + o * t[1]) * a, u.f[3] = l === 0 ? n * a * a : 0, r.ok(r.exports.shape_set_mass(i, c, 2));
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
mass() {
|
|
689
|
+
this.live();
|
|
690
|
+
let { kernel: e, handle: t } = this.owner, n = 0;
|
|
691
|
+
for (let r of this.shapes) e.ok(e.exports.shape_mass(t, r)), n += e.io(5).f[0];
|
|
692
|
+
return n;
|
|
693
|
+
}
|
|
694
|
+
live() {
|
|
695
|
+
if (this.removed) throw new e("state", "Collider2D was removed; add a new collider with Body2D.addCollider.");
|
|
696
|
+
}
|
|
697
|
+
setActiveCollisionTypes(e) {
|
|
698
|
+
if (!Number.isInteger(e) || e < 0 || e > 63) throw RangeError(`activeCollisionTypes must be a whole number from 0 to 63, the six pair kinds as bits (got ${e}); use 7 for the default.`);
|
|
699
|
+
this.settings[5] = this.settings[5] & -65281 | e << 8, this.push();
|
|
700
|
+
}
|
|
701
|
+
activeCollisionTypes() {
|
|
702
|
+
return this.settings[5] >>> 8 & 255;
|
|
703
|
+
}
|
|
704
|
+
setActiveEvents(e) {
|
|
705
|
+
let t = this.settings[5];
|
|
706
|
+
e.collision !== void 0 && (t = e.collision ? t | 1 : t & -2), e.contactForce !== void 0 && (t = e.contactForce ? t | 2 : t & -3), this.settings[5] = t, this.push();
|
|
707
|
+
}
|
|
708
|
+
activeEvents() {
|
|
709
|
+
return {
|
|
710
|
+
collision: !!(this.settings[5] & 1),
|
|
711
|
+
contactForce: !!(this.settings[5] & 2)
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
setContactSkin(e) {
|
|
715
|
+
if (!(e >= 0) || !Number.isFinite(e)) throw RangeError(`A collider's contact skin is a finite distance of 0 or more (got ${e}); pass one.`);
|
|
716
|
+
new Float32Array(this.settings.buffer)[7] = e * this.owner.scale, this.push();
|
|
717
|
+
}
|
|
718
|
+
contactSkin() {
|
|
719
|
+
return new Float32Array(this.settings.buffer)[7] / this.owner.scale;
|
|
720
|
+
}
|
|
721
|
+
setContactForceEventThreshold(e) {
|
|
722
|
+
if (!(e >= 0) || !Number.isFinite(e)) throw RangeError(`The contact force threshold must be a finite number of newtons, 0 or more (got ${e}).`);
|
|
723
|
+
new Float32Array(this.settings.buffer)[6] = e, this.settings[5] |= 2, this.push();
|
|
724
|
+
}
|
|
725
|
+
setCollisionGroups(e) {
|
|
726
|
+
this.enabled ? this.settings[3] = e >>> 0 : this.savedGroups = e >>> 0, this.push();
|
|
727
|
+
}
|
|
728
|
+
setSolverGroups(e) {
|
|
729
|
+
this.settings[4] = e >>> 0, this.push();
|
|
730
|
+
}
|
|
731
|
+
setEnabled(e) {
|
|
732
|
+
e !== this.enabled && (this.enabled = e, e ? this.settings[3] = this.savedGroups : (this.savedGroups = this.settings[3], this.settings[3] = 0), this.push());
|
|
733
|
+
}
|
|
734
|
+
get isEnabled() {
|
|
735
|
+
return this.enabled && this.body.enabled;
|
|
736
|
+
}
|
|
737
|
+
translation() {
|
|
738
|
+
let [e, t] = this.body.position, n = this.body.rotation, r = Math.cos(n), i = Math.sin(n);
|
|
739
|
+
return [e + r * this.offset[0] - i * this.offset[1], t + i * this.offset[0] + r * this.offset[1]];
|
|
740
|
+
}
|
|
741
|
+
rotation() {
|
|
742
|
+
return this.body.rotation + this.turn;
|
|
743
|
+
}
|
|
744
|
+
translationWrtParent() {
|
|
745
|
+
return [this.offset[0], this.offset[1]];
|
|
746
|
+
}
|
|
747
|
+
rotationWrtParent() {
|
|
748
|
+
return this.turn;
|
|
749
|
+
}
|
|
750
|
+
setTranslationWrtParent(e) {
|
|
751
|
+
this.place(e, this.turn);
|
|
752
|
+
}
|
|
753
|
+
setRotationWrtParent(e) {
|
|
754
|
+
this.place(this.offset, e);
|
|
755
|
+
}
|
|
756
|
+
place(t, n) {
|
|
757
|
+
if (this.removed) throw new e("state", "Collider2D was removed; add a new collider with Body2D.addCollider.");
|
|
758
|
+
if (!(t?.length === 2 && Number.isFinite(t[0]) && Number.isFinite(t[1]) && Number.isFinite(n))) throw RangeError(`A collider's place on its body is a finite [x, y] and angle (got ${String(t)}, ${n}); pass them.`);
|
|
759
|
+
let { kernel: r, handle: i, scale: a } = this.owner, o = n - this.turn, s = Math.cos(o), c = Math.sin(o), [l, u] = this.offset, d = (t[0] - (s * l - c * u)) * a, f = (t[1] - (c * l + s * u)) * a;
|
|
760
|
+
for (let e of this.shapes) {
|
|
761
|
+
let t = r.io(3);
|
|
762
|
+
t.f[0] = d, t.f[1] = f, t.f[2] = o, r.ok(r.exports.shape_move(i, e));
|
|
763
|
+
}
|
|
764
|
+
this.offset = [t[0], t[1]], this.turn = n, this.massProps && this.setMassProperties(this.massProps.mass, this.massProps.centre, this.massProps.inertia), this.options && (this.options = {
|
|
765
|
+
...this.options,
|
|
766
|
+
offset: this.offset,
|
|
767
|
+
rotation: n
|
|
768
|
+
}, this.described = {
|
|
769
|
+
...E(this.options, a)[0],
|
|
770
|
+
settings: this.settings
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
remove() {
|
|
774
|
+
this.owner.removeCollider(this);
|
|
775
|
+
}
|
|
776
|
+
}, A = class {
|
|
777
|
+
constructor(e, t, n, r) {
|
|
778
|
+
this.owner = e, this.handle = t, this.sprite = n, this.kind = r, this.colliders = [], this.pose = /* @__PURE__ */ new Float64Array(6), this.dirty = !0, this.removed = !1, this.extraMass = 0, this.params = {
|
|
779
|
+
linearDamping: 0,
|
|
780
|
+
angularDamping: 0,
|
|
781
|
+
gravityScale: 1,
|
|
782
|
+
canSleep: !0,
|
|
783
|
+
ccd: !1,
|
|
784
|
+
dominance: 0
|
|
785
|
+
}, this.soft = 0, this.turnLocked = !1, this.lockJoint = -1, this.locked = [!1, !1], this.snapshot(), this.pose.copyWithin(0, 3, 6);
|
|
786
|
+
}
|
|
787
|
+
snapshot() {
|
|
788
|
+
let { kernel: e, handle: t, scale: n } = this.owner;
|
|
789
|
+
e.ok(e.exports.body_read(t, this.handle));
|
|
790
|
+
let r = e.io(4);
|
|
791
|
+
this.store(r.f[0], r.f[1], r.f[2], r.f[3], 1 / n);
|
|
792
|
+
}
|
|
793
|
+
store(e, t, n, r, i) {
|
|
794
|
+
let a = this.pose;
|
|
795
|
+
a.copyWithin(0, 3, 6), a[3] = e * i, a[4] = t * i, a[5] = Math.atan2(r, n);
|
|
796
|
+
}
|
|
797
|
+
get type() {
|
|
798
|
+
return this.kind;
|
|
799
|
+
}
|
|
800
|
+
setType(e) {
|
|
801
|
+
return this.check(), this.owner.retype(this, e), this.kind = e, this;
|
|
802
|
+
}
|
|
803
|
+
setParams(e) {
|
|
804
|
+
let { x: t, h: n, kernel: r } = this.k, i = {
|
|
805
|
+
...this.params,
|
|
806
|
+
...e
|
|
807
|
+
}, a = +!!i.canSleep | (i.ccd ? 4 : 0);
|
|
808
|
+
if (!Number.isInteger(i.dominance) || i.dominance < -127 || i.dominance > 127) throw RangeError(`dominance must be an integer from -127 to 127, got ${i.dominance}.`);
|
|
809
|
+
r.ok(t.body_set_params(n, this.handle, O(i.linearDamping, "damping"), O(i.angularDamping, "damping"), O(i.gravityScale, "gravity scale"), a, i.dominance)), this.params = i;
|
|
810
|
+
}
|
|
811
|
+
get linearDamping() {
|
|
812
|
+
return this.params.linearDamping;
|
|
813
|
+
}
|
|
814
|
+
set linearDamping(e) {
|
|
815
|
+
this.setParams({ linearDamping: e });
|
|
816
|
+
}
|
|
817
|
+
get angularDamping() {
|
|
818
|
+
return this.params.angularDamping;
|
|
819
|
+
}
|
|
820
|
+
set angularDamping(e) {
|
|
821
|
+
this.setParams({ angularDamping: e });
|
|
822
|
+
}
|
|
823
|
+
get gravityScale() {
|
|
824
|
+
return this.params.gravityScale;
|
|
825
|
+
}
|
|
826
|
+
set gravityScale(e) {
|
|
827
|
+
this.setParams({ gravityScale: e });
|
|
828
|
+
}
|
|
829
|
+
get canSleep() {
|
|
830
|
+
return this.params.canSleep;
|
|
831
|
+
}
|
|
832
|
+
set canSleep(e) {
|
|
833
|
+
this.setParams({ canSleep: e });
|
|
834
|
+
}
|
|
835
|
+
get ccd() {
|
|
836
|
+
return this.params.ccd;
|
|
837
|
+
}
|
|
838
|
+
set ccd(e) {
|
|
839
|
+
this.setParams({ ccd: e });
|
|
840
|
+
}
|
|
841
|
+
get softCcd() {
|
|
842
|
+
return this.soft;
|
|
843
|
+
}
|
|
844
|
+
set softCcd(e) {
|
|
845
|
+
let { x: t, h: n, kernel: r, scale: i } = this.k;
|
|
846
|
+
r.ok(t.body_set_soft_ccd(n, this.handle, O(e, "softCcd") * i)), this.soft = e;
|
|
847
|
+
}
|
|
848
|
+
get dominance() {
|
|
849
|
+
return this.params.dominance;
|
|
850
|
+
}
|
|
851
|
+
set dominance(e) {
|
|
852
|
+
this.setParams({ dominance: e });
|
|
853
|
+
}
|
|
854
|
+
check() {
|
|
855
|
+
if (this.removed) throw new e("state", "Body2D was removed; add a new body with Physics2D.addBody.");
|
|
856
|
+
}
|
|
857
|
+
get k() {
|
|
858
|
+
this.check();
|
|
859
|
+
let { kernel: e, handle: t, scale: n } = this.owner;
|
|
860
|
+
return {
|
|
861
|
+
x: e.exports,
|
|
862
|
+
h: t,
|
|
863
|
+
kernel: e,
|
|
864
|
+
scale: n
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
get isRemoved() {
|
|
868
|
+
return this.removed;
|
|
869
|
+
}
|
|
870
|
+
markRemoved() {
|
|
871
|
+
this.removed = !0;
|
|
872
|
+
}
|
|
873
|
+
get position() {
|
|
874
|
+
return this.check(), [this.pose[3], this.pose[4]];
|
|
875
|
+
}
|
|
876
|
+
get rotation() {
|
|
877
|
+
return this.check(), this.pose[5];
|
|
878
|
+
}
|
|
879
|
+
teleport(e, t) {
|
|
880
|
+
let { x: n, h: r, kernel: i, scale: a } = this.k, o = t ?? this.pose[5];
|
|
881
|
+
return O(e[0], "position x"), O(e[1], "position y"), O(o, "rotation"), i.ok(n.body_set_transform(r, this.handle, e[0] * a, e[1] * a, Math.cos(o), Math.sin(o))), this.snapshot(), this.pose.copyWithin(0, 3, 6), this.dirty = !0, (this.locked[0] || this.locked[1]) && this.owner.lockTranslation(this, this.locked[0], this.locked[1]), this;
|
|
882
|
+
}
|
|
883
|
+
moveTo(e, t) {
|
|
884
|
+
let { x: n, h: r, kernel: i, scale: a } = this.k, o = t ?? this.pose[5];
|
|
885
|
+
return i.ok(n.body_set_next(r, this.handle, e[0] * a, e[1] * a, Math.cos(o), Math.sin(o))), this;
|
|
886
|
+
}
|
|
887
|
+
read() {
|
|
888
|
+
let { x: e, h: t, kernel: n } = this.k;
|
|
889
|
+
return n.ok(e.body_read(t, this.handle)), n.io(16);
|
|
890
|
+
}
|
|
891
|
+
get linearVelocity() {
|
|
892
|
+
let { f: e } = this.read(), t = 1 / this.owner.scale;
|
|
893
|
+
return [e[4] * t, e[5] * t];
|
|
894
|
+
}
|
|
895
|
+
set linearVelocity(e) {
|
|
896
|
+
let { x: t, h: n, kernel: r, scale: i } = this.k, a = this.read().f[6];
|
|
897
|
+
r.ok(t.body_set_velocity(n, this.handle, O(e[0], "velocity x") * i, O(e[1], "velocity y") * i, a));
|
|
898
|
+
}
|
|
899
|
+
get angularVelocity() {
|
|
900
|
+
return this.read().f[6];
|
|
901
|
+
}
|
|
902
|
+
set angularVelocity(e) {
|
|
903
|
+
let { x: t, h: n, kernel: r } = this.k, { f: i } = this.read();
|
|
904
|
+
r.ok(t.body_set_velocity(n, this.handle, i[4], i[5], O(e, "angular velocity")));
|
|
905
|
+
}
|
|
906
|
+
applyImpulse(e) {
|
|
907
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
908
|
+
return r.ok(t.body_impulse(n, this.handle, O(e[0], "impulse x"), O(e[1], "impulse y"), 0, 0, 0)), this;
|
|
909
|
+
}
|
|
910
|
+
applyImpulseAtPoint(e, t) {
|
|
911
|
+
let { x: n, h: r, kernel: i, scale: a } = this.k;
|
|
912
|
+
return i.ok(n.body_impulse(r, this.handle, O(e[0], "impulse x"), O(e[1], "impulse y"), 1, t[0] * a, t[1] * a)), this;
|
|
913
|
+
}
|
|
914
|
+
applyTorqueImpulse(e) {
|
|
915
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
916
|
+
return r.ok(t.body_torque_impulse(n, this.handle, O(e, "torque impulse"))), this;
|
|
917
|
+
}
|
|
918
|
+
applyForce(e) {
|
|
919
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
920
|
+
return r.ok(t.body_add_force(n, this.handle, O(e[0], "force x"), O(e[1], "force y"), 0)), this;
|
|
921
|
+
}
|
|
922
|
+
applyTorque(e) {
|
|
923
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
924
|
+
return r.ok(t.body_add_force(n, this.handle, 0, 0, O(e, "torque"))), this;
|
|
925
|
+
}
|
|
926
|
+
applyForceAtPoint(e, t) {
|
|
927
|
+
let { x: n, h: r, kernel: i, scale: a } = this.k;
|
|
928
|
+
return O(e[0], "force x"), O(e[1], "force y"), O(t[0], "point x"), O(t[1], "point y"), i.ok(n.body_add_force_at(r, this.handle, e[0], e[1], t[0] * a, t[1] * a)), this;
|
|
929
|
+
}
|
|
930
|
+
resetTorques() {
|
|
931
|
+
let { x: e, h: t, kernel: n } = this.k;
|
|
932
|
+
return n.ok(e.body_reset_torques(t, this.handle)), this;
|
|
933
|
+
}
|
|
934
|
+
resetForces() {
|
|
935
|
+
let { x: e, h: t, kernel: n } = this.k;
|
|
936
|
+
return n.ok(e.body_reset_forces(t, this.handle)), this;
|
|
937
|
+
}
|
|
938
|
+
get mass() {
|
|
939
|
+
return this.read().f[7];
|
|
940
|
+
}
|
|
941
|
+
get inertia() {
|
|
942
|
+
let e = this.owner.scale;
|
|
943
|
+
return this.read().f[15] / (e * e);
|
|
944
|
+
}
|
|
945
|
+
restoreAdditionalMass(e) {
|
|
946
|
+
this.extraMass = e;
|
|
947
|
+
}
|
|
948
|
+
get additionalMass() {
|
|
949
|
+
return this.extraMass;
|
|
950
|
+
}
|
|
951
|
+
set additionalMass(e) {
|
|
952
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
953
|
+
r.ok(t.body_set_additional_mass(n, this.handle, O(e, "additional mass"))), this.extraMass = e;
|
|
954
|
+
}
|
|
955
|
+
get enabled() {
|
|
956
|
+
return this.read().u[11] !== 0;
|
|
957
|
+
}
|
|
958
|
+
set enabled(e) {
|
|
959
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
960
|
+
r.ok(t.body_set_enabled(n, this.handle, +!!e));
|
|
961
|
+
}
|
|
962
|
+
set maxAngularSpeed(e) {
|
|
963
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
964
|
+
O(e, "max angular speed"), r.ok(t.body_set_max_angular_speed(n, this.handle, e));
|
|
965
|
+
}
|
|
966
|
+
get maxAngularSpeed() {
|
|
967
|
+
return this.read().f[12];
|
|
968
|
+
}
|
|
969
|
+
set additionalPgsIterations(e) {
|
|
970
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
971
|
+
if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw RangeError(`additionalPgsIterations must be a whole number of 0 or more; got ${e}.`);
|
|
972
|
+
r.ok(t.body_set_extra_passes(n, this.handle, e));
|
|
973
|
+
}
|
|
974
|
+
get additionalPgsIterations() {
|
|
975
|
+
return this.read().u[13];
|
|
976
|
+
}
|
|
977
|
+
set additionalSolverIterations(e) {
|
|
978
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
979
|
+
if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw RangeError(`additionalSolverIterations must be a whole number of 0 or more; got ${e}.`);
|
|
980
|
+
r.ok(t.body_set_extra_substeps(n, this.handle, e));
|
|
981
|
+
}
|
|
982
|
+
get additionalSolverIterations() {
|
|
983
|
+
return this.read().u[14];
|
|
984
|
+
}
|
|
985
|
+
get centerOfMass() {
|
|
986
|
+
let { f: e } = this.read(), { scale: t } = this.k;
|
|
987
|
+
return [e[9] / t, e[10] / t];
|
|
988
|
+
}
|
|
989
|
+
setAdditionalMassProperties(e, t = [0, 0], n = 0) {
|
|
990
|
+
let { x: r, h: i, kernel: a, scale: o } = this.k;
|
|
991
|
+
return O(e, "additional mass"), O(n, "additional inertia"), O(t[0], "additional mass centre x"), O(t[1], "additional mass centre y"), a.ok(r.body_set_additional_mass_properties(i, this.handle, e, t[0] * o, t[1] * o, n * o * o)), this;
|
|
992
|
+
}
|
|
993
|
+
get sleeping() {
|
|
994
|
+
return this.read().u[8] === 0;
|
|
995
|
+
}
|
|
996
|
+
wakeUp() {
|
|
997
|
+
let { x: e, h: t, kernel: n } = this.k;
|
|
998
|
+
return n.ok(e.body_wake(t, this.handle)), this;
|
|
999
|
+
}
|
|
1000
|
+
sleep() {
|
|
1001
|
+
let { x: e, h: t, kernel: n } = this.k;
|
|
1002
|
+
return n.ok(e.body_sleep(t, this.handle)), this;
|
|
1003
|
+
}
|
|
1004
|
+
lockTranslation(e = !0, t = e) {
|
|
1005
|
+
if (this.check(), this.type === "fixed") throw TypeError("A fixed body does not move; lock the translation of a dynamic or kinematic body.");
|
|
1006
|
+
return this.owner.lockTranslation(this, e, t), this.locked = [e, t], this;
|
|
1007
|
+
}
|
|
1008
|
+
lockRotation(e = !0) {
|
|
1009
|
+
let { x: t, h: n, kernel: r } = this.k;
|
|
1010
|
+
if (this.type === "fixed") throw TypeError("A fixed body does not turn; lock the rotation of a dynamic or kinematic body.");
|
|
1011
|
+
return r.ok(t.body_set_fixed_rotation(n, this.handle, +!!e)), this.turnLocked = e, this;
|
|
1012
|
+
}
|
|
1013
|
+
get rotationLocked() {
|
|
1014
|
+
return this.turnLocked;
|
|
1015
|
+
}
|
|
1016
|
+
get translationLocked() {
|
|
1017
|
+
return [this.locked[0], this.locked[1]];
|
|
1018
|
+
}
|
|
1019
|
+
addCollider(e) {
|
|
1020
|
+
return this.check(), this.owner.addCollider(this, e);
|
|
1021
|
+
}
|
|
1022
|
+
remove() {
|
|
1023
|
+
this.owner.removeBody(this);
|
|
1024
|
+
}
|
|
1025
|
+
}, j = (e) => {
|
|
1026
|
+
let t = e % (2 * Math.PI);
|
|
1027
|
+
return t > Math.PI ? t - 2 * Math.PI : t < -Math.PI ? t + 2 * Math.PI : t;
|
|
1028
|
+
}, M = class {
|
|
1029
|
+
constructor(e, t, n) {
|
|
1030
|
+
if (this.physics = e, this.root = t, this.joints = [], this.removed = !1, !n.length) throw TypeError("An articulation needs at least one link; pass the bodies hanging from the root.");
|
|
1031
|
+
this.links = n.map((e, t) => {
|
|
1032
|
+
let n = e.parent ?? -1;
|
|
1033
|
+
if (!Number.isInteger(n) || n < -1 || n >= t) throw RangeError(`link ${t}: parent must be -1 (the root) or the index of an earlier link, got ${n}; list parents before children.`);
|
|
1034
|
+
let r = e.joint;
|
|
1035
|
+
if (r.type !== "revolute" && r.type !== "prismatic") throw TypeError(`link ${t}: an articulation joint is revolute or prismatic, got "${r.type}"; use one of those types.`);
|
|
1036
|
+
let i = r.axis ?? [1, 0], a = Math.hypot(i[0], i[1]);
|
|
1037
|
+
if (!(a > 0) || !Number.isFinite(a)) throw RangeError(`link ${t}: the joint axis must be a finite vector that is not zero; try [1, 0].`);
|
|
1038
|
+
let o = [i[0] / a, i[1] / a];
|
|
1039
|
+
return {
|
|
1040
|
+
link: e,
|
|
1041
|
+
parent: n,
|
|
1042
|
+
anchorParent: r.anchorParent ?? [0, 0],
|
|
1043
|
+
anchorChild: r.anchorChild ?? [0, 0],
|
|
1044
|
+
axis: o
|
|
1045
|
+
};
|
|
1046
|
+
});
|
|
1047
|
+
try {
|
|
1048
|
+
this.links.forEach(({ link: n, parent: r, anchorParent: i, anchorChild: a, axis: o }) => {
|
|
1049
|
+
let s = n.joint, c = {
|
|
1050
|
+
type: s.type,
|
|
1051
|
+
anchorA: i,
|
|
1052
|
+
anchorB: a,
|
|
1053
|
+
...s.type === "prismatic" ? { axis: o } : {},
|
|
1054
|
+
...s.limits ? { limits: s.limits } : {},
|
|
1055
|
+
...s.motor ? { motor: s.motor } : {},
|
|
1056
|
+
collide: !1
|
|
1057
|
+
};
|
|
1058
|
+
this.joints.push(e.addJoint(r < 0 ? t : this.links[r].link.body, n.body, c));
|
|
1059
|
+
});
|
|
1060
|
+
} catch (e) {
|
|
1061
|
+
for (let e of this.joints) e.remove();
|
|
1062
|
+
throw e;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
get dof() {
|
|
1066
|
+
return this.links.length;
|
|
1067
|
+
}
|
|
1068
|
+
bodyOf(e) {
|
|
1069
|
+
return e < 0 ? this.root : this.links[e].link.body;
|
|
1070
|
+
}
|
|
1071
|
+
check() {
|
|
1072
|
+
if (this.removed) throw new e("state", "Articulation2D was removed; add a new one with Physics2D.addArticulation.");
|
|
1073
|
+
}
|
|
1074
|
+
positions() {
|
|
1075
|
+
return this.check(), this.links.map(({ link: e, parent: t, anchorParent: n, anchorChild: r, axis: i }) => {
|
|
1076
|
+
let a = this.bodyOf(t), o = e.body;
|
|
1077
|
+
if (e.joint.type === "revolute") return j(o.rotation - a.rotation);
|
|
1078
|
+
let [s, c] = [this.world(a, n), this.world(o, r)], l = N(i, a.rotation);
|
|
1079
|
+
return l[0] * (c[0] - s[0]) + l[1] * (c[1] - s[1]);
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
velocities() {
|
|
1083
|
+
return this.check(), this.links.map(({ link: e, parent: t, anchorParent: n, anchorChild: r, axis: i }) => {
|
|
1084
|
+
let a = this.bodyOf(t), o = e.body;
|
|
1085
|
+
if (e.joint.type === "revolute") return o.angularVelocity - a.angularVelocity;
|
|
1086
|
+
let s = this.pointVelocity(a, n), c = this.pointVelocity(o, r), l = N(i, a.rotation);
|
|
1087
|
+
return l[0] * (c[0] - s[0]) + l[1] * (c[1] - s[1]);
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
world(e, t) {
|
|
1091
|
+
let n = N(t, e.rotation);
|
|
1092
|
+
return [e.position[0] + n[0], e.position[1] + n[1]];
|
|
1093
|
+
}
|
|
1094
|
+
pointVelocity(e, t) {
|
|
1095
|
+
let n = N(t, e.rotation), r = e.linearVelocity, i = e.angularVelocity;
|
|
1096
|
+
return [r[0] - i * n[1], r[1] + i * n[0]];
|
|
1097
|
+
}
|
|
1098
|
+
forwardKinematics(e = this.positions()) {
|
|
1099
|
+
if (this.check(), e.length !== this.dof) throw RangeError(`q has ${e.length} values for ${this.dof} joints; pass one value per joint.`);
|
|
1100
|
+
let t = [], n = {
|
|
1101
|
+
position: [...this.root.position],
|
|
1102
|
+
rotation: this.root.rotation
|
|
1103
|
+
};
|
|
1104
|
+
return this.links.forEach(({ link: r, parent: i, anchorParent: a, anchorChild: o, axis: s }, c) => {
|
|
1105
|
+
let l = i < 0 ? n : t[i], u = N(a, l.rotation), d = [l.position[0] + u[0], l.position[1] + u[1]];
|
|
1106
|
+
if (r.joint.type === "revolute") {
|
|
1107
|
+
let n = l.rotation + e[c], r = N(o, n);
|
|
1108
|
+
t.push({
|
|
1109
|
+
position: [d[0] - r[0], d[1] - r[1]],
|
|
1110
|
+
rotation: n
|
|
1111
|
+
});
|
|
1112
|
+
} else {
|
|
1113
|
+
let n = N(s, l.rotation), r = N(o, l.rotation);
|
|
1114
|
+
t.push({
|
|
1115
|
+
position: [d[0] + n[0] * e[c] - r[0], d[1] + n[1] * e[c] - r[1]],
|
|
1116
|
+
rotation: l.rotation
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
}), t;
|
|
1120
|
+
}
|
|
1121
|
+
pointAt(e, t, n = this.positions()) {
|
|
1122
|
+
this.checkLink(e);
|
|
1123
|
+
let r = this.forwardKinematics(n)[e], i = N(t, r.rotation);
|
|
1124
|
+
return [r.position[0] + i[0], r.position[1] + i[1]];
|
|
1125
|
+
}
|
|
1126
|
+
checkLink(e) {
|
|
1127
|
+
if (!Number.isInteger(e) || e < 0 || e >= this.dof) throw RangeError(`link must be 0 to ${this.dof - 1}, got ${e}; pass an index of this articulation.`);
|
|
1128
|
+
}
|
|
1129
|
+
jacobian(e, t, n = this.positions()) {
|
|
1130
|
+
this.checkLink(e);
|
|
1131
|
+
let r = [], i = [];
|
|
1132
|
+
for (let a = 0; a < this.dof; a++) {
|
|
1133
|
+
let o = this.links[a].link.joint.type === "revolute" ? 1e-5 : 1e-4 * (1 + Math.abs(n[a])), [s, c] = [n.slice(), n.slice()];
|
|
1134
|
+
s[a] -= o, c[a] += o;
|
|
1135
|
+
let [l, u] = [this.pointAt(e, t, s), this.pointAt(e, t, c)];
|
|
1136
|
+
r.push((u[0] - l[0]) / (2 * o)), i.push((u[1] - l[1]) / (2 * o));
|
|
1137
|
+
}
|
|
1138
|
+
return [r, i];
|
|
1139
|
+
}
|
|
1140
|
+
solveIK(e, t, n, r = {}) {
|
|
1141
|
+
this.checkLink(e);
|
|
1142
|
+
let { iterations: i = 64, tolerance: a = .01, damping: o = .5 } = r, s = this.positions(), c = this.links.map(({ link: e }) => e.joint.limits), l = Infinity;
|
|
1143
|
+
for (let r = 0; r < i; r++) {
|
|
1144
|
+
let r = this.pointAt(e, t, s), i = [n[0] - r[0], n[1] - r[1]];
|
|
1145
|
+
if (l = Math.hypot(i[0], i[1]), l <= a) break;
|
|
1146
|
+
let [u, d] = this.jacobian(e, t, s), f = o * o, p = 0, m = o * o;
|
|
1147
|
+
for (let e = 0; e < this.dof; e++) f += u[e] * u[e], p += u[e] * d[e], m += d[e] * d[e];
|
|
1148
|
+
let h = f * m - p * p, g = [(m * i[0] - p * i[1]) / h, (f * i[1] - p * i[0]) / h], _ = u.map((e, t) => e * g[0] + d[t] * g[1]), v = 0;
|
|
1149
|
+
_.forEach((e, t) => {
|
|
1150
|
+
this.links[t].link.joint.type === "revolute" && (v = Math.max(v, Math.abs(e)));
|
|
1151
|
+
});
|
|
1152
|
+
let y = v > .3 ? .3 / v : 1;
|
|
1153
|
+
for (let e = 0; e < this.dof; e++) {
|
|
1154
|
+
s[e] += _[e] * y;
|
|
1155
|
+
let t = c[e];
|
|
1156
|
+
t && (s[e] = Math.max(t[0], Math.min(t[1], s[e])));
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
let u = this.pointAt(e, t, s);
|
|
1160
|
+
return l = Math.hypot(n[0] - u[0], n[1] - u[1]), {
|
|
1161
|
+
q: s,
|
|
1162
|
+
reached: l <= a,
|
|
1163
|
+
error: l
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
setPositions(e) {
|
|
1167
|
+
this.check();
|
|
1168
|
+
let t = this.forwardKinematics(e);
|
|
1169
|
+
return this.links.forEach(({ link: e }, n) => e.body.teleport(t[n].position, t[n].rotation)), this;
|
|
1170
|
+
}
|
|
1171
|
+
driveTo(e, { stiffness: t = 200, damping: n = 20, maxForce: r } = {}) {
|
|
1172
|
+
if (this.check(), e.length !== this.dof) throw RangeError(`q has ${e.length} values for ${this.dof} joints; pass one value per joint.`);
|
|
1173
|
+
return this.joints.forEach((i, a) => i.setMotor({
|
|
1174
|
+
targetPosition: e[a],
|
|
1175
|
+
stiffness: t,
|
|
1176
|
+
damping: n,
|
|
1177
|
+
...r === void 0 ? {} : { maxForce: r }
|
|
1178
|
+
})), this;
|
|
1179
|
+
}
|
|
1180
|
+
setMotor(e, t) {
|
|
1181
|
+
return this.check(), this.checkLink(e), this.joints[e].setMotor(t), this;
|
|
1182
|
+
}
|
|
1183
|
+
setLimits(e, t, n) {
|
|
1184
|
+
return this.check(), this.checkLink(e), this.joints[e].setLimits(t, n), this.links[e].link.joint.limits = [t, n], this;
|
|
1185
|
+
}
|
|
1186
|
+
remove() {
|
|
1187
|
+
if (!this.removed) {
|
|
1188
|
+
this.removed = !0;
|
|
1189
|
+
for (let e of this.joints) e.remove();
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
function N(e, t) {
|
|
1194
|
+
let n = Math.cos(t), r = Math.sin(t);
|
|
1195
|
+
return [n * e[0] - r * e[1], r * e[0] + n * e[1]];
|
|
1196
|
+
}
|
|
1197
|
+
//#endregion
|
|
1198
|
+
//#region src/character.ts
|
|
1199
|
+
var P = 1e-5, F = 8, I = class {
|
|
1200
|
+
constructor(e, t, n, r) {
|
|
1201
|
+
this.physics = e, this.body = t, this.options = n, this.onDispose = r, this.disposed = !1, this.filter = {};
|
|
1202
|
+
let i = n.up ?? [0, -1], a = Math.hypot(i[0], i[1]);
|
|
1203
|
+
if (!(a > 0) || !Number.isFinite(a)) throw RangeError("The up vector must be finite and not zero; try [0, -1].");
|
|
1204
|
+
this.up = [i[0] / a, i[1] / a], this.offset = (n.offset ?? .5) * e.scale, this.climb = Math.cos(n.maxSlopeClimbAngle ?? Math.PI / 4), this.slideAngle = Math.cos(n.minSlopeSlideAngle ?? Math.PI / 6);
|
|
1205
|
+
}
|
|
1206
|
+
cast(e, t, n) {
|
|
1207
|
+
let r = this.body.colliders[0];
|
|
1208
|
+
if (!r?.described) throw TypeError("The character body has no collider; add one with body.addCollider.");
|
|
1209
|
+
let i = this.body.rotation;
|
|
1210
|
+
return this.physics.cast(r.described, e[0], e[1], i, t[0], t[1], n, this.filter);
|
|
1211
|
+
}
|
|
1212
|
+
filterFor(e) {
|
|
1213
|
+
let t = e ?? this.options.filter ?? {}, n = t.exclude;
|
|
1214
|
+
return {
|
|
1215
|
+
...t,
|
|
1216
|
+
exclude: this.body,
|
|
1217
|
+
excludeSensors: t.excludeSensors ?? !0,
|
|
1218
|
+
predicate: n ? (e, r) => e !== n && (!t.predicate || t.predicate(e, r)) : t.predicate
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
1221
|
+
move(e, t) {
|
|
1222
|
+
if (this.disposed) throw TypeError("This character controller was disposed; create a new one.");
|
|
1223
|
+
if (!Number.isFinite(e[0]) || !Number.isFinite(e[1])) throw TypeError("delta must be two finite numbers [x, y].");
|
|
1224
|
+
this.filter = this.filterFor(t);
|
|
1225
|
+
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 < F; e++) {
|
|
1227
|
+
let e = Math.hypot(o[0], o[1]);
|
|
1228
|
+
if (e < P) break;
|
|
1229
|
+
let t = [o[0] / e, o[1] / e], r = this.cast(a, t, e + this.offset);
|
|
1230
|
+
if (!r) {
|
|
1231
|
+
a[0] += o[0], a[1] += o[1], o = [0, 0];
|
|
1232
|
+
break;
|
|
1233
|
+
}
|
|
1234
|
+
let i = r.normal, s = Math.max(-f(t, i), .1), p = Math.max(0, Math.min(e, r.distance * n - this.offset / s));
|
|
1235
|
+
a[0] += t[0] * p, a[1] += t[1] * p, l++, u.push({
|
|
1236
|
+
collider: r.collider,
|
|
1237
|
+
body: r.body,
|
|
1238
|
+
normal: [i[0], i[1]],
|
|
1239
|
+
point: [r.point[0], r.point[1]]
|
|
1240
|
+
});
|
|
1241
|
+
let m = e - p, h = [t[0] * m, t[1] * m], g = f(i, d), _ = g >= this.climb - 1e-6;
|
|
1242
|
+
if (_ && (c = !0), this.pushDynamic(r.body, i, h), this.options.slide === !1) {
|
|
1243
|
+
o = [0, 0];
|
|
1244
|
+
break;
|
|
1245
|
+
}
|
|
1246
|
+
if (!_ && this.options.autostep && g > -.5 && (this.options.autostep.includeDynamic !== !1 || r.body.type !== "dynamic")) {
|
|
1247
|
+
let e = this.step(a, h);
|
|
1248
|
+
if (e) {
|
|
1249
|
+
a[0] = e.pos[0], a[1] = e.pos[1], o = e.remaining, c = !0;
|
|
1250
|
+
continue;
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
if (_ && g < this.slideAngle - 1e-6) o = [h[0] - i[0] * f(h, i), h[1] - i[1] * f(h, i)];
|
|
1254
|
+
else if (_) {
|
|
1255
|
+
let e = [h[0] - d[0] * f(h, d), h[1] - d[1] * f(h, d)], t = -f(e, i) / Math.max(g, .001);
|
|
1256
|
+
o = [e[0] + d[0] * t, e[1] + d[1] * t], f(h, d) < 0 && Math.abs(f(e, e)) < P * P && (o = [0, 0]);
|
|
1257
|
+
} else {
|
|
1258
|
+
o = [h[0] - i[0] * f(h, i), h[1] - i[1] * f(h, i)];
|
|
1259
|
+
let e = f(o, d);
|
|
1260
|
+
e > 0 && (o = [o[0] - d[0] * e, o[1] - d[1] * e]);
|
|
1261
|
+
}
|
|
1262
|
+
if (f(o, o) < P * P) break;
|
|
1263
|
+
}
|
|
1264
|
+
if (this.options.snapToGround !== void 0 && !c && f(s, d) <= P) {
|
|
1265
|
+
let e = this.options.snapToGround * n, t = this.cast(a, [-d[0], -d[1]], e + this.offset);
|
|
1266
|
+
if (t && f(t.normal, d) >= this.climb - 1e-6) {
|
|
1267
|
+
let e = Math.max(0, t.distance * n - this.offset);
|
|
1268
|
+
a[0] -= d[0] * e, a[1] -= d[1] * e, c = !0;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
let p = [(a[0] - i[0]) * r, (a[1] - i[1]) * r];
|
|
1272
|
+
return this.body.moveTo([this.body.position[0] + p[0], this.body.position[1] + p[1]]), {
|
|
1273
|
+
movement: p,
|
|
1274
|
+
grounded: c,
|
|
1275
|
+
collisions: l,
|
|
1276
|
+
hits: u
|
|
1277
|
+
};
|
|
1278
|
+
}
|
|
1279
|
+
step(e, t) {
|
|
1280
|
+
let n = this.options.autostep;
|
|
1281
|
+
if (!n) return null;
|
|
1282
|
+
let r = this.physics.scale, i = this.up, a = t[0] * i[0] + t[1] * i[1], o = [t[0] - i[0] * a, t[1] - i[1] * a], s = Math.hypot(o[0], o[1]);
|
|
1283
|
+
if (s < P) return null;
|
|
1284
|
+
let c = [o[0] / s, o[1] / s], l = n.maxHeight * r, u = n.minWidth * r, d = this.cast(e, i, l + this.offset), f = d ? Math.max(0, d.distance * r - this.offset) : l;
|
|
1285
|
+
if (f < P) return null;
|
|
1286
|
+
let p = [e[0] + i[0] * f, e[1] + i[1] * f], m = Math.max(s, u), h = this.cast(p, c, m + this.offset), g = h ? Math.max(0, h.distance * r - this.offset) : m;
|
|
1287
|
+
if (g < u - P) return null;
|
|
1288
|
+
let _ = [p[0] + c[0] * g, p[1] + c[1] * g], v = this.cast(_, [-i[0], -i[1]], f + this.offset);
|
|
1289
|
+
if (!v || v.normal[0] * i[0] + v.normal[1] * i[1] < this.climb - 1e-6 || n.includeDynamic === !1 && v.body.type === "dynamic") return null;
|
|
1290
|
+
let y = Math.max(0, v.distance * r - this.offset), b = [_[0] - i[0] * y, _[1] - i[1] * y];
|
|
1291
|
+
if (f - y < P) return null;
|
|
1292
|
+
let x = Math.max(0, s - Math.min(g, s));
|
|
1293
|
+
return {
|
|
1294
|
+
pos: b,
|
|
1295
|
+
remaining: [c[0] * x, c[1] * x]
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
pushDynamic(e, t, n) {
|
|
1299
|
+
if (!this.options.pushDynamic || e.type !== "dynamic") return;
|
|
1300
|
+
let r = -(n[0] * t[0] + n[1] * t[1]);
|
|
1301
|
+
if (r <= 0) return;
|
|
1302
|
+
let i = (this.options.mass ?? (this.body.mass || 1)) * r / this.physics.fixedTimestep;
|
|
1303
|
+
e.applyImpulse([-t[0] * i, -t[1] * i]);
|
|
1304
|
+
}
|
|
1305
|
+
dispose() {
|
|
1306
|
+
this.disposed || (this.disposed = !0, this.onDispose(this));
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1309
|
+
I.prototype[Symbol.dispose] = I.prototype.dispose;
|
|
1310
|
+
//#endregion
|
|
1311
|
+
//#region src/joint.ts
|
|
1312
|
+
var L = (e, t) => {
|
|
1313
|
+
if (!Number.isFinite(e)) throw RangeError(`${t} must be a finite number, got ${e}; fix the value.`);
|
|
1314
|
+
return e;
|
|
1315
|
+
};
|
|
1316
|
+
function R(e, t, n) {
|
|
1317
|
+
let r = e.io(32);
|
|
1318
|
+
r.u.fill(0, 0, 32);
|
|
1319
|
+
let i = t.anchorA ?? [0, 0], a = t.anchorB ?? [0, 0];
|
|
1320
|
+
r.f.set([
|
|
1321
|
+
L(i[0], "anchorA x") * n,
|
|
1322
|
+
L(i[1], "anchorA y") * n,
|
|
1323
|
+
L(a[0], "anchorB x") * n,
|
|
1324
|
+
L(a[1], "anchorB y") * n
|
|
1325
|
+
], 1);
|
|
1326
|
+
let o = t.limits, s = t.type === "prismatic" ? n : 1;
|
|
1327
|
+
o && (r.u[5] = 1, r.f[6] = L(o[0], "joint limit") * s, r.f[7] = L(o[1], "joint limit") * s);
|
|
1328
|
+
let c = t.motor;
|
|
1329
|
+
switch (c && (z(r.f, r.u, 8, c, s), r.u[25] = +(c.model === "acceleration")), t.type) {
|
|
1330
|
+
case "revolute":
|
|
1331
|
+
r.u[0] = 0;
|
|
1332
|
+
break;
|
|
1333
|
+
case "prismatic":
|
|
1334
|
+
r.u[0] = 1, r.f[14] = L(t.axis[0], "axis x"), r.f[15] = L(t.axis[1], "axis y");
|
|
1335
|
+
break;
|
|
1336
|
+
case "fixed": {
|
|
1337
|
+
r.u[0] = 2;
|
|
1338
|
+
let [e, n] = [t.angleA ?? 0, t.angleB ?? 0];
|
|
1339
|
+
r.f.set([
|
|
1340
|
+
Math.cos(e),
|
|
1341
|
+
Math.sin(e),
|
|
1342
|
+
Math.cos(n),
|
|
1343
|
+
Math.sin(n)
|
|
1344
|
+
], 16);
|
|
1345
|
+
break;
|
|
1346
|
+
}
|
|
1347
|
+
case "generic": {
|
|
1348
|
+
r.u[0] = 5, r.u[24] = +!!t.lockX | (t.lockY ? 2 : 0) | (t.lockAngle ? 4 : 0);
|
|
1349
|
+
let [e, n] = [L(t.frameA ?? 0, "frameA"), L(t.frameB ?? 0, "frameB")];
|
|
1350
|
+
r.f.set([
|
|
1351
|
+
Math.cos(e),
|
|
1352
|
+
Math.sin(e),
|
|
1353
|
+
Math.cos(n),
|
|
1354
|
+
Math.sin(n)
|
|
1355
|
+
], 16);
|
|
1356
|
+
break;
|
|
1357
|
+
}
|
|
1358
|
+
case "slot": {
|
|
1359
|
+
r.u[0] = 5, r.u[24] = 2;
|
|
1360
|
+
let e = Math.atan2(L(t.axis[1], "axis y"), L(t.axis[0], "axis x"));
|
|
1361
|
+
r.f.set([
|
|
1362
|
+
Math.cos(e),
|
|
1363
|
+
Math.sin(e),
|
|
1364
|
+
1,
|
|
1365
|
+
0
|
|
1366
|
+
], 16);
|
|
1367
|
+
break;
|
|
1368
|
+
}
|
|
1369
|
+
case "rope":
|
|
1370
|
+
r.u[0] = 3, r.f[20] = L(t.length, "rope length") * n;
|
|
1371
|
+
break;
|
|
1372
|
+
case "spring":
|
|
1373
|
+
r.u[0] = 4, r.f[20] = L(t.restLength, "spring restLength") * n, r.f[21] = L(t.stiffness, "spring stiffness"), r.f[22] = L(t.damping ?? 0, "spring damping");
|
|
1374
|
+
break;
|
|
1375
|
+
default: throw TypeError(`Unknown joint type "${t.type}"; use revolute, prismatic, fixed, generic, slot, rope or spring.`);
|
|
1376
|
+
}
|
|
1377
|
+
return s;
|
|
1378
|
+
}
|
|
1379
|
+
function z(e, t, n, r, i) {
|
|
1380
|
+
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
|
+
t[n] = 1;
|
|
1382
|
+
let a = r.targetVelocity !== void 0, o = r.targetPosition !== void 0;
|
|
1383
|
+
e[n + 1] = o ? L(r.targetPosition, "motor targetPosition") * i : 0, e[n + 2] = a ? L(r.targetVelocity, "motor targetVelocity") * i : 0, e[n + 3] = o ? L(r.stiffness ?? 1, "motor stiffness") : 0, e[n + 4] = o ? L(r.damping ?? 0, "motor damping") : L(r.factor ?? 1, "motor factor"), o && a && r.damping === void 0 && (e[n + 4] = L(r.factor ?? 1, "motor factor")), e[n + 5] = r.maxForce === void 0 ? Infinity : L(r.maxForce, "motor maxForce");
|
|
1384
|
+
}
|
|
1385
|
+
var B = class {
|
|
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;
|
|
1388
|
+
}
|
|
1389
|
+
get limited() {
|
|
1390
|
+
if (this.type !== "revolute" && this.type !== "prismatic") throw TypeError("Only revolute and prismatic joints have limits and motors; use one of those types.");
|
|
1391
|
+
if (this.removed) throw new e("state", "Joint2D was removed; add a new joint with Physics2D.addJoint.");
|
|
1392
|
+
return !0;
|
|
1393
|
+
}
|
|
1394
|
+
setLimits(e, t) {
|
|
1395
|
+
this.limited;
|
|
1396
|
+
let n = this.unit;
|
|
1397
|
+
return this.kernel.ok(this.kernel.exports.joint_set_limits(this.world, this.handle, 1, L(e, "limit") * n, L(t, "limit") * n)), this;
|
|
1398
|
+
}
|
|
1399
|
+
clearLimits() {
|
|
1400
|
+
return this.limited, this.kernel.ok(this.kernel.exports.joint_set_limits(this.world, this.handle, 0, 0, 0)), this;
|
|
1401
|
+
}
|
|
1402
|
+
setMotor(e) {
|
|
1403
|
+
this.limited;
|
|
1404
|
+
let t = this.kernel.io(8);
|
|
1405
|
+
return z(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
|
+
}
|
|
1407
|
+
clearMotor() {
|
|
1408
|
+
return this.limited, this.kernel.ok(this.kernel.exports.joint_set_motor(this.world, this.handle, 0)), this;
|
|
1409
|
+
}
|
|
1410
|
+
remove() {
|
|
1411
|
+
this.onRemove(this);
|
|
1412
|
+
}
|
|
1413
|
+
free() {
|
|
1414
|
+
this.removed || (this.removed = !0, this.kernel.exports.joint_remove(this.world, this.handle));
|
|
1415
|
+
}
|
|
1416
|
+
}, V = [
|
|
1417
|
+
"fixed",
|
|
1418
|
+
"awake",
|
|
1419
|
+
"sleeping",
|
|
1420
|
+
"kinematic",
|
|
1421
|
+
"sensor",
|
|
1422
|
+
"joint",
|
|
1423
|
+
"contactPoint",
|
|
1424
|
+
"contactNormal",
|
|
1425
|
+
"box",
|
|
1426
|
+
"axisX",
|
|
1427
|
+
"axisY",
|
|
1428
|
+
"solverPoint",
|
|
1429
|
+
"solverNormal"
|
|
1430
|
+
], H = {
|
|
1431
|
+
fixed: [
|
|
1432
|
+
.5,
|
|
1433
|
+
.5,
|
|
1434
|
+
.5,
|
|
1435
|
+
1
|
|
1436
|
+
],
|
|
1437
|
+
awake: [
|
|
1438
|
+
.2,
|
|
1439
|
+
.9,
|
|
1440
|
+
.3,
|
|
1441
|
+
1
|
|
1442
|
+
],
|
|
1443
|
+
sleeping: [
|
|
1444
|
+
.15,
|
|
1445
|
+
.5,
|
|
1446
|
+
.2,
|
|
1447
|
+
1
|
|
1448
|
+
],
|
|
1449
|
+
kinematic: [
|
|
1450
|
+
.3,
|
|
1451
|
+
.5,
|
|
1452
|
+
1,
|
|
1453
|
+
1
|
|
1454
|
+
],
|
|
1455
|
+
sensor: [
|
|
1456
|
+
1,
|
|
1457
|
+
.9,
|
|
1458
|
+
.2,
|
|
1459
|
+
1
|
|
1460
|
+
],
|
|
1461
|
+
joint: [
|
|
1462
|
+
1,
|
|
1463
|
+
.2,
|
|
1464
|
+
.2,
|
|
1465
|
+
1
|
|
1466
|
+
],
|
|
1467
|
+
contactPoint: [
|
|
1468
|
+
1,
|
|
1469
|
+
.2,
|
|
1470
|
+
1,
|
|
1471
|
+
1
|
|
1472
|
+
],
|
|
1473
|
+
contactNormal: [
|
|
1474
|
+
1,
|
|
1475
|
+
.6,
|
|
1476
|
+
1,
|
|
1477
|
+
1
|
|
1478
|
+
],
|
|
1479
|
+
box: [
|
|
1480
|
+
.2,
|
|
1481
|
+
.9,
|
|
1482
|
+
.9,
|
|
1483
|
+
.4
|
|
1484
|
+
],
|
|
1485
|
+
axisX: [
|
|
1486
|
+
.9,
|
|
1487
|
+
.1,
|
|
1488
|
+
.1,
|
|
1489
|
+
1
|
|
1490
|
+
],
|
|
1491
|
+
axisY: [
|
|
1492
|
+
.1,
|
|
1493
|
+
.8,
|
|
1494
|
+
.1,
|
|
1495
|
+
1
|
|
1496
|
+
],
|
|
1497
|
+
solverPoint: [
|
|
1498
|
+
1,
|
|
1499
|
+
.5,
|
|
1500
|
+
0,
|
|
1501
|
+
1
|
|
1502
|
+
],
|
|
1503
|
+
solverNormal: [
|
|
1504
|
+
1,
|
|
1505
|
+
.75,
|
|
1506
|
+
.3,
|
|
1507
|
+
1
|
|
1508
|
+
]
|
|
1509
|
+
}, U = () => {};
|
|
1510
|
+
function W(e) {
|
|
1511
|
+
e(this);
|
|
1512
|
+
}
|
|
1513
|
+
var G = Math.PI * 2, K = 4294967295, q = new i(({ wasm: e, simd: t }, n) => import("./kernel-D6zM0ozJ.js").then((r) => r.startKernel(e ?? r.defaultWasm(!(t ?? r.hasSimd())), n))), J = {
|
|
1514
|
+
dynamic: 0,
|
|
1515
|
+
fixed: 1,
|
|
1516
|
+
kinematicPosition: 2,
|
|
1517
|
+
kinematicVelocity: 2
|
|
1518
|
+
}, Y = [
|
|
1519
|
+
["substeps", "count"],
|
|
1520
|
+
["iterations", "count"],
|
|
1521
|
+
["contactHertz", "amount"],
|
|
1522
|
+
["contactDamping", "amount"],
|
|
1523
|
+
["contactMaxPush", "speed"],
|
|
1524
|
+
["restitutionThreshold", "speed"],
|
|
1525
|
+
["sleepThreshold", "speed"],
|
|
1526
|
+
["sleepTime", "amount"],
|
|
1527
|
+
["canSleep", "flag"],
|
|
1528
|
+
["blockSolver", "flag"],
|
|
1529
|
+
["jointHertz", "amount"],
|
|
1530
|
+
["jointDamping", "amount"],
|
|
1531
|
+
["jointMaxPush", "speed"],
|
|
1532
|
+
["jointGeometricStiffness", "flag"],
|
|
1533
|
+
["maxCcdSubsteps", "whole"],
|
|
1534
|
+
["minCcdDt", "amount"],
|
|
1535
|
+
["ccdTolerance", "length"],
|
|
1536
|
+
["ccdMaxAdvances", "advances"],
|
|
1537
|
+
["warmStart", "share"],
|
|
1538
|
+
["allowedPenetration", "depth"],
|
|
1539
|
+
["predictionDistance", "depth"],
|
|
1540
|
+
["staticContactHertz", "spring"],
|
|
1541
|
+
["staticContactDamping", "spring"]
|
|
1542
|
+
], X = (e) => e === "speed" || e === "length" || e === "depth", Z = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
1543
|
+
static async load({ signal: e, wasm: t, simd: n } = {}) {
|
|
1544
|
+
await q.load({
|
|
1545
|
+
wasm: t,
|
|
1546
|
+
simd: n
|
|
1547
|
+
}, e);
|
|
1548
|
+
}
|
|
1549
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
1550
|
+
return new t(e, n);
|
|
1551
|
+
}
|
|
1552
|
+
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 = q.value;
|
|
1555
|
+
if (!r) throw new e("state", "Physics2D is not loaded; call await Physics2D.load() once before new Physics2D().");
|
|
1556
|
+
this.kernel = r, this.name = n.name ?? "physics2d";
|
|
1557
|
+
let i = n.pixelsPerMeter ?? 32;
|
|
1558
|
+
if (!(i > 0) || !Number.isFinite(i)) throw RangeError("pixelsPerMeter must be a finite number above 0");
|
|
1559
|
+
this.scale = 1 / i;
|
|
1560
|
+
let { fixedTimestep: a = 1 / 60, maxSubsteps: o = 4 } = n;
|
|
1561
|
+
if (!(a > 0 && a <= 1)) throw RangeError(`fixedTimestep must be in (0, 1] seconds, got ${a}; try 1/60.`);
|
|
1562
|
+
if (!Number.isInteger(o) || o < 1) throw RangeError(`maxSubsteps must be a positive integer, got ${o}; try 4.`);
|
|
1563
|
+
this.fixedTimestep = a, this.maxSubsteps = o, this.interpolate = n.interpolate ?? !0, this.timeScale = n.timeScale ?? 1, this.paused = n.paused ?? !1;
|
|
1564
|
+
let s = n.gravity ?? [0, 9.81 * i];
|
|
1565
|
+
if (this.checkVec(s, "Gravity"), this.gravityValue = [s[0], s[1]], this.handle = r.ok(r.exports.world_create(s[0] * this.scale, s[1] * this.scale)), t) try {
|
|
1566
|
+
t.use(this);
|
|
1567
|
+
} catch (e) {
|
|
1568
|
+
throw this.dispose(), e;
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
checkVec(e, t) {
|
|
1572
|
+
let n = e;
|
|
1573
|
+
if (!n || n.length !== 2 || !Number.isFinite(n[0]) || !Number.isFinite(n[1])) throw TypeError(`${t} must be two finite numbers [x, y]; fix the value.`);
|
|
1574
|
+
}
|
|
1575
|
+
live() {
|
|
1576
|
+
if (this.disposed) throw new e("disposed", "Physics2D is disposed; create a new Physics2D.");
|
|
1577
|
+
}
|
|
1578
|
+
get isDisposed() {
|
|
1579
|
+
return this.disposed;
|
|
1580
|
+
}
|
|
1581
|
+
get gravity() {
|
|
1582
|
+
return [this.gravityValue[0], this.gravityValue[1]];
|
|
1583
|
+
}
|
|
1584
|
+
set gravity(e) {
|
|
1585
|
+
this.live(), this.checkVec(e, "Gravity"), this.kernel.ok(this.kernel.exports.world_set_gravity(this.handle, e[0] * this.scale, e[1] * this.scale)), this.gravityValue = [e[0], e[1]];
|
|
1586
|
+
}
|
|
1587
|
+
get alpha() {
|
|
1588
|
+
return this.lastAlpha;
|
|
1589
|
+
}
|
|
1590
|
+
get bodyCount() {
|
|
1591
|
+
return this.bodies.size;
|
|
1592
|
+
}
|
|
1593
|
+
get timing() {
|
|
1594
|
+
this.live();
|
|
1595
|
+
let { kernel: e, handle: t } = this, n = (n) => e.exports.world_timing(t, n);
|
|
1596
|
+
return {
|
|
1597
|
+
step: n(0),
|
|
1598
|
+
begin: n(1),
|
|
1599
|
+
broad: n(2),
|
|
1600
|
+
narrow: n(3),
|
|
1601
|
+
islands: n(4),
|
|
1602
|
+
solver: n(5),
|
|
1603
|
+
ccd: n(6),
|
|
1604
|
+
events: n(7),
|
|
1605
|
+
assembly: n(8),
|
|
1606
|
+
resolution: n(9),
|
|
1607
|
+
update: n(10),
|
|
1608
|
+
writeback: n(11),
|
|
1609
|
+
ccdBroad: n(12),
|
|
1610
|
+
ccdToi: n(13),
|
|
1611
|
+
ccdSolver: n(14)
|
|
1612
|
+
};
|
|
1613
|
+
}
|
|
1614
|
+
get counts() {
|
|
1615
|
+
this.live();
|
|
1616
|
+
let { kernel: e } = this, t = e.ok(e.exports.world_counts(this.handle)), n = e.io(t).u;
|
|
1617
|
+
return {
|
|
1618
|
+
awake: n[0],
|
|
1619
|
+
islands: n[1],
|
|
1620
|
+
boxesCompared: n[2],
|
|
1621
|
+
pairs: n[3],
|
|
1622
|
+
manifolds: n[4],
|
|
1623
|
+
coldPoints: n[5],
|
|
1624
|
+
contactsSolved: n[6],
|
|
1625
|
+
blockSolves: n[7],
|
|
1626
|
+
sequentialSolves: n[8],
|
|
1627
|
+
colours: n[9],
|
|
1628
|
+
uncoloured: n[10],
|
|
1629
|
+
treeJoints: n[11],
|
|
1630
|
+
events: n[12],
|
|
1631
|
+
steps: n[13]
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1634
|
+
get solver() {
|
|
1635
|
+
this.live();
|
|
1636
|
+
let { kernel: e } = this, t = e.io(e.ok(e.exports.world_settings(this.handle))).f, n = {};
|
|
1637
|
+
return Y.forEach(([e, r], i) => {
|
|
1638
|
+
n[e] = r === "flag" ? t[i] !== 0 : r === "spring" && t[i] === -1 ? null : X(r) ? t[i] / this.scale : t[i];
|
|
1639
|
+
}), n;
|
|
1640
|
+
}
|
|
1641
|
+
setSolver(e) {
|
|
1642
|
+
this.live();
|
|
1643
|
+
let { kernel: t } = this;
|
|
1644
|
+
for (let [t, n] of Y) {
|
|
1645
|
+
let r = e[t];
|
|
1646
|
+
if (n === "flag" || r === void 0) continue;
|
|
1647
|
+
if (r === null) {
|
|
1648
|
+
if (n === "spring") continue;
|
|
1649
|
+
throw RangeError(`Solver ${t} must be a number; got null.`);
|
|
1650
|
+
}
|
|
1651
|
+
let i = Number.isInteger(r), [a, o, s] = n === "count" ? [
|
|
1652
|
+
i && r >= 1 && r <= 65535,
|
|
1653
|
+
"a whole number from 1 to 65535",
|
|
1654
|
+
"a count such as 4"
|
|
1655
|
+
] : n === "whole" ? [
|
|
1656
|
+
i && r >= 0 && r <= 65535,
|
|
1657
|
+
"a whole number from 0 to 65535",
|
|
1658
|
+
"0 for none"
|
|
1659
|
+
] : n === "advances" ? [
|
|
1660
|
+
i && r >= 0 && r <= 4096,
|
|
1661
|
+
"a whole number from 0 to 4096",
|
|
1662
|
+
"a count such as 32"
|
|
1663
|
+
] : n === "length" ? [
|
|
1664
|
+
r * this.scale >= 1e-7 && r < Infinity,
|
|
1665
|
+
"a finite number of 1e-7 metres or more",
|
|
1666
|
+
"a distance such as 0.005"
|
|
1667
|
+
] : n === "share" ? [
|
|
1668
|
+
r >= 0 && r <= 1,
|
|
1669
|
+
"a number from 0 to 1",
|
|
1670
|
+
"1 for the whole impulse"
|
|
1671
|
+
] : n === "spring" ? [
|
|
1672
|
+
r >= 0 && r < Infinity,
|
|
1673
|
+
"a finite number of 0 or more, or null",
|
|
1674
|
+
"null for the contact spring"
|
|
1675
|
+
] : [
|
|
1676
|
+
r >= 0 && r < Infinity,
|
|
1677
|
+
"a finite number of 0 or more",
|
|
1678
|
+
"0 for none"
|
|
1679
|
+
];
|
|
1680
|
+
if (!a) throw RangeError(`Solver ${t} must be ${o}; got ${r}. Give ${s}.`);
|
|
1681
|
+
}
|
|
1682
|
+
return Y.forEach(([n, r], i) => {
|
|
1683
|
+
let a = e[n];
|
|
1684
|
+
a !== void 0 && t.ok(t.exports.world_set_setting(this.handle, i, a === null ? -1 : Number(a) * (X(r) ? this.scale : 1)));
|
|
1685
|
+
}), this;
|
|
1686
|
+
}
|
|
1687
|
+
getBodies() {
|
|
1688
|
+
return [...this.bodies];
|
|
1689
|
+
}
|
|
1690
|
+
getColliders() {
|
|
1691
|
+
return [...new Set(this.byShape.values())].sort((e, t) => e.shapes[0] - t.shapes[0]);
|
|
1692
|
+
}
|
|
1693
|
+
get colliderCount() {
|
|
1694
|
+
return new Set(this.byShape.values()).size;
|
|
1695
|
+
}
|
|
1696
|
+
getJoints() {
|
|
1697
|
+
return [...this.joints];
|
|
1698
|
+
}
|
|
1699
|
+
get jointCount() {
|
|
1700
|
+
return this.joints.size;
|
|
1701
|
+
}
|
|
1702
|
+
setup(t) {
|
|
1703
|
+
if (this.live(), this.engine && this.engine !== t) throw new e("attached", "Physics2D is attached to another engine; create one Physics2D per engine.");
|
|
1704
|
+
this.engine = t;
|
|
1705
|
+
}
|
|
1706
|
+
onPreUpdate(e) {
|
|
1707
|
+
this.update(e);
|
|
1708
|
+
}
|
|
1709
|
+
update(e) {
|
|
1710
|
+
if (this.disposed) return 0;
|
|
1711
|
+
if (!Number.isFinite(e) || e < 0) throw RangeError(`delta must be a finite, non-negative number of seconds, got ${e}.`);
|
|
1712
|
+
let t = 0;
|
|
1713
|
+
if (!this.paused) {
|
|
1714
|
+
let n = this.fixedTimestep;
|
|
1715
|
+
this.accumulator += e * this.timeScale, t = Math.min(this.maxSubsteps, Math.floor(this.accumulator / n + 1e-6));
|
|
1716
|
+
for (let e = 0; e < t; e++) this.step();
|
|
1717
|
+
this.accumulator = Math.max(0, this.accumulator - t * n) % n, this.lastAlpha = this.interpolate ? this.accumulator / n : 1;
|
|
1718
|
+
}
|
|
1719
|
+
return this.sync(), t;
|
|
1720
|
+
}
|
|
1721
|
+
step() {
|
|
1722
|
+
this.live();
|
|
1723
|
+
let e = this.fixedTimestep;
|
|
1724
|
+
if (this.emit("beforestep", e), this.disposed) return;
|
|
1725
|
+
let { kernel: t } = this;
|
|
1726
|
+
this.hookSet && this.installHooks();
|
|
1727
|
+
try {
|
|
1728
|
+
t.ok(t.exports.world_step(this.handle, e));
|
|
1729
|
+
} finally {
|
|
1730
|
+
t.hooks = null;
|
|
1731
|
+
}
|
|
1732
|
+
this.stepCount++, this.readPoses();
|
|
1733
|
+
let n = this.dispatching, r = n ? [] : this.events;
|
|
1734
|
+
this.collectEvents(r), this.dispatching = !0;
|
|
1735
|
+
try {
|
|
1736
|
+
for (let [e, t] of r) this.emit(e, t);
|
|
1737
|
+
} finally {
|
|
1738
|
+
this.dispatching = n, r.length = 0;
|
|
1739
|
+
}
|
|
1740
|
+
this.emit("afterstep", e);
|
|
1741
|
+
}
|
|
1742
|
+
readPoses() {
|
|
1743
|
+
let e = this.movers ??= [...this.bodies].filter((e) => e.type !== "fixed"), t = e.length;
|
|
1744
|
+
if (!t) return;
|
|
1745
|
+
let { kernel: n } = this, r = n.io(5 * t);
|
|
1746
|
+
for (let n = 0; n < t; n++) r.u[n] = e[n].handle;
|
|
1747
|
+
n.ok(n.exports.body_read_poses(this.handle, t));
|
|
1748
|
+
let i = n.io(5 * t).f, a = 1 / this.scale;
|
|
1749
|
+
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
|
+
}
|
|
1751
|
+
collectEvents(e) {
|
|
1752
|
+
let { kernel: t } = this, n = t.ok(t.exports.events_read(this.handle));
|
|
1753
|
+
if (n) {
|
|
1754
|
+
let r = t.io(3 * n).u;
|
|
1755
|
+
for (let t = 0; t < n; t++) {
|
|
1756
|
+
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
|
+
e.push([s ? o ? "intersectionstart" : "intersectionend" : o ? "contactstart" : "contactend", {
|
|
1760
|
+
type: s ? o ? "intersectionstart" : "intersectionend" : o ? "contactstart" : "contactend",
|
|
1761
|
+
bodyA: n.body,
|
|
1762
|
+
bodyB: i.body,
|
|
1763
|
+
colliderA: n,
|
|
1764
|
+
colliderB: i,
|
|
1765
|
+
sensor: s
|
|
1766
|
+
}]);
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
let r = t.ok(t.exports.force_events_read(this.handle));
|
|
1770
|
+
if (r) {
|
|
1771
|
+
let n = t.io(5 * r);
|
|
1772
|
+
for (let t = 0; t < r; t++) {
|
|
1773
|
+
let r = this.byShape.get(n.u[5 * t]), i = this.byShape.get(n.u[5 * t + 1]);
|
|
1774
|
+
r && i && e.push(["contactforce", {
|
|
1775
|
+
bodyA: r.body,
|
|
1776
|
+
bodyB: i.body,
|
|
1777
|
+
colliderA: r,
|
|
1778
|
+
colliderB: i,
|
|
1779
|
+
force: n.f[5 * t + 2],
|
|
1780
|
+
normal: [n.f[5 * t + 3], n.f[5 * t + 4]]
|
|
1781
|
+
}]);
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
sync(e = this.lastAlpha) {
|
|
1786
|
+
if (!this.disposed) {
|
|
1787
|
+
if (!Number.isFinite(e)) throw RangeError("Interpolation alpha must be a finite number; pass a value from 0 to 1.");
|
|
1788
|
+
this.syncAlpha = this.interpolate ? Math.min(1, Math.max(0, e)) : 1;
|
|
1789
|
+
for (let e of this.bodies) {
|
|
1790
|
+
let t = e.sprite;
|
|
1791
|
+
if (!t || e.type === "fixed" && !e.dirty) continue;
|
|
1792
|
+
let n = e.pose, r = this.syncAlpha;
|
|
1793
|
+
t.x = n[0] + (n[3] - n[0]) * r, t.y = n[1] + (n[4] - n[1]) * r;
|
|
1794
|
+
let i = (n[5] - n[2]) % G;
|
|
1795
|
+
i > Math.PI ? i -= G : i < -Math.PI && (i += G), t.rotation = n[2] + i * r, e.dirty = !1;
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
addBody(e, t = {}) {
|
|
1800
|
+
this.live();
|
|
1801
|
+
let n = t.type ?? "dynamic", r = this.scale;
|
|
1802
|
+
if (!(n in J)) throw TypeError(`Unknown body type "${n}"; use dynamic, fixed, kinematicPosition or kinematicVelocity.`);
|
|
1803
|
+
let i = t.position ?? [e?.x ?? 0, e?.y ?? 0];
|
|
1804
|
+
this.checkVec(i, "Body position");
|
|
1805
|
+
let a = t.rotation ?? e?.rotation ?? 0;
|
|
1806
|
+
if (!Number.isFinite(a)) throw RangeError("Body rotation must be a finite number of radians");
|
|
1807
|
+
let o = t.linearVelocity ?? [0, 0];
|
|
1808
|
+
this.checkVec(o, "linearVelocity");
|
|
1809
|
+
let s = t.dominance ?? 0;
|
|
1810
|
+
if (!Number.isInteger(s) || s < -127 || s > 127) throw RangeError(`dominance must be an integer from -127 to 127, got ${s}.`);
|
|
1811
|
+
let c = this.kernel.io(16);
|
|
1812
|
+
c.f.set([
|
|
1813
|
+
i[0] * r,
|
|
1814
|
+
i[1] * r,
|
|
1815
|
+
Math.cos(a),
|
|
1816
|
+
Math.sin(a),
|
|
1817
|
+
o[0] * r,
|
|
1818
|
+
o[1] * r,
|
|
1819
|
+
t.angularVelocity ?? 0,
|
|
1820
|
+
t.linearDamping ?? 0,
|
|
1821
|
+
t.angularDamping ?? 0,
|
|
1822
|
+
t.gravityScale ?? 1
|
|
1823
|
+
], 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, J[n], l)), d = new A(this, u, e, n);
|
|
1825
|
+
if (d.userData = t.userData, d.turnLocked = t.lockRotation === !0, d.params = {
|
|
1826
|
+
linearDamping: t.linearDamping ?? 0,
|
|
1827
|
+
angularDamping: t.angularDamping ?? 0,
|
|
1828
|
+
gravityScale: t.gravityScale ?? 1,
|
|
1829
|
+
canSleep: t.canSleep ?? !0,
|
|
1830
|
+
ccd: t.ccd === !0,
|
|
1831
|
+
dominance: s
|
|
1832
|
+
}, t.lockTranslation) {
|
|
1833
|
+
let e = t.lockTranslation === !0 ? [!0, !0] : t.lockTranslation;
|
|
1834
|
+
try {
|
|
1835
|
+
d.lockTranslation(e[0], e[1]);
|
|
1836
|
+
} catch (e) {
|
|
1837
|
+
throw this.kernel.exports.body_remove(this.handle, u), this.bodies.delete(d), e;
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
if (t.softCcd) try {
|
|
1841
|
+
d.softCcd = t.softCcd;
|
|
1842
|
+
} catch (e) {
|
|
1843
|
+
throw this.removeBody(d), e;
|
|
1844
|
+
}
|
|
1845
|
+
if (t.additionalMass) try {
|
|
1846
|
+
d.additionalMass = t.additionalMass;
|
|
1847
|
+
} catch (e) {
|
|
1848
|
+
throw this.kernel.exports.body_remove(this.handle, u), e;
|
|
1849
|
+
}
|
|
1850
|
+
this.bodies.add(d), this.movers = null;
|
|
1851
|
+
try {
|
|
1852
|
+
let n = t.colliders;
|
|
1853
|
+
n === void 0 && (n = Q(e));
|
|
1854
|
+
for (let e of Array.isArray(n) ? n : n ? [n] : []) this.addCollider(d, e);
|
|
1855
|
+
} catch (e) {
|
|
1856
|
+
throw this.removeBody(d), e;
|
|
1857
|
+
}
|
|
1858
|
+
return d;
|
|
1859
|
+
}
|
|
1860
|
+
addTileMap(e, t = {}) {
|
|
1861
|
+
let n = this.addBody(null, {
|
|
1862
|
+
type: "fixed",
|
|
1863
|
+
colliders: [],
|
|
1864
|
+
userData: t.userData
|
|
1865
|
+
});
|
|
1866
|
+
try {
|
|
1867
|
+
for (let r of f(e, t)) this.addCollider(n, r);
|
|
1868
|
+
} catch (e) {
|
|
1869
|
+
throw this.removeBody(n), e;
|
|
1870
|
+
}
|
|
1871
|
+
return n;
|
|
1872
|
+
}
|
|
1873
|
+
addCollider(e, t) {
|
|
1874
|
+
this.live();
|
|
1875
|
+
let n = E(t, this.scale), r = [];
|
|
1876
|
+
try {
|
|
1877
|
+
let t = [];
|
|
1878
|
+
for (let i of n) {
|
|
1879
|
+
let n = this.createShapes(e, i);
|
|
1880
|
+
r.push(...n), i.seam && t.push(...n);
|
|
1881
|
+
}
|
|
1882
|
+
if (t.length > 1) {
|
|
1883
|
+
let { kernel: e } = this;
|
|
1884
|
+
e.io(t.length).u.set(t, 0), e.ok(e.exports.shapes_join_seams(this.handle, t.length));
|
|
1885
|
+
}
|
|
1886
|
+
} catch (e) {
|
|
1887
|
+
for (let e of r) this.kernel.exports.shape_remove(this.handle, e);
|
|
1888
|
+
throw e;
|
|
1889
|
+
}
|
|
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);
|
|
1892
|
+
for (let e of r) this.byShape.set(e, a);
|
|
1893
|
+
return e.colliders.push(a), a;
|
|
1894
|
+
}
|
|
1895
|
+
createShapes(e, t) {
|
|
1896
|
+
let { kernel: n } = this, r = n.io(8 + t.geometry.length);
|
|
1897
|
+
r.u.set(t.settings, 0), r.u.set(t.geometry, 8);
|
|
1898
|
+
let i = n.ok(n.exports.shape_create(this.handle, e.handle, t.kind));
|
|
1899
|
+
return Array.from(n.io(i).u.subarray(0, i));
|
|
1900
|
+
}
|
|
1901
|
+
removeCollider(e) {
|
|
1902
|
+
if (e.isRemoved) return;
|
|
1903
|
+
for (let t of e.shapes) this.byShape.delete(t), this.kernel.exports.shape_remove(this.handle, t);
|
|
1904
|
+
e.markRemoved();
|
|
1905
|
+
let t = e.body.colliders;
|
|
1906
|
+
t.splice(t.indexOf(e), 1);
|
|
1907
|
+
}
|
|
1908
|
+
removeBody(e) {
|
|
1909
|
+
if (this.bodies.has(e)) {
|
|
1910
|
+
for (let t of [...this.joints]) (t.bodyA === e || t.bodyB === e) && this.removeJoint(t);
|
|
1911
|
+
for (let t of [...this.controllers]) t.body === e && t.dispose();
|
|
1912
|
+
for (let t of e.colliders) {
|
|
1913
|
+
for (let e of t.shapes) this.byShape.delete(e);
|
|
1914
|
+
t.markRemoved();
|
|
1915
|
+
}
|
|
1916
|
+
e.colliders.length = 0, this.bodies.delete(e), this.movers = null, this.kernel.exports.body_remove(this.handle, e.handle), e.markRemoved();
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
get hooks() {
|
|
1920
|
+
return this.hookSet;
|
|
1921
|
+
}
|
|
1922
|
+
set hooks(e) {
|
|
1923
|
+
this.live(), this.hookSet = e;
|
|
1924
|
+
let { kernel: t } = this;
|
|
1925
|
+
t.ok(t.exports.world_set_hooks(this.handle, +!!e)), this.installHooks();
|
|
1926
|
+
}
|
|
1927
|
+
installHooks() {
|
|
1928
|
+
this.kernel.hooks = this.hookSet ? {
|
|
1929
|
+
filter: (e, t) => {
|
|
1930
|
+
let n = this.byShape.get(e), r = this.byShape.get(t);
|
|
1931
|
+
if (!n || !r || !this.hookSet) return !0;
|
|
1932
|
+
let i = n.isSensor() || r.isSensor() ? this.hookSet.filterIntersection : this.hookSet.filterPair;
|
|
1933
|
+
return !i || i.call(this.hookSet, n, r) !== !1;
|
|
1934
|
+
},
|
|
1935
|
+
contact: (e, t, n, r, i) => {
|
|
1936
|
+
let a = this.byShape.get(e), o = this.byShape.get(t);
|
|
1937
|
+
if (!a || !o || !this.hookSet?.modifyContact) return 0;
|
|
1938
|
+
let s = {
|
|
1939
|
+
colliderA: a,
|
|
1940
|
+
colliderB: o,
|
|
1941
|
+
normal: [n, r],
|
|
1942
|
+
tangent: [r, -n],
|
|
1943
|
+
points: i,
|
|
1944
|
+
solverActive: !0
|
|
1945
|
+
};
|
|
1946
|
+
this.hookSet.modifyContact(s);
|
|
1947
|
+
let c = +!s.solverActive, l = this.kernel.current(5);
|
|
1948
|
+
return s.tangentVelocity && (l.f[4] = s.tangentVelocity * this.scale, c |= 16), s.friction !== void 0 && (l.f[0] = s.friction, c |= 2), s.restitution !== void 0 && (l.f[1] = s.restitution, c |= 4), s.oneWay && (l.f[2] = s.oneWay[0], l.f[3] = s.oneWay[1], c |= 8), c;
|
|
1949
|
+
}
|
|
1950
|
+
} : null;
|
|
1951
|
+
}
|
|
1952
|
+
retype(e, t) {
|
|
1953
|
+
if (this.live(), !(t in J)) throw TypeError(`Unknown body type "${t}"; use dynamic, fixed, kinematicPosition or kinematicVelocity.`);
|
|
1954
|
+
this.kernel.ok(this.kernel.exports.body_set_kind(this.handle, e.handle, J[t])), this.movers = null, e.snapshot(), e.pose.copyWithin(0, 3, 6);
|
|
1955
|
+
}
|
|
1956
|
+
projectPoint(e, t, n = !0) {
|
|
1957
|
+
this.live(), this.checkVec(e, "Point");
|
|
1958
|
+
let r = this.scale;
|
|
1959
|
+
this.writeFilter(t);
|
|
1960
|
+
let i = this.kernel.io(8);
|
|
1961
|
+
i.f.set([e[0] * r, e[1] * r], 4), i.u[6] = +!!n;
|
|
1962
|
+
let a = this.kernel.ok(this.kernel.exports.project_point(this.handle)), o = this.kernel.io(5 * a);
|
|
1963
|
+
for (let e = 0; e < a; e++) {
|
|
1964
|
+
let n = this.hitOf(o.u[5 * e], t);
|
|
1965
|
+
if (n) return {
|
|
1966
|
+
...n,
|
|
1967
|
+
point: [o.f[5 * e + 1] / r, o.f[5 * e + 2] / r],
|
|
1968
|
+
inside: o.u[5 * e + 4] !== 0,
|
|
1969
|
+
distance: o.f[5 * e + 3] / r
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
return null;
|
|
1973
|
+
}
|
|
1974
|
+
intersectionsWithBox(e, t, n) {
|
|
1975
|
+
this.live(), this.checkVec(e, "Box min"), this.checkVec(t, "Box max"), this.writeFilter(n);
|
|
1976
|
+
let r = this.scale;
|
|
1977
|
+
this.kernel.io(8).f.set([
|
|
1978
|
+
e[0] * r,
|
|
1979
|
+
e[1] * r,
|
|
1980
|
+
t[0] * r,
|
|
1981
|
+
t[1] * r
|
|
1982
|
+
], 4);
|
|
1983
|
+
let i = this.kernel.ok(this.kernel.exports.aabb_query(this.handle));
|
|
1984
|
+
return this.bodiesOf(this.kernel.io(i).u, i, n);
|
|
1985
|
+
}
|
|
1986
|
+
contactPairs(t) {
|
|
1987
|
+
if (this.live(), t.isRemoved) throw new e("state", "Collider2D was removed; ask for the contacts of a live collider.");
|
|
1988
|
+
let n = [], r = 1 / this.scale;
|
|
1989
|
+
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(11 * i);
|
|
1991
|
+
for (let e = 0; e < i; e++) {
|
|
1992
|
+
let i = this.byShape.get(o[11 * e]);
|
|
1993
|
+
if (!i) continue;
|
|
1994
|
+
let s = o[11 * e + 1], c = s >> 2, l = [];
|
|
1995
|
+
for (let t = 0; t < c; t++) l.push({
|
|
1996
|
+
point: [a[11 * e + 4 + 3 * t] * r, a[11 * e + 5 + 3 * t] * r],
|
|
1997
|
+
distance: a[11 * e + 6 + 3 * t] * r
|
|
1998
|
+
});
|
|
1999
|
+
n.push({
|
|
2000
|
+
collider: t,
|
|
2001
|
+
other: i,
|
|
2002
|
+
normal: [a[11 * e + 2], a[11 * e + 3]],
|
|
2003
|
+
points: l,
|
|
2004
|
+
impulse: a[11 * e + 10],
|
|
2005
|
+
solverActive: !!(s & 1),
|
|
2006
|
+
touching: !!(s & 2)
|
|
2007
|
+
});
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
return n;
|
|
2011
|
+
}
|
|
2012
|
+
contactPair(e, t) {
|
|
2013
|
+
return this.contactPairs(e).find((e) => e.other === t) ?? null;
|
|
2014
|
+
}
|
|
2015
|
+
intersectionPair(e, t) {
|
|
2016
|
+
return !e.isSensor() && !t.isSensor() ? !1 : this.contactPair(e, t)?.touching ?? !1;
|
|
2017
|
+
}
|
|
2018
|
+
intersectionPairsWith(e) {
|
|
2019
|
+
return this.contactPairs(e).filter((t) => t.touching && (e.isSensor() || t.other.isSensor())).map((e) => e.other);
|
|
2020
|
+
}
|
|
2021
|
+
lockTranslation(e, t, n) {
|
|
2022
|
+
this.live();
|
|
2023
|
+
let { kernel: r } = this;
|
|
2024
|
+
if (e.lockJoint >= 0 && (r.exports.joint_remove(this.handle, e.lockJoint), e.lockJoint = -1), !t && !n) return;
|
|
2025
|
+
if (this.anchor < 0) {
|
|
2026
|
+
let e = r.io(16);
|
|
2027
|
+
e.u.fill(0, 0, 16), e.f.set([
|
|
2028
|
+
0,
|
|
2029
|
+
0,
|
|
2030
|
+
1,
|
|
2031
|
+
0
|
|
2032
|
+
], 0), e.f[9] = 1, this.anchor = r.ok(r.exports.body_create(this.handle, 1, 1));
|
|
2033
|
+
}
|
|
2034
|
+
r.ok(r.exports.body_read(this.handle, e.handle));
|
|
2035
|
+
let i = r.io(11).f, [a, o] = [i[0], i[1]], s = r.io(32);
|
|
2036
|
+
s.u.fill(0, 0, 32), s.u[0] = 5, s.f.set([
|
|
2037
|
+
a,
|
|
2038
|
+
o,
|
|
2039
|
+
0,
|
|
2040
|
+
0
|
|
2041
|
+
], 1), s.f.set([
|
|
2042
|
+
1,
|
|
2043
|
+
0,
|
|
2044
|
+
1,
|
|
2045
|
+
0
|
|
2046
|
+
], 16), s.u[24] = !!t | (n ? 2 : 0), e.lockJoint = r.ok(r.exports.joint_create(this.handle, this.anchor, e.handle, 0));
|
|
2047
|
+
}
|
|
2048
|
+
addJoint(t, n, r) {
|
|
2049
|
+
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 = R(this.kernel, r, this.scale), a = this.kernel.ok(this.kernel.exports.joint_create(this.handle, t.handle, n.handle, +(r.collide === !0))), o = new B(this.kernel, this.handle, a, t, n, r.type, i, (e) => this.removeJoint(e));
|
|
2051
|
+
return this.joints.add(o), o;
|
|
2052
|
+
}
|
|
2053
|
+
removeJoint(e) {
|
|
2054
|
+
this.joints.delete(e) && e.free();
|
|
2055
|
+
}
|
|
2056
|
+
addArticulation(t, n) {
|
|
2057
|
+
if (this.live(), !this.bodies.has(t)) throw new e("state", "The root must be a body of this Physics2D and not removed.");
|
|
2058
|
+
for (let t of n) if (!this.bodies.has(t.body)) throw new e("state", "Every link body must belong to this Physics2D and not be removed.");
|
|
2059
|
+
return new M(this, t, n);
|
|
2060
|
+
}
|
|
2061
|
+
createCharacterController(e, t = {}) {
|
|
2062
|
+
if (this.live(), e.type !== "kinematicPosition") throw TypeError("Character bodies must be 'kinematicPosition'; add the body with {type: 'kinematicPosition'}.");
|
|
2063
|
+
let n = new I(this, e, t, (e) => this.controllers.delete(e));
|
|
2064
|
+
return this.controllers.add(n), n;
|
|
2065
|
+
}
|
|
2066
|
+
writeFilter(e = {}) {
|
|
2067
|
+
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] = K, t.u[3] = e.exclude && this.bodies.has(e.exclude) ? e.exclude.handle : K;
|
|
2069
|
+
}
|
|
2070
|
+
hitOf(e, t) {
|
|
2071
|
+
let n = this.byShape.get(e);
|
|
2072
|
+
if (!n || n === t?.excludeCollider) return null;
|
|
2073
|
+
let r = n.body;
|
|
2074
|
+
return t?.predicate && t.predicate(r, n) === !1 ? null : {
|
|
2075
|
+
body: r,
|
|
2076
|
+
sprite: r.sprite,
|
|
2077
|
+
collider: n
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
raycast(e, t, n = {}) {
|
|
2081
|
+
return this.rayHits(e, t, n, !0)[0] ?? null;
|
|
2082
|
+
}
|
|
2083
|
+
raycastAll(e, t, n = {}) {
|
|
2084
|
+
return this.rayHits(e, t, n, !1);
|
|
2085
|
+
}
|
|
2086
|
+
rayHits(e, t, n, r) {
|
|
2087
|
+
this.live(), this.checkVec(e, "Ray2D origin"), this.checkVec(t, "Ray2D direction");
|
|
2088
|
+
let i = Math.hypot(t[0], t[1]);
|
|
2089
|
+
if (!(i > 0)) throw RangeError("Ray2D direction must not be zero; pass a direction such as [0, 1].");
|
|
2090
|
+
let a = this.scale, o = t[0] / i, s = t[1] / i;
|
|
2091
|
+
this.writeFilter(n);
|
|
2092
|
+
let c = this.kernel.io(16);
|
|
2093
|
+
c.f.set([
|
|
2094
|
+
e[0] * a,
|
|
2095
|
+
e[1] * a,
|
|
2096
|
+
o,
|
|
2097
|
+
s,
|
|
2098
|
+
Math.min((n.maxDistance ?? 1e9) * a, 3e38)
|
|
2099
|
+
], 4), c.u[9] = n.solid ?? !0 ? 1 : 0;
|
|
2100
|
+
let l = this.kernel.ok(this.kernel.exports.ray_cast(this.handle)), u = this.kernel.io(4 * l), d = [];
|
|
2101
|
+
for (let t = 0; t < l; t++) {
|
|
2102
|
+
let i = this.hitOf(u.u[4 * t], n);
|
|
2103
|
+
if (!i) continue;
|
|
2104
|
+
let c = u.f[4 * t + 1] / a;
|
|
2105
|
+
if (d.push({
|
|
2106
|
+
...i,
|
|
2107
|
+
distance: c,
|
|
2108
|
+
point: [e[0] + o * c, e[1] + s * c],
|
|
2109
|
+
normal: [u.f[4 * t + 2] + 0, u.f[4 * t + 3] + 0]
|
|
2110
|
+
}), r) break;
|
|
2111
|
+
}
|
|
2112
|
+
return d;
|
|
2113
|
+
}
|
|
2114
|
+
overlapPoint(e, t) {
|
|
2115
|
+
this.live(), this.checkVec(e, "Point"), this.writeFilter(t), this.kernel.io(8).f.set([e[0] * this.scale, e[1] * this.scale], 4);
|
|
2116
|
+
let n = this.kernel.ok(this.kernel.exports.point_query(this.handle));
|
|
2117
|
+
return this.bodiesOf(this.kernel.io(n).u, n, t);
|
|
2118
|
+
}
|
|
2119
|
+
bodiesOf(e, t, n) {
|
|
2120
|
+
let r = /* @__PURE__ */ new Set();
|
|
2121
|
+
for (let i = 0; i < t; i++) {
|
|
2122
|
+
let t = this.hitOf(e[i], n);
|
|
2123
|
+
t && r.add(t.body);
|
|
2124
|
+
}
|
|
2125
|
+
return [...r];
|
|
2126
|
+
}
|
|
2127
|
+
overlapShape(e, t, n = 0, r) {
|
|
2128
|
+
this.live(), this.checkVec(t, "Shape2D position");
|
|
2129
|
+
let i = this.queryShape(e);
|
|
2130
|
+
this.writeFilter(r);
|
|
2131
|
+
let a = this.kernel.io(8 + i.geometry.length);
|
|
2132
|
+
a.f.set([
|
|
2133
|
+
t[0] * this.scale,
|
|
2134
|
+
t[1] * this.scale,
|
|
2135
|
+
Math.cos(n),
|
|
2136
|
+
Math.sin(n)
|
|
2137
|
+
], 4), a.u.set(i.geometry, 8);
|
|
2138
|
+
let o = this.kernel.ok(this.kernel.exports.shape_overlap(this.handle, i.kind));
|
|
2139
|
+
return this.bodiesOf(this.kernel.io(o).u, o, r);
|
|
2140
|
+
}
|
|
2141
|
+
queryShape(e) {
|
|
2142
|
+
let t = S(e, this.scale);
|
|
2143
|
+
if (t.kind > 4 || !t.convex) throw RangeError("A query shape must be a box, circle, capsule, segment or a convex polygon of at most 8 points.");
|
|
2144
|
+
return t;
|
|
2145
|
+
}
|
|
2146
|
+
castShape(e, t, n, r, i, a = 0, o = {}) {
|
|
2147
|
+
if (this.live(), this.checkVec(t, "Shape2D position"), this.checkVec(n, "Shape2D direction"), o.targetDistance !== void 0 && !(o.targetDistance >= 0 && o.targetDistance < Infinity)) throw RangeError(`targetDistance must be a finite number of 0 or more; got ${o.targetDistance}.`);
|
|
2148
|
+
let s = Math.hypot(n[0], n[1]);
|
|
2149
|
+
if (!(s > 0)) throw RangeError("Shape2D direction must not be zero; pass a direction such as [0, 1].");
|
|
2150
|
+
let c = this.queryShape(e);
|
|
2151
|
+
return this.cast(c, t[0] * this.scale, t[1] * this.scale, a, n[0] / s, n[1] / s, r * this.scale, i, o);
|
|
2152
|
+
}
|
|
2153
|
+
cast(e, t, n, r, i, a, o, s, c = {}) {
|
|
2154
|
+
this.writeFilter(s);
|
|
2155
|
+
let l = this.kernel.io(13 + e.geometry.length);
|
|
2156
|
+
l.f.set([
|
|
2157
|
+
t,
|
|
2158
|
+
n,
|
|
2159
|
+
Math.cos(r),
|
|
2160
|
+
Math.sin(r),
|
|
2161
|
+
i,
|
|
2162
|
+
a,
|
|
2163
|
+
Math.min(o, 3e38),
|
|
2164
|
+
(c.targetDistance ?? 0) * this.scale
|
|
2165
|
+
], 4), l.u[12] = c.stopAtPenetration === !1 ? 0 : 1, l.u.set(e.geometry, 13);
|
|
2166
|
+
let u = this.kernel.ok(this.kernel.exports.shape_cast(this.handle, e.kind)), d = this.kernel.io(9 * u), f = 1 / this.scale;
|
|
2167
|
+
for (let e = 0; e < u; e++) {
|
|
2168
|
+
let t = this.hitOf(d.u[9 * e], s);
|
|
2169
|
+
if (!t) continue;
|
|
2170
|
+
let n = d.f, r = n[9 * e + 8] * f, i = n[9 * e + 1] * f;
|
|
2171
|
+
return {
|
|
2172
|
+
...t,
|
|
2173
|
+
distance: i,
|
|
2174
|
+
point: [n[9 * e + 4] * f, n[9 * e + 5] * f],
|
|
2175
|
+
normal: [0 - n[9 * e + 6], 0 - n[9 * e + 7]],
|
|
2176
|
+
witnessA: [n[9 * e + 2] * f, n[9 * e + 3] * f],
|
|
2177
|
+
witnessB: [n[9 * e + 4] * f, n[9 * e + 5] * f],
|
|
2178
|
+
normalA: [n[9 * e + 6], n[9 * e + 7]],
|
|
2179
|
+
normalB: [0 - n[9 * e + 6], 0 - n[9 * e + 7]],
|
|
2180
|
+
gap: r,
|
|
2181
|
+
status: i === 0 ? "penetrating" : "converged"
|
|
2182
|
+
};
|
|
2183
|
+
}
|
|
2184
|
+
return null;
|
|
2185
|
+
}
|
|
2186
|
+
snapshot() {
|
|
2187
|
+
this.live();
|
|
2188
|
+
let { kernel: e } = this, t = e.ok(e.exports.world_snapshot(this.handle)), n = e.io(Math.ceil(t / 4)).u, r = new Uint8Array(n.buffer.slice(n.byteOffset, n.byteOffset + t)), i = [...this.bodies].map((e) => [e.handle, e.colliders.map((e) => e.shapes)]), a = new TextEncoder().encode(JSON.stringify({
|
|
2189
|
+
gravity: this.gravityValue,
|
|
2190
|
+
bodies: i,
|
|
2191
|
+
joints: [...this.joints].map((e) => e.handle),
|
|
2192
|
+
extra: [...this.bodies].map((e) => e.additionalMass)
|
|
2193
|
+
})), o = new Uint8Array(8 + a.length + r.length);
|
|
2194
|
+
return new DataView(o.buffer).setUint32(0, 1346646611, !0), new DataView(o.buffer).setUint32(4, a.length, !0), o.set(a, 8), o.set(r, 8 + a.length), o;
|
|
2195
|
+
}
|
|
2196
|
+
restore(t) {
|
|
2197
|
+
this.live();
|
|
2198
|
+
let n = new DataView(t.buffer, t.byteOffset, t.byteLength);
|
|
2199
|
+
if (t.byteLength < 8 || n.getUint32(0, !0) !== 1346646611 || 8 + n.getUint32(4, !0) > t.byteLength) throw RangeError("Physics2D.restore needs the bytes Physics2D.snapshot returned.");
|
|
2200
|
+
let r = n.getUint32(4, !0), i = JSON.parse(new TextDecoder().decode(t.subarray(8, 8 + r)));
|
|
2201
|
+
if (JSON.stringify([...this.bodies].map((e) => [e.handle, e.colliders.map((e) => e.shapes)])) !== JSON.stringify(i.bodies) || JSON.stringify([...this.joints].map((e) => e.handle)) !== JSON.stringify(i.joints)) throw new e("state", "Physics2D.restore: bodies, colliders or joints were added or removed since the snapshot; restore it before changing them, or take a new snapshot.");
|
|
2202
|
+
let a = t.subarray(8 + r), { kernel: o } = this, s = o.io(Math.ceil(a.length / 4));
|
|
2203
|
+
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
|
+
let c = 0;
|
|
2205
|
+
for (let e of this.bodies) e.snapshot(), e.pose.copyWithin(0, 3, 6), e.dirty = !0, e.restoreAdditionalMass(i.extra[c++] ?? 0);
|
|
2206
|
+
}
|
|
2207
|
+
debugRender(e = {}) {
|
|
2208
|
+
this.live();
|
|
2209
|
+
let { style: t, filter: n, ...r } = e, i = Object.keys(r).length === 0, a = (i || r.shapes ? 1 : 0) | (i || r.joints ? 2 : 0) | (i || r.contacts ? 4 : 0) | (i || r.boxes ? 8 : 0) | (r.axes ? 16 : 0) | (r.solverContacts ? 32 : 0), { kernel: o } = this, s = [];
|
|
2210
|
+
if (n) for (let e of this.bodies) n(e) || s.push(e.handle);
|
|
2211
|
+
o.io(Math.max(s.length, 1)).u.set(s, 0);
|
|
2212
|
+
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
|
+
for (let e = 0; e < c; e++) {
|
|
2214
|
+
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?.[V[f[e]]] ?? H[V[f[e]]], 4 * e);
|
|
2216
|
+
}
|
|
2217
|
+
return {
|
|
2218
|
+
vertices: d,
|
|
2219
|
+
colors: f,
|
|
2220
|
+
rgba: p
|
|
2221
|
+
};
|
|
2222
|
+
}
|
|
2223
|
+
on(e, t) {
|
|
2224
|
+
if (this.disposed) return U;
|
|
2225
|
+
let n = this.listeners.get(e);
|
|
2226
|
+
n || this.listeners.set(e, n = /* @__PURE__ */ new Set());
|
|
2227
|
+
let r = n;
|
|
2228
|
+
return r.add(t), () => {
|
|
2229
|
+
r.delete(t);
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
emit(e, t) {
|
|
2233
|
+
this.listeners.get(e)?.forEach(W, t);
|
|
2234
|
+
}
|
|
2235
|
+
dispose() {
|
|
2236
|
+
if (this.disposed) return;
|
|
2237
|
+
let e = this.engine, t = e && !e.disposed ? e.sceneOf(this) : void 0;
|
|
2238
|
+
if (t) try {
|
|
2239
|
+
t.removePlugin(this.name);
|
|
2240
|
+
return;
|
|
2241
|
+
} catch {}
|
|
2242
|
+
for (let e of [...this.controllers]) e.dispose();
|
|
2243
|
+
for (let e of [...this.joints]) this.removeJoint(e);
|
|
2244
|
+
for (let e of [...this.bodies]) this.removeBody(e);
|
|
2245
|
+
this.listeners.clear(), this.kernel.exports.world_destroy(this.handle), this.disposed = !0, this.engine = null;
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
Z.prototype[Symbol.dispose] = Z.prototype.dispose;
|
|
2249
|
+
function Q(e) {
|
|
2250
|
+
if (!e?.rect) return;
|
|
2251
|
+
let t = Math.abs(e.rect[2] * (e.scaleX ?? 1)), n = Math.abs(e.rect[3] * (e.scaleY ?? 1));
|
|
2252
|
+
if (t > 0 && n > 0) return {
|
|
2253
|
+
shape: "box",
|
|
2254
|
+
width: t,
|
|
2255
|
+
height: n,
|
|
2256
|
+
offset: [(.5 - (e.pivotX ?? 0)) * t, (.5 - (e.pivotY ?? 0)) * n]
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
//#endregion
|
|
2260
|
+
//#region src/pid.ts
|
|
2261
|
+
var $ = (e, t) => {
|
|
2262
|
+
if (!Number.isFinite(e)) throw RangeError(`${t} must be a finite number, got ${e}; fix the value.`);
|
|
2263
|
+
return e;
|
|
2264
|
+
}, ee = class {
|
|
2265
|
+
constructor({ kp: e = 1, ki: t = 0, kd: n = 0 } = {}) {
|
|
2266
|
+
this.axes = [!0, !0], this.sum = [0, 0], this.angleSum = 0, this.kp = $(e, "kp"), this.ki = $(t, "ki"), this.kd = $(n, "kd");
|
|
2267
|
+
}
|
|
2268
|
+
reset() {
|
|
2269
|
+
return this.sum = [0, 0], this.angleSum = 0, this;
|
|
2270
|
+
}
|
|
2271
|
+
linearCorrection(e, t, n, r = [0, 0]) {
|
|
2272
|
+
if (!(e > 0) || !Number.isFinite(e)) throw RangeError(`dt must be a finite number of seconds above 0, got ${e}.`);
|
|
2273
|
+
let i = t.position, a = t.linearVelocity, o = [0, 0];
|
|
2274
|
+
for (let t = 0; t < 2; t++) {
|
|
2275
|
+
if (!this.axes[t]) continue;
|
|
2276
|
+
let s = $(n[t], "target") - i[t];
|
|
2277
|
+
this.sum[t] += s * e, o[t] = this.kp * s + this.ki * this.sum[t] + this.kd * ($(r[t], "targetVelocity") - a[t]);
|
|
2278
|
+
}
|
|
2279
|
+
return o;
|
|
2280
|
+
}
|
|
2281
|
+
applyLinear(e, t, n, r = [0, 0]) {
|
|
2282
|
+
let i = this.linearCorrection(e, t, n, r), a = t.linearVelocity;
|
|
2283
|
+
return t.linearVelocity = [a[0] + i[0] * e, a[1] + i[1] * e], this;
|
|
2284
|
+
}
|
|
2285
|
+
angularCorrection(e, t, n, r = 0) {
|
|
2286
|
+
if (!(e > 0) || !Number.isFinite(e)) throw RangeError(`dt must be a finite number of seconds above 0, got ${e}.`);
|
|
2287
|
+
let i = ($(n, "target") - t.rotation) % (2 * Math.PI);
|
|
2288
|
+
return i > Math.PI ? i -= 2 * Math.PI : i < -Math.PI && (i += 2 * Math.PI), this.angleSum += i * e, this.kp * i + this.ki * this.angleSum + this.kd * ($(r, "targetVelocity") - t.angularVelocity);
|
|
2289
|
+
}
|
|
2290
|
+
applyAngular(e, t, n, r = 0) {
|
|
2291
|
+
return t.angularVelocity += this.angularCorrection(e, t, n, r) * e, this;
|
|
2292
|
+
}
|
|
2293
|
+
};
|
|
2294
|
+
//#endregion
|
|
2295
|
+
export { M as Articulation2D, A as Body2D, I as CharacterController2D, v as CoefficientCombineRule2D, k as Collider2D, B as Joint2D, Z as Physics2D, ee as PidController2D, m as interactionGroups, f as tileMapColliders };
|