@margoengine/ragdoll 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,998 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { t as e } from "./errors-CDwcEs-j.js";
5
+ import { MargoError as t } from "@margoengine/math/errors";
6
+ import { conjugateQuaternion as n, normalizeQuaternion as r, rotateByQuaternion as i, rotationToQuaternion as a } from "@margoengine/math/quat";
7
+ //#region ../shared/scene-scope.js
8
+ var o = 0, s = (e, t) => {
9
+ let n = t + o++;
10
+ for (; e.plugins?.has(n);) n += ":";
11
+ return n;
12
+ };
13
+ function c(e, t) {
14
+ return {
15
+ scene: t,
16
+ run(n) {
17
+ if (!t) return n();
18
+ let r, i = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
19
+ name: s(e, "scene-scope:"),
20
+ setup() {
21
+ r = n();
22
+ }
23
+ };
24
+ return t.use(i), t.removePlugin(i.name), r;
25
+ }
26
+ };
27
+ }
28
+ //#endregion
29
+ //#region src/ragdoll.ts
30
+ var l = 2e-6, u = [
31
+ 0,
32
+ 0,
33
+ 0,
34
+ 1
35
+ ], d = {
36
+ type: "cone",
37
+ swing: Math.PI / 4,
38
+ twist: [-Math.PI / 6, Math.PI / 6]
39
+ }, f = (e) => e[0] * (e[5] * e[10] - e[6] * e[9]) - e[4] * (e[1] * e[10] - e[2] * e[9]) + e[8] * (e[1] * e[6] - e[2] * e[5]), p = (e, t) => r(a(e, t, Math.hypot(e[0], e[1], e[2]), Math.hypot(e[4], e[5], e[6]), Math.hypot(e[8], e[9], e[10])), t), m = (e, t, n, r = 0) => {
40
+ let i = e[0], a = e[1], o = e[2], s = e[3], c = t[0], l = t[1], u = t[2], d = t[3];
41
+ return n[r] = s * c + i * d + a * u - o * l, n[r + 1] = s * l - i * u + a * d + o * c, n[r + 2] = s * u + i * l - a * c + o * d, n[r + 3] = s * d - i * c - a * l - o * u, n;
42
+ };
43
+ function h(e, t, n) {
44
+ let i = e[0] * t[0] + e[1] * t[1] + e[2] * t[2];
45
+ if (i < -.999999) {
46
+ let t = 0, r = -e[2], i = e[1];
47
+ Math.hypot(r, i) < 1e-6 && (t = e[2], r = 0, i = -e[0]), n[0] = t, n[1] = r, n[2] = i, n[3] = 0;
48
+ } else n[0] = e[1] * t[2] - e[2] * t[1], n[1] = e[2] * t[0] - e[0] * t[2], n[2] = e[0] * t[1] - e[1] * t[0], n[3] = 1 + i;
49
+ return r(n, n);
50
+ }
51
+ function g(e, t, n, r, i, a, o) {
52
+ let s = e[t] * n[r] + e[t + 1] * n[r + 1] + e[t + 2] * n[r + 2] + e[t + 3] * n[r + 3], c = 1;
53
+ s < 0 && (c = -1, s = -s);
54
+ let l = 1 - i, u = i * c;
55
+ if (s < .9995) {
56
+ let e = Math.acos(s), t = Math.sin(e);
57
+ l = Math.sin((1 - i) * e) / t, u = Math.sin(i * e) / t * c;
58
+ }
59
+ let d = 0;
60
+ for (let i = 0; i < 4; i++) {
61
+ let s = e[t + i] * l + n[r + i] * u;
62
+ a[o + i] = s, d += s * s;
63
+ }
64
+ d = Math.sqrt(d);
65
+ for (let e = 0; e < 4; e++) a[o + e] /= d;
66
+ }
67
+ function _(e) {
68
+ let t = e.store.parents, n = /* @__PURE__ */ new Map();
69
+ for (let r of e.meshes.values()) {
70
+ let e = t[r.id];
71
+ if (e === -1) continue;
72
+ let i = n.get(e);
73
+ i || n.set(e, i = []), i.push(r);
74
+ }
75
+ return n;
76
+ }
77
+ function v(e, t, n, r, i) {
78
+ let a = n, o = r, s = y(e);
79
+ if (s) {
80
+ let e = Infinity, n = -Infinity, r = 0, i = s.length / 3, c = [
81
+ 0,
82
+ 1,
83
+ 2
84
+ ].map((e) => {
85
+ let t = 0;
86
+ for (let n = e; n < s.length; n += 3) t += s[n];
87
+ return t / i;
88
+ });
89
+ for (let i = 0; i < s.length; i += 3) {
90
+ let a = s[i] - c[0], o = s[i + 1] - c[1], l = s[i + 2] - c[2], u = a * t[0] + o * t[1] + l * t[2];
91
+ e = Math.min(e, u), n = Math.max(n, u), r += a * a + o * o + l * l - u * u;
92
+ }
93
+ a = n - e, o = Math.sqrt(r / i);
94
+ }
95
+ return [2 / (i * o * o), 12 / (i * (3 * o * o + a * a))];
96
+ }
97
+ function y(e) {
98
+ if (e.type === "hull") return e.points;
99
+ if (e.type === "capsule") return null;
100
+ let t = /* @__PURE__ */ new Float64Array(24);
101
+ for (let n = 0; n < 8; n++) {
102
+ let r = i([
103
+ 0,
104
+ 1,
105
+ 2
106
+ ].map((t) => e.halfExtents[t] * (n >> t & 1 ? 1 : -1)), e.rotation);
107
+ for (let i = 0; i < 3; i++) t[n * 3 + i] = r[i] + e.center[i];
108
+ }
109
+ return t;
110
+ }
111
+ function b(t) {
112
+ let n = (e) => Array.prototype.every.call(e, Number.isFinite);
113
+ if (t?.type === "capsule") return t;
114
+ if (t?.type === "hull") {
115
+ if (!t.points || t.points.length < 12 || t.points.length % 3 || !n(t.points)) throw e("A ragdoll hull needs at least four finite xyz points; pass points that span a volume.");
116
+ return t;
117
+ }
118
+ if (t?.type === "box") {
119
+ if (!(t.halfExtents?.length === 3 && n(t.halfExtents) && t.halfExtents.every((e) => e > 0)) || !(t.center?.length === 3 && n(t.center)) || !(t.rotation?.length === 4 && n(t.rotation)) || Math.abs(Math.hypot(...t.rotation) - 1) > .001) throw e("A ragdoll box needs positive halfExtents, a finite center and a unit rotation; pass {center, halfExtents, rotation}.");
120
+ return t;
121
+ }
122
+ throw e("A ragdoll shape must be of type 'capsule', 'hull' or 'box'.");
123
+ }
124
+ var x = (t, n, r) => {
125
+ if (typeof t != "number" || !(t > 0) || t === Infinity) throw e(`Ragdoll ${n} must be a positive finite number; ${r}`);
126
+ return t;
127
+ }, S = class r {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
128
+ constructor(t, r) {
129
+ if (this.physics = t, this.unsubscribe = [], this.loose = 0, this.stepped = 0, this.disposed = !1, this.q = /* @__PURE__ */ new Float64Array(4), this.r = /* @__PURE__ */ new Float64Array(4), this.s = /* @__PURE__ */ new Float64Array(4), this.v = /* @__PURE__ */ new Float64Array(3), this.w = /* @__PURE__ */ new Float64Array(3), this.c = /* @__PURE__ */ new Float64Array(3), this.pose = /* @__PURE__ */ new Float64Array(7), this.turnBy = /* @__PURE__ */ new Float64Array(4), this.turned = /* @__PURE__ */ new Float64Array(4), this.omega = /* @__PURE__ */ new Float64Array(), typeof t?.addBody != "function" || t.isDisposed) throw e("A ragdoll needs a live Physics world: new Ragdoll(physics, {root: hips}).");
130
+ if (!r || r.root === void 0 == (r.bones === void 0)) throw e("Ragdoll options need exactly one of root (the hips node) or bones (a list of nodes).");
131
+ let a = r.minLength ?? .05, o = r.thickness ?? .2, s = r.density ?? 1e3;
132
+ if (!(a >= 0) || a === Infinity) throw e("Ragdoll minLength must be a finite number of at least 0; pass metres, e.g. 0.05.");
133
+ if (x(o, "thickness", "pass a fraction of the bone length, e.g. 0.2."), x(s, "density", "pass kilograms per cubic metre, e.g. 1000."), this.stiffness = r.limitStiffness ?? .2, !(this.stiffness >= 0 && this.stiffness <= 1)) throw e("Ragdoll limitStiffness must be from 0 to 1; pass the share of the error removed per step.");
134
+ let c = this.boneList(r, a), l = c[0].node.engine;
135
+ this.engine = l, this.name = "ragdoll:" + c[0].node.id;
136
+ let u = l.store, h = u.parents, g = /* @__PURE__ */ new Map();
137
+ c.forEach((t, n) => {
138
+ if (t.node.engine !== l) throw e("Ragdoll bones must all belong to one engine; pass nodes of one skeleton.");
139
+ if (g.has(t.node.id)) throw e("Ragdoll bones must be distinct; list each node once.");
140
+ g.set(t.node.id, n);
141
+ });
142
+ let y = c.map((e) => {
143
+ let t = h[e.node.id];
144
+ return t !== -1 && g.has(t) ? g.get(t) : -1;
145
+ });
146
+ if (y.filter((e) => e === -1).length !== 1) throw e("Ragdoll bones must form one tree; list every node between the root bone and each other bone (each bone's parent node is a bone).");
147
+ let S = [], C = (e) => {
148
+ S.push(e), c.forEach((t, n) => y[n] === e && C(n));
149
+ };
150
+ C(y.indexOf(-1)), u.tick(!1);
151
+ let w = _(l), T = (t, n) => {
152
+ let r = t.matrix;
153
+ if (!(f(r) > 0)) throw e("Ragdoll bones need positive world scales; remove zero or mirrored bone scales.");
154
+ return n[0] = r[12], n[1] = r[13], n[2] = r[14], n.set(p(r, this.q), 3), n;
155
+ }, E = [], D = [], O = /* @__PURE__ */ new Map(), k = r.collisionGroups ?? 2147516415;
156
+ if (!Number.isInteger(k) || k < 0 || k > 4294967295) throw e("Ragdoll collisionGroups must be a packed 32-bit integer; build it with collisionGroups().");
157
+ this.settings = {
158
+ collisionGroups: k,
159
+ friction: r.friction ?? .6,
160
+ linearDamping: r.linearDamping ?? .05,
161
+ angularDamping: r.angularDamping ?? .8,
162
+ limitStiffness: this.stiffness
163
+ };
164
+ try {
165
+ for (let a of S) {
166
+ let l = c[a], u = l.node, f = T(u, /* @__PURE__ */ new Float64Array(7)), p = [
167
+ f[0],
168
+ f[1],
169
+ f[2]
170
+ ], h = [
171
+ f[3],
172
+ f[4],
173
+ f[5],
174
+ f[6]
175
+ ], g = n(h), _ = (w.get(u.id) ?? []).map((e) => [
176
+ e.matrix[12],
177
+ e.matrix[13],
178
+ e.matrix[14]
179
+ ]), S = [
180
+ 0,
181
+ 1,
182
+ 0
183
+ ], C = 0, k = 0;
184
+ if (_.length) {
185
+ let e = [
186
+ 0,
187
+ 1,
188
+ 2
189
+ ].map((e) => _.reduce((t, n) => t + n[e], 0) / _.length - p[e]), t = e, n = Math.hypot(e[0], e[1], e[2]);
190
+ if (n < .001) for (let e of _) {
191
+ let r = [
192
+ e[0] - p[0],
193
+ e[1] - p[1],
194
+ e[2] - p[2]
195
+ ], i = Math.hypot(r[0], r[1], r[2]);
196
+ i > n && (n = i, t = r);
197
+ }
198
+ if (n > 1e-6) {
199
+ S = i(t.map((e) => e / n), g), C = n;
200
+ for (let e of _) {
201
+ let r = [
202
+ e[0] - p[0],
203
+ e[1] - p[1],
204
+ e[2] - p[2]
205
+ ], i = (r[0] * t[0] + r[1] * t[1] + r[2] * t[2]) / n;
206
+ k = Math.max(k, Math.hypot(r[0] - t[0] / n * i, r[1] - t[1] / n * i, r[2] - t[2] / n * i));
207
+ }
208
+ }
209
+ }
210
+ let A = l.length === void 0 ? C : x(l.length, "bone length", "pass metres, or leave it out for a bone with children.");
211
+ if (!(A > 0)) throw e(`Ragdoll bone "${u.name || u.id}" has no children to measure; give it a length in metres.`);
212
+ let j = l.radius === void 0 ? Math.max(A * o, k / 2) : x(l.radius, "bone radius", "pass metres, or leave it out for thickness times the length."), M = Math.PI * j * j * (2 * Math.max(A / 2 - j, .001) + 4 / 3 * j), N = l.mass === void 0 ? M * s : x(l.mass, "bone mass", "pass kilograms, or leave it out for the capsule volume times density."), P = Float64Array.from(S), F = b(l.shape ?? { type: "capsule" }), I = t.addBody(null, {
213
+ type: r.active ? "dynamic" : "kinematicPosition",
214
+ position: p,
215
+ rotation: h,
216
+ linearDamping: this.settings.linearDamping,
217
+ angularDamping: this.settings.angularDamping,
218
+ userData: {
219
+ ragdoll: this,
220
+ node: u
221
+ },
222
+ colliders: this.collider(F, P, A, j, N)
223
+ });
224
+ D.push(I);
225
+ let L = y[a] === -1 ? -1 : O.get(y[a]), R = new Float64Array([
226
+ 0,
227
+ 0,
228
+ 0,
229
+ 1
230
+ ]), z = null, B = null, V = null;
231
+ if (L !== -1) {
232
+ let e = E[L], t = n(e.world.subarray(3, 7)), a = i([
233
+ p[0] - e.world[0],
234
+ p[1] - e.world[1],
235
+ p[2] - e.world[2]
236
+ ], t);
237
+ m(t, h, R);
238
+ let o = l.joint ?? r.joint ?? d;
239
+ ({joint: z, cone: B, spec: V} = this.join(e.body, I, o, a, R));
240
+ }
241
+ let H = {
242
+ node: u,
243
+ body: I,
244
+ joint: z,
245
+ parent: L,
246
+ length: A,
247
+ radius: j,
248
+ mass: N,
249
+ shape: F,
250
+ axis: P,
251
+ rest: R,
252
+ bind: new Float64Array([
253
+ 0,
254
+ 0,
255
+ 0,
256
+ 1
257
+ ]),
258
+ spec: V,
259
+ cone: B,
260
+ inertia: Float64Array.from(v(F, P, A, j, N)),
261
+ prev: f.slice(),
262
+ cur: f.slice(),
263
+ animated: /* @__PURE__ */ new Float64Array(7),
264
+ written: (/* @__PURE__ */ new Float64Array(7)).fill(NaN),
265
+ local: /* @__PURE__ */ new Float64Array(7),
266
+ world: f,
267
+ target: [
268
+ f[0],
269
+ f[1],
270
+ f[2],
271
+ f[3],
272
+ f[4],
273
+ f[5],
274
+ f[6]
275
+ ],
276
+ limp: !!r.active,
277
+ weight: +!!r.active,
278
+ fade: 0,
279
+ fadeFrom: 0,
280
+ fadeAt: 0,
281
+ landed: !1,
282
+ jump: !r.active
283
+ };
284
+ this.readLocal(u, H.animated), H.local.set(H.animated), this.fit(H, E), O.set(a, E.length), E.push(H);
285
+ }
286
+ } catch (e) {
287
+ for (let e of E) e.joint?.remove();
288
+ for (let e of D) e.remove();
289
+ throw e;
290
+ }
291
+ this.parts = E, this.bones = E, this.omega = new Float64Array(E.length * 3), this.loose = r.active ? E.length : 0, this.unsubscribe.push(t.on("beforestep", () => this.beforeStep()), t.on("afterstep", () => this.afterStep()));
292
+ }
293
+ static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
294
+ let i = new r(e, n), a = i.engine;
295
+ return c(a, a.sceneOf(e)).run(() => {
296
+ if (a.hasPlugin(i.name)) throw i.dispose(), new t("attached", "This skeleton already has an attached ragdoll; dispose it first, or use the one you have.");
297
+ try {
298
+ a.use(i);
299
+ } catch (e) {
300
+ throw i.dispose(), e;
301
+ }
302
+ return i;
303
+ });
304
+ }
305
+ collider(e, t, n, r, i) {
306
+ let { friction: a, collisionGroups: o } = this.settings, s = {
307
+ mass: i,
308
+ friction: a,
309
+ collisionGroups: o
310
+ };
311
+ return e.type === "hull" ? {
312
+ shape: "convexHull",
313
+ points: e.points,
314
+ ...s
315
+ } : e.type === "box" ? {
316
+ shape: "box",
317
+ halfExtents: e.halfExtents,
318
+ offset: e.center,
319
+ rotation: e.rotation,
320
+ ...s
321
+ } : {
322
+ shape: "capsule",
323
+ halfHeight: Math.max(n / 2 - r, .001),
324
+ radius: r,
325
+ offset: [
326
+ t[0] * (n / 2),
327
+ t[1] * (n / 2),
328
+ t[2] * (n / 2)
329
+ ],
330
+ rotation: [...h([
331
+ 0,
332
+ 1,
333
+ 0
334
+ ], t, /* @__PURE__ */ new Float64Array(4))],
335
+ ...s
336
+ };
337
+ }
338
+ setShape(t, n) {
339
+ this.live();
340
+ let r = t && "body" in t ? t.node : t, i = this.parts.find((e) => e.node === r);
341
+ if (!i) throw e("That node is not a bone of this ragdoll; pass a node or part of ragdoll.bones.");
342
+ b(n);
343
+ let a = [...i.body.colliders];
344
+ i.body.addCollider(this.collider(n, i.axis, i.length, i.radius, i.mass));
345
+ for (let e of a) i.body.removeCollider(e);
346
+ return i.shape = n, i.inertia.set(v(n, i.axis, i.length, i.radius, i.mass)), this;
347
+ }
348
+ boneList(t, n) {
349
+ if (t.bones !== void 0) {
350
+ if (!Array.isArray(t.bones) || !t.bones.length) throw e("Ragdoll bones must be a non-empty array of nodes or {node} objects.");
351
+ return t.bones.map((t) => {
352
+ let n = t && "node" in t ? t : { node: t };
353
+ if (!n.node || typeof n.node.setQuaternion != "function" || !n.node.engine) throw e("Ragdoll bones must be skeleton nodes (meshes) or {node} objects; pass model.nodes[i].");
354
+ return n.node.check(), n;
355
+ });
356
+ }
357
+ let r = t.root;
358
+ if (typeof r?.setQuaternion != "function" || !r.engine) throw e("Ragdoll root must be a skeleton node (a mesh), e.g. the hips: model.nodes[i].");
359
+ r.check(), r.engine.store.tick(!1);
360
+ let i = _(r.engine), a = [], o = (e) => {
361
+ let t = i.get(e.id);
362
+ if (!t?.length) return;
363
+ let r = e.matrix;
364
+ if (!(Math.max(...t.map((e) => Math.hypot(e.matrix[12] - r[12], e.matrix[13] - r[13], e.matrix[14] - r[14]))) < n && a.length)) {
365
+ a.push({ node: e });
366
+ for (let e of t) o(e);
367
+ }
368
+ };
369
+ if (o(r), !a.length) throw e("Ragdoll root has no child bones; pass the hips of a skeleton, or list bones with lengths.");
370
+ return a;
371
+ }
372
+ join(t, n, r, i, a) {
373
+ let o = this.physics, s = i;
374
+ if (r?.type === "free" || r?.type === "cone") {
375
+ let i = null;
376
+ if (r.type === "cone") {
377
+ let t = r.swing ?? Math.PI / 4, [n, a] = r.twist ?? [-Math.PI / 6, Math.PI / 6];
378
+ if (!(t >= 0 && t <= Math.PI) || !(n <= a && n >= -Math.PI && a <= Math.PI)) throw e("Ragdoll cone limits need a swing from 0 to π and a twist [min, max] within -π to π; pass radians.");
379
+ i = {
380
+ swing: t,
381
+ min: n,
382
+ max: a
383
+ };
384
+ }
385
+ if (!i) return {
386
+ joint: o.addJoint(t, n, {
387
+ type: "spherical",
388
+ anchorA: s,
389
+ anchorB: [
390
+ 0,
391
+ 0,
392
+ 0
393
+ ]
394
+ }),
395
+ cone: i,
396
+ spec: { type: "free" }
397
+ };
398
+ let c = h([
399
+ 1,
400
+ 0,
401
+ 0
402
+ ], [
403
+ 0,
404
+ 1,
405
+ 0
406
+ ], /* @__PURE__ */ new Float64Array(4)), l = m(a, c, /* @__PURE__ */ new Float64Array(4));
407
+ return {
408
+ joint: o.addJoint(t, n, {
409
+ type: "cone",
410
+ anchorA: s,
411
+ anchorB: [
412
+ 0,
413
+ 0,
414
+ 0
415
+ ],
416
+ frameA: [
417
+ l[0],
418
+ l[1],
419
+ l[2],
420
+ l[3]
421
+ ],
422
+ frameB: [
423
+ c[0],
424
+ c[1],
425
+ c[2],
426
+ c[3]
427
+ ],
428
+ swing: i.swing,
429
+ twist: [i.min, i.max]
430
+ }),
431
+ cone: null,
432
+ spec: {
433
+ type: "cone",
434
+ swing: i.swing,
435
+ twist: [i.min, i.max]
436
+ }
437
+ };
438
+ }
439
+ if (r?.type !== "hinge") throw e("Unknown ragdoll joint type; use 'cone', 'hinge' or 'free'.");
440
+ let c = r.axis, l = Array.isArray(c) && c.length === 3 ? Math.hypot(c[0], c[1], c[2]) : NaN;
441
+ if (!(l > 0) || l === Infinity) throw e("Ragdoll hinge axis must be a nonzero finite [x, y, z] in the bone frame; e.g. [1, 0, 0].");
442
+ let [u, d] = r.limits ?? [-Math.PI * .75, 0];
443
+ if (!(u <= d && u >= -Math.PI && d <= Math.PI)) throw e("Ragdoll hinge limits need [min, max] within -π to π; pass radians.");
444
+ let f = [
445
+ c[0] / l,
446
+ c[1] / l,
447
+ c[2] / l
448
+ ], p = h([
449
+ 1,
450
+ 0,
451
+ 0
452
+ ], f, /* @__PURE__ */ new Float64Array(4)), g = m(a, p, /* @__PURE__ */ new Float64Array(4)), _ = (e) => [
453
+ e[0],
454
+ e[1],
455
+ e[2],
456
+ e[3]
457
+ ];
458
+ return {
459
+ joint: o.addJoint(t, n, {
460
+ type: "revolute",
461
+ frameA: _(g),
462
+ frameB: _(p),
463
+ anchorA: s,
464
+ anchorB: [
465
+ 0,
466
+ 0,
467
+ 0
468
+ ],
469
+ limits: [u, d]
470
+ }),
471
+ cone: null,
472
+ spec: {
473
+ type: "hinge",
474
+ axis: [
475
+ f[0],
476
+ f[1],
477
+ f[2]
478
+ ],
479
+ limits: [u, d]
480
+ }
481
+ };
482
+ }
483
+ get active() {
484
+ return this.loose > 0;
485
+ }
486
+ get weight() {
487
+ let e = 0;
488
+ for (let t of this.parts) e = Math.max(e, t.weight);
489
+ return e;
490
+ }
491
+ set weight(t) {
492
+ if (!(t >= 0 && t <= 1)) throw e("Ragdoll weight must be from 0 to 1.");
493
+ for (let e of this.parts) (e.limp || !this.loose) && (e.weight = t, e.fade = 0);
494
+ }
495
+ get rootPosition() {
496
+ let e = this.live().parts[0].cur;
497
+ return [
498
+ e[0],
499
+ e[1],
500
+ e[2]
501
+ ];
502
+ }
503
+ limbBones(t) {
504
+ if (t === void 0) return this.parts;
505
+ let n = /* @__PURE__ */ new Set();
506
+ for (let r of Array.isArray(t) ? t : [t]) {
507
+ let t = r && "body" in r ? r.node : r, i = this.parts.find((e) => e.node === t);
508
+ if (!i) throw e("That node is not a bone of this ragdoll; pass nodes or parts of ragdoll.bones.");
509
+ n.add(i);
510
+ }
511
+ for (let e of this.parts) e.parent !== -1 && n.has(this.parts[e.parent]) && n.add(e);
512
+ return [...n];
513
+ }
514
+ activate(e) {
515
+ this.live();
516
+ let t = this.physics.fixedTimestep, r = this.stepped >= 2;
517
+ this.engine.store.tick(!1);
518
+ for (let i of this.limbBones(e)) {
519
+ if (i.limp) continue;
520
+ this.fit(i, this.parts), i.limp = !0, i.weight = 1, i.fade = 0, this.loose++, i.body.setType("dynamic");
521
+ let { prev: e, cur: a } = i;
522
+ if (!r) continue;
523
+ if (i.body.setLinearVelocity([
524
+ (a[0] - e[0]) / t,
525
+ (a[1] - e[1]) / t,
526
+ (a[2] - e[2]) / t
527
+ ]), m(a.subarray(3, 7), n(e.subarray(3, 7), this.q), this.r), this.r[3] < 0) for (let e = 0; e < 4; e++) this.r[e] = -this.r[e];
528
+ let o = Math.hypot(this.r[0], this.r[1], this.r[2]), s = 2 * Math.atan2(o, this.r[3]), c = o > 1e-9 ? s / o / t : 0;
529
+ i.body.setAngularVelocity([
530
+ this.r[0] * c,
531
+ this.r[1] * c,
532
+ this.r[2] * c
533
+ ]);
534
+ }
535
+ return this;
536
+ }
537
+ deactivate(t = .3, n) {
538
+ if (this.live(), !(t >= 0) || t === Infinity) throw e("Ragdoll blend duration must be a finite number of seconds, 0 or more.");
539
+ let r = this.limbBones(n);
540
+ if (!this.loose) return this;
541
+ this.physicsLocals();
542
+ for (let e of r) e.limp && (e.body.setType("kinematicPosition"), e.limp = !1, e.jump = !0, e.fade = t > 0 ? 1 / t : 0, e.fadeFrom = e.weight, e.fadeAt = 0, t || (e.weight = 0), this.loose--);
543
+ return this;
544
+ }
545
+ part(e) {
546
+ return this.parts.find((t) => t.node === e);
547
+ }
548
+ debugRender(t = {}) {
549
+ this.live();
550
+ let { capsules: n = !0, limits: r = !0, bones: a = !0, limitScale: o = .12 } = t;
551
+ if (!(o > 0) || !Number.isFinite(o)) throw e(`Ragdoll debug limitScale must be a positive finite number of metres (got ${o}).`);
552
+ let s = [], c = [], l = (e, t, n) => {
553
+ s.push(e[0], e[1], e[2], t[0], t[1], t[2]), c.push(n[0], n[1], n[2], n[3], n[0], n[1], n[2], n[3]);
554
+ }, u = (e) => {
555
+ let t = Math.abs(e[1]) < .9 ? [
556
+ 0,
557
+ 1,
558
+ 0
559
+ ] : [
560
+ 1,
561
+ 0,
562
+ 0
563
+ ], n = t[1] * e[2] - t[2] * e[1], r = t[2] * e[0] - t[0] * e[2], i = t[0] * e[1] - t[1] * e[0], a = Math.hypot(n, r, i) || 1;
564
+ return n /= a, r /= a, i /= a, [[
565
+ n,
566
+ r,
567
+ i
568
+ ], [
569
+ e[1] * i - e[2] * r,
570
+ e[2] * n - e[0] * i,
571
+ e[0] * r - e[1] * n
572
+ ]];
573
+ }, d = (e, t, n = 1) => [
574
+ e[0] + t[0] * n,
575
+ e[1] + t[1] * n,
576
+ e[2] + t[2] * n
577
+ ], f = this.parts.map((e) => ({
578
+ pos: e.body.valid ? e.body.position : [
579
+ e.cur[0],
580
+ e.cur[1],
581
+ e.cur[2]
582
+ ],
583
+ rot: e.body.valid ? e.body.rotation : [
584
+ e.cur[3],
585
+ e.cur[4],
586
+ e.cur[5],
587
+ e.cur[6]
588
+ ]
589
+ }));
590
+ for (let e = 0; e < this.parts.length; e++) {
591
+ let t = this.parts[e], { pos: s, rot: c } = f[e], p = i(t.axis, c), h = t.limp ? t.weight < 1 ? [
592
+ 1,
593
+ .58,
594
+ .15,
595
+ 1
596
+ ] : [
597
+ .25,
598
+ .9,
599
+ .4,
600
+ 1
601
+ ] : t.weight > 0 ? [
602
+ .2,
603
+ .85,
604
+ 1,
605
+ 1
606
+ ] : [
607
+ .35,
608
+ .6,
609
+ 1,
610
+ 1
611
+ ];
612
+ if (n) {
613
+ if (t.shape.type === "capsule") {
614
+ let e = t.radius, n = Math.max(t.length / 2 - e, .001), r = d(s, p, t.length / 2), i = d(r, p, n), a = d(r, p, -n), [o, c] = u(p);
615
+ for (let t = 0; t < 16; t++) {
616
+ let n = t / 16 * Math.PI * 2, r = (t + 1) / 16 * Math.PI * 2, s = Math.cos(n) * e, u = Math.sin(n) * e, f = Math.cos(r) * e, p = Math.sin(r) * e, m = [
617
+ o[0] * s + c[0] * u,
618
+ o[1] * s + c[1] * u,
619
+ o[2] * s + c[2] * u
620
+ ], g = [
621
+ o[0] * f + c[0] * p,
622
+ o[1] * f + c[1] * p,
623
+ o[2] * f + c[2] * p
624
+ ];
625
+ l(d(i, m), d(i, g), h), l(d(a, m), d(a, g), h);
626
+ }
627
+ for (let t of [
628
+ o,
629
+ c,
630
+ [
631
+ -o[0],
632
+ -o[1],
633
+ -o[2]
634
+ ],
635
+ [
636
+ -c[0],
637
+ -c[1],
638
+ -c[2]
639
+ ]
640
+ ]) {
641
+ let n = d(i, t, e), r = d(a, t, e);
642
+ l(n, r, h), l(n, d(i, p, e), h), l(r, d(a, p, -e), h);
643
+ }
644
+ } else if (t.shape.type === "box") {
645
+ let e = [], { center: n, halfExtents: r, rotation: a } = t.shape;
646
+ for (let t = 0; t < 8; t++) {
647
+ let o = i([
648
+ r[0] * (t & 1 ? 1 : -1),
649
+ r[1] * (t & 2 ? 1 : -1),
650
+ r[2] * (t & 4 ? 1 : -1)
651
+ ], a), l = [
652
+ o[0] + n[0],
653
+ o[1] + n[1],
654
+ o[2] + n[2]
655
+ ], u = i(l, c);
656
+ e.push([
657
+ s[0] + u[0],
658
+ s[1] + u[1],
659
+ s[2] + u[2]
660
+ ]);
661
+ }
662
+ for (let t = 0; t < 8; t++) for (let n of [
663
+ 1,
664
+ 2,
665
+ 4
666
+ ]) t & n || l(e[t], e[t | n], h);
667
+ } else {
668
+ let e = t.shape.points, n = Math.floor(e.length / 3), r = [];
669
+ for (let t = 0; t < n; t++) {
670
+ let n = i([
671
+ e[t * 3],
672
+ e[t * 3 + 1],
673
+ e[t * 3 + 2]
674
+ ], c);
675
+ r.push([
676
+ s[0] + n[0],
677
+ s[1] + n[1],
678
+ s[2] + n[2]
679
+ ]);
680
+ }
681
+ for (let e = 0; e < n; e++) l(r[e], r[(e + 1) % n], h);
682
+ }
683
+ }
684
+ if (a && (l(s, d(s, p, t.length), [
685
+ .95,
686
+ .95,
687
+ .85,
688
+ 1
689
+ ]), t.parent !== -1 && l(f[t.parent].pos, s, [
690
+ .9,
691
+ .78,
692
+ .3,
693
+ .9
694
+ ])), r && t.parent !== -1 && t.spec) {
695
+ let e = m(f[t.parent].rot, t.rest, /* @__PURE__ */ new Float64Array(4)), n = [
696
+ 1,
697
+ .8,
698
+ .2,
699
+ 1
700
+ ];
701
+ if (t.spec.type === "cone") {
702
+ let r = i(t.axis, e), a = t.spec.swing ?? Math.PI / 4, [c, f] = u(r), m = d(s, r, o * Math.cos(a)), h = o * Math.sin(a);
703
+ for (let e = 0; e < 16; e++) {
704
+ let t = e / 16 * Math.PI * 2, r = (e + 1) / 16 * Math.PI * 2, i = [
705
+ m[0] + (c[0] * Math.cos(t) + f[0] * Math.sin(t)) * h,
706
+ m[1] + (c[1] * Math.cos(t) + f[1] * Math.sin(t)) * h,
707
+ m[2] + (c[2] * Math.cos(t) + f[2] * Math.sin(t)) * h
708
+ ];
709
+ l(i, [
710
+ m[0] + (c[0] * Math.cos(r) + f[0] * Math.sin(r)) * h,
711
+ m[1] + (c[1] * Math.cos(r) + f[1] * Math.sin(r)) * h,
712
+ m[2] + (c[2] * Math.cos(r) + f[2] * Math.sin(r)) * h
713
+ ], n), e % 4 == 0 && l(s, i, n);
714
+ }
715
+ let g = Math.acos(Math.max(-1, Math.min(1, p[0] * r[0] + p[1] * r[1] + p[2] * r[2])));
716
+ l(s, d(s, p, o), g > a + .02 ? [
717
+ 1,
718
+ .25,
719
+ .2,
720
+ 1
721
+ ] : [
722
+ .3,
723
+ 1,
724
+ .5,
725
+ 1
726
+ ]);
727
+ } else if (t.spec.type === "hinge") {
728
+ let r = i(t.spec.axis, e), a = i(t.axis, e), c = a[0] * r[0] + a[1] * r[1] + a[2] * r[2], f = a[0] - r[0] * c, m = a[1] - r[1] * c, h = a[2] - r[2] * c, g = Math.hypot(f, m, h), _ = g > 1e-4 ? [
729
+ f / g,
730
+ m / g,
731
+ h / g
732
+ ] : u(r)[0], v = [
733
+ r[1] * _[2] - r[2] * _[1],
734
+ r[2] * _[0] - r[0] * _[2],
735
+ r[0] * _[1] - r[1] * _[0]
736
+ ], [y, b] = t.spec.limits ?? [-Math.PI * .75, 0], x = (e) => [
737
+ s[0] + (_[0] * Math.cos(e) + v[0] * Math.sin(e)) * o,
738
+ s[1] + (_[1] * Math.cos(e) + v[1] * Math.sin(e)) * o,
739
+ s[2] + (_[2] * Math.cos(e) + v[2] * Math.sin(e)) * o
740
+ ];
741
+ l(d(s, r, -o * .5), d(s, r, o * .5), [
742
+ 1,
743
+ .5,
744
+ .15,
745
+ 1
746
+ ]), l(s, x(y), n), l(s, x(b), n);
747
+ for (let e = 0; e < 12; e++) l(x(y + (b - y) * e / 12), x(y + (b - y) * (e + 1) / 12), n);
748
+ l(s, d(s, p, o), [
749
+ .3,
750
+ 1,
751
+ .5,
752
+ 1
753
+ ]);
754
+ } else {
755
+ let e = o * .35;
756
+ l([
757
+ s[0] - e,
758
+ s[1],
759
+ s[2]
760
+ ], [
761
+ s[0] + e,
762
+ s[1],
763
+ s[2]
764
+ ], [
765
+ .8,
766
+ .85,
767
+ .95,
768
+ 1
769
+ ]), l([
770
+ s[0],
771
+ s[1] - e,
772
+ s[2]
773
+ ], [
774
+ s[0],
775
+ s[1] + e,
776
+ s[2]
777
+ ], [
778
+ .8,
779
+ .85,
780
+ .95,
781
+ 1
782
+ ]), l([
783
+ s[0],
784
+ s[1],
785
+ s[2] - e
786
+ ], [
787
+ s[0],
788
+ s[1],
789
+ s[2] + e
790
+ ], [
791
+ .8,
792
+ .85,
793
+ .95,
794
+ 1
795
+ ]);
796
+ }
797
+ }
798
+ }
799
+ return {
800
+ vertices: new Float32Array(s),
801
+ colors: new Float32Array(c)
802
+ };
803
+ }
804
+ setup(e) {
805
+ if (this.disposed) throw new t("disposed", "Ragdoll is disposed; create a new Ragdoll.");
806
+ if (e !== this.engine) throw new t("attached", "Ragdoll belongs to its bones' engine; attach it to that engine.");
807
+ this.attached = e;
808
+ }
809
+ onPreUpdate(t) {
810
+ if (this.disposed) return;
811
+ if (this.physics.isDisposed || !this.parts[0].body.valid) {
812
+ this.dispose();
813
+ return;
814
+ }
815
+ if (!Number.isFinite(t) || t < 0) throw e("Invalid ragdoll delta; pass a finite delta of 0 or more seconds.");
816
+ let n = this.engine.store;
817
+ n.tick(!1);
818
+ for (let e of this.parts) if (this.track(e), !e.limp && e.fade > 0) {
819
+ e.fadeAt = Math.min(1, e.fadeAt + e.fade * t);
820
+ let n = e.fadeAt;
821
+ e.weight = e.fadeFrom * (1 - n * n * (3 - 2 * n)), n >= 1 && (e.weight = 0, e.fade = 0, e.landed = !0);
822
+ }
823
+ this.loose && this.physicsLocals(), this.write(), n.tick(!1);
824
+ for (let e of this.parts) {
825
+ if (e.limp) continue;
826
+ let t = e.node.matrix, n = e.target;
827
+ p(t, this.q), n[0] = t[12], n[1] = t[13], n[2] = t[14], n[3] = this.q[0], n[4] = this.q[1], n[5] = this.q[2], n[6] = this.q[3];
828
+ }
829
+ }
830
+ fit(e, t) {
831
+ let r = this.engine.store.parents[e.node.id], i = e.parent === -1 ? r === -1 ? null : this.engine.meshes.get(r) : t[e.parent].node, a = p(e.node.matrix, /* @__PURE__ */ new Float64Array(4));
832
+ i && m(n(p(i.matrix, this.q), this.q), a, a);
833
+ let o = this.readLocal(e.node, this.pose);
834
+ m(n(a, a), o.subarray(3, 7), e.bind), Math.hypot(e.bind[0], e.bind[1], e.bind[2]) < l && e.bind.set(u);
835
+ }
836
+ readLocal(e, t) {
837
+ let n = e.engine.store.transforms, r = e.id;
838
+ return t[0] = n.x[r], t[1] = n.y[r], t[2] = n.z[r], t[3] = n.qx[r], t[4] = n.qy[r], t[5] = n.qz[r], t[6] = n.qw[r], t;
839
+ }
840
+ track(e) {
841
+ let t = this.readLocal(e.node, this.pose), n = e.written;
842
+ for (let r = 0; r < 7; r++) if (t[r] !== n[r]) {
843
+ e.animated.set(t);
844
+ return;
845
+ }
846
+ }
847
+ physicsLocals() {
848
+ let e = this.physics.alpha;
849
+ for (let t of this.parts) {
850
+ let { prev: r, cur: i, world: a, local: o } = t;
851
+ for (let t = 0; t < 3; t++) a[t] = r[t] + (i[t] - r[t]) * e;
852
+ if (g(r, 3, i, 3, e, a, 3), !t.limp) continue;
853
+ if (o[0] = t.animated[0], o[1] = t.animated[1], o[2] = t.animated[2], t.parent !== -1) {
854
+ let e = this.parts[t.parent].world;
855
+ n(e.subarray(3, 7), this.q), m(this.q, a.subarray(3, 7), this.r), m(this.r, t.bind, o, 3);
856
+ continue;
857
+ }
858
+ let s = this.engine.store.parents[t.node.id];
859
+ if (s === -1) {
860
+ o.set(a), m(a.subarray(3, 7), t.bind, o, 3);
861
+ continue;
862
+ }
863
+ let c = this.engine.meshes.get(s).matrix;
864
+ n(p(c, this.q), this.q), m(this.q, a.subarray(3, 7), this.r), m(this.r, t.bind, o, 3);
865
+ let l = a[0] - c[12], u = a[1] - c[13], d = a[2] - c[14], h = f(c);
866
+ o[0] = (l * (c[5] * c[10] - c[6] * c[9]) + u * (c[8] * c[6] - c[4] * c[10]) + d * (c[4] * c[9] - c[8] * c[5])) / h, o[1] = (l * (c[9] * c[2] - c[1] * c[10]) + u * (c[0] * c[10] - c[8] * c[2]) + d * (c[8] * c[1] - c[0] * c[9])) / h, o[2] = (l * (c[1] * c[6] - c[5] * c[2]) + u * (c[4] * c[2] - c[0] * c[6]) + d * (c[0] * c[5] - c[4] * c[1])) / h;
867
+ }
868
+ }
869
+ write() {
870
+ for (let e of this.parts) {
871
+ let { animated: t, local: n, written: r, node: i, weight: a } = e;
872
+ if (!a && !e.landed) continue;
873
+ if (e.landed = !1, g(t, 3, n, 3, a, r, 3), e.parent === -1) for (let e = 0; e < 3; e++) r[e] = t[e] + (n[e] - t[e]) * a;
874
+ else for (let e = 0; e < 3; e++) r[e] = t[e];
875
+ i.setPosition([
876
+ r[0],
877
+ r[1],
878
+ r[2]
879
+ ]), i.setQuaternion([
880
+ r[3],
881
+ r[4],
882
+ r[5],
883
+ r[6]
884
+ ]);
885
+ let o = i.engine.store.transforms, s = i.id;
886
+ r[0] = o.x[s], r[1] = o.y[s], r[2] = o.z[s], r[3] = o.qx[s], r[4] = o.qy[s], r[5] = o.qz[s], r[6] = o.qw[s];
887
+ }
888
+ }
889
+ beforeStep() {
890
+ if (!this.disposed && this.parts[0].body.valid) {
891
+ for (let e of this.parts) {
892
+ if (e.limp) continue;
893
+ let t = e.target, n = [
894
+ t[0],
895
+ t[1],
896
+ t[2]
897
+ ], r = [
898
+ t[3],
899
+ t[4],
900
+ t[5],
901
+ t[6]
902
+ ];
903
+ e.jump && (e.jump = !1, e.body.setPosition(n), e.body.setRotation(r), e.cur.set(t)), e.body.setNextPosition(n), e.body.setNextRotation(r);
904
+ }
905
+ this.loose && this.limits();
906
+ }
907
+ }
908
+ limits() {
909
+ let e = this.parts, t = this.omega, r = this.stiffness, { q: a, r: o, s, v: c, w: l } = this, u = !1;
910
+ for (let n = 0; n < e.length; n++) {
911
+ let r = e[n].body.angularVelocity;
912
+ t[n * 3] = r[0], t[n * 3 + 1] = r[1], t[n * 3 + 2] = r[2];
913
+ }
914
+ for (let t = 1; t < e.length; t++) {
915
+ let d = e[t], f = d.cone;
916
+ if (!f || !d.limp) continue;
917
+ let p = e[d.parent], h = p.body.rotation, g = d.body.rotation;
918
+ if (m(h, d.rest, s), n(s, a), m(a, g, o), o[3] < 0) for (let e = 0; e < 4; e++) o[e] = -o[e];
919
+ let _ = d.axis, v = o[0] * _[0] + o[1] * _[1] + o[2] * _[2], y = 2 * Math.atan2(v, o[3]);
920
+ y > Math.PI && (y -= 2 * Math.PI);
921
+ let b = Math.hypot(v, o[3]);
922
+ b > 1e-9 ? (a[0] = -_[0] * v / b, a[1] = -_[1] * v / b, a[2] = -_[2] * v / b, a[3] = o[3] / b) : a.set([
923
+ 0,
924
+ 0,
925
+ 0,
926
+ 1
927
+ ]), m(o, a, o);
928
+ let x = Math.hypot(o[0], o[1], o[2]), S = 2 * Math.atan2(x, Math.abs(o[3])), C = p.limp;
929
+ if (S > f.swing && x > 1e-9) {
930
+ let e = o[3] < 0 ? -1 : 1;
931
+ c[0] = o[0] / x * e, c[1] = o[1] / x * e, c[2] = o[2] / x * e, i(c, s, c), u = this.push(t, d.parent, c, C) || u, this.turn(d, c, -(S - f.swing) * r);
932
+ }
933
+ if (y > f.max || y < f.min) {
934
+ i(_, g, l);
935
+ let e = y - f.max;
936
+ y < f.min && (e = f.min - y, l[0] = -l[0], l[1] = -l[1], l[2] = -l[2]), u = this.push(t, d.parent, l, C) || u, this.turn(d, l, -e * r);
937
+ }
938
+ }
939
+ if (u) for (let n = 0; n < e.length; n++) e[n].limp && e[n].body.setAngularVelocity([
940
+ t[n * 3],
941
+ t[n * 3 + 1],
942
+ t[n * 3 + 2]
943
+ ]);
944
+ }
945
+ turn(e, t, n) {
946
+ let r = n / 2, i = Math.sin(r), a = this.turnBy;
947
+ a[0] = t[0] * i, a[1] = t[1] * i, a[2] = t[2] * i, a[3] = Math.cos(r);
948
+ let o = m(a, e.body.rotation, this.turned);
949
+ e.body.setRotation([
950
+ o[0],
951
+ o[1],
952
+ o[2],
953
+ o[3]
954
+ ]);
955
+ }
956
+ push(e, t, n, r) {
957
+ let i = this.omega, a = e * 3, o = t * 3, s = (i[a] - i[o]) * n[0] + (i[a + 1] - i[o + 1]) * n[1] + (i[a + 2] - i[o + 2]) * n[2];
958
+ if (s <= 0) return !1;
959
+ let c = this.inverseInertia(this.parts[e], n), l = r ? this.inverseInertia(this.parts[t], n) : 0, u = c + l;
960
+ if (!(u > 0)) return !1;
961
+ let d = (0 - s) / u;
962
+ for (let e = 0; e < 3; e++) i[a + e] += d * c * n[e], i[o + e] -= d * l * n[e];
963
+ return !0;
964
+ }
965
+ inverseInertia(e, t) {
966
+ let n = this.c, r = e.cur.subarray(3, 7);
967
+ i(e.axis, r, n);
968
+ let a = n[0] * t[0] + n[1] * t[1] + n[2] * t[2], o = a * a;
969
+ return 1 / (o / e.inertia[0] + (1 - o) / e.inertia[1]);
970
+ }
971
+ afterStep() {
972
+ if (!this.disposed && this.parts[0].body.valid) {
973
+ this.stepped++;
974
+ for (let e of this.parts) {
975
+ e.prev.set(e.cur);
976
+ let t = e.body.position, n = e.body.rotation;
977
+ e.cur[0] = t[0], e.cur[1] = t[1], e.cur[2] = t[2], e.cur[3] = n[0], e.cur[4] = n[1], e.cur[5] = n[2], e.cur[6] = n[3];
978
+ }
979
+ }
980
+ }
981
+ live() {
982
+ if (this.disposed) throw new t("disposed", "Ragdoll is disposed; create a new Ragdoll.");
983
+ return this;
984
+ }
985
+ dispose() {
986
+ if (this.disposed) return;
987
+ this.disposed = !0;
988
+ for (let e of this.unsubscribe) e();
989
+ if (this.unsubscribe.length = 0, !this.physics.isDisposed) for (let e of this.parts) e.joint?.remove(), e.body.remove();
990
+ let e = this.attached;
991
+ this.attached = void 0, e?.sceneOf(this)?.removePlugin(this.name);
992
+ }
993
+ [Symbol.dispose ?? Symbol()]() {
994
+ this.dispose();
995
+ }
996
+ };
997
+ //#endregion
998
+ export { S as Ragdoll };