@margoengine/ragdoll 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,21 +1,22 @@
1
- // @margo/core-built 0.1.0
1
+ // @margo/core-built 1.0.0
2
2
  import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
3
 
4
- import { MargoError as e } from "@margoengine/math/errors";
5
- import { conjugateQuaternion as t, normalizeQuaternion as n, rotateByQuaternion as r, rotationToQuaternion as i } from "@margoengine/math/quat";
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";
6
7
  //#region ../shared/scene-scope.js
7
- var a = 0, o = (e, t) => {
8
- let n = t + a++;
8
+ var o = 0, s = (e, t) => {
9
+ let n = t + o++;
9
10
  for (; e.plugins?.has(n);) n += ":";
10
11
  return n;
11
12
  };
12
- function s(e, t) {
13
+ function c(e, t) {
13
14
  return {
14
15
  scene: t,
15
16
  run(n) {
16
17
  if (!t) return n();
17
- let r, i = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("0.1.0",import.meta.url),
18
- name: o(e, "scene-scope:"),
18
+ let r, i = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
19
+ name: s(e, "scene-scope:"),
19
20
  setup() {
20
21
  r = n();
21
22
  }
@@ -26,28 +27,28 @@ function s(e, t) {
26
27
  }
27
28
  //#endregion
28
29
  //#region src/ragdoll.ts
29
- var c = 2e-6, l = [
30
+ var l = 2e-6, u = [
30
31
  0,
31
32
  0,
32
33
  0,
33
34
  1
34
- ], u = {
35
+ ], d = {
35
36
  type: "cone",
36
37
  swing: Math.PI / 4,
37
38
  twist: [-Math.PI / 6, Math.PI / 6]
38
- }, d = (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]), f = (e, t) => n(i(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), p = (e, t, n, r = 0) => {
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) => {
39
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];
40
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;
41
42
  };
42
- function m(e, t, r) {
43
+ function h(e, t, n) {
43
44
  let i = e[0] * t[0] + e[1] * t[1] + e[2] * t[2];
44
45
  if (i < -.999999) {
45
- let t = 0, n = -e[2], i = e[1];
46
- Math.hypot(n, i) < 1e-6 && (t = e[2], n = 0, i = -e[0]), r[0] = t, r[1] = n, r[2] = i, r[3] = 0;
47
- } else r[0] = e[1] * t[2] - e[2] * t[1], r[1] = e[2] * t[0] - e[0] * t[2], r[2] = e[0] * t[1] - e[1] * t[0], r[3] = 1 + i;
48
- return n(r, r);
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);
49
50
  }
50
- function h(e, t, n, r, i, a, o) {
51
+ function g(e, t, n, r, i, a, o) {
51
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;
52
53
  s < 0 && (c = -1, s = -s);
53
54
  let l = 1 - i, u = i * c;
@@ -63,7 +64,7 @@ function h(e, t, n, r, i, a, o) {
63
64
  d = Math.sqrt(d);
64
65
  for (let e = 0; e < 4; e++) a[o + e] /= d;
65
66
  }
66
- function g(e) {
67
+ function _(e) {
67
68
  let t = e.store.parents, n = /* @__PURE__ */ new Map();
68
69
  for (let r of e.meshes.values()) {
69
70
  let e = t[r.id];
@@ -73,8 +74,8 @@ function g(e) {
73
74
  }
74
75
  return n;
75
76
  }
76
- function _(e, t, n, r, i) {
77
- let a = n, o = r, s = v(e);
77
+ function v(e, t, n, r, i) {
78
+ let a = n, o = r, s = y(e);
78
79
  if (s) {
79
80
  let e = Infinity, n = -Infinity, r = 0, i = s.length / 3, c = [
80
81
  0,
@@ -93,206 +94,206 @@ function _(e, t, n, r, i) {
93
94
  }
94
95
  return [2 / (i * o * o), 12 / (i * (3 * o * o + a * a))];
95
96
  }
96
- function v(e) {
97
+ function y(e) {
97
98
  if (e.type === "hull") return e.points;
98
99
  if (e.type === "capsule") return null;
99
100
  let t = /* @__PURE__ */ new Float64Array(24);
100
101
  for (let n = 0; n < 8; n++) {
101
- let i = r([
102
+ let r = i([
102
103
  0,
103
104
  1,
104
105
  2
105
106
  ].map((t) => e.halfExtents[t] * (n >> t & 1 ? 1 : -1)), e.rotation);
106
- for (let r = 0; r < 3; r++) t[n * 3 + r] = i[r] + e.center[r];
107
+ for (let i = 0; i < 3; i++) t[n * 3 + i] = r[i] + e.center[i];
107
108
  }
108
109
  return t;
109
110
  }
110
- function y(e) {
111
- let t = (e) => Array.prototype.every.call(e, Number.isFinite);
112
- if (e?.type === "capsule") return e;
113
- if (e?.type === "hull") {
114
- if (!e.points || e.points.length < 12 || e.points.length % 3 || !t(e.points)) throw RangeError("A ragdoll hull needs at least four finite xyz points; pass points that span a volume.");
115
- return e;
116
- }
117
- if (e?.type === "box") {
118
- if (!(e.halfExtents?.length === 3 && t(e.halfExtents) && e.halfExtents.every((e) => e > 0)) || !(e.center?.length === 3 && t(e.center)) || !(e.rotation?.length === 4 && t(e.rotation)) || Math.abs(Math.hypot(...e.rotation) - 1) > .001) throw RangeError("A ragdoll box needs positive halfExtents, a finite center and a unit rotation; pass {center, halfExtents, rotation}.");
119
- return e;
120
- }
121
- throw RangeError("A ragdoll shape must be of type 'capsule', 'hull' or 'box'.");
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'.");
122
123
  }
123
- var b = (e, t, n) => {
124
- if (typeof e != "number" || !(e > 0) || e === Infinity) throw RangeError(`Ragdoll ${t} must be a positive finite number; ${n}`);
125
- return e;
126
- }, x = class n {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
127
- constructor(e, n) {
128
- if (this.physics = e, 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 e?.addBody != "function" || e.isDisposed) throw TypeError("A ragdoll needs a live Physics world: new Ragdoll(physics, {root: hips}).");
129
- if (!n || n.root === void 0 == (n.bones === void 0)) throw TypeError("Ragdoll options need exactly one of root (the hips node) or bones (a list of nodes).");
130
- let i = n.minLength ?? .05, a = n.thickness ?? .2, o = n.density ?? 1e3;
131
- if (!(i >= 0) || i === Infinity) throw RangeError("Ragdoll minLength must be a finite number of at least 0; pass metres, e.g. 0.05.");
132
- if (b(a, "thickness", "pass a fraction of the bone length, e.g. 0.2."), b(o, "density", "pass kilograms per cubic metre, e.g. 1000."), this.stiffness = n.limitStiffness ?? .2, !(this.stiffness >= 0 && this.stiffness <= 1)) throw RangeError("Ragdoll limitStiffness must be from 0 to 1; pass the share of the error removed per step.");
133
- let s = this.boneList(n, i), c = s[0].node.engine;
134
- this.engine = c, this.name = "ragdoll:" + s[0].node.id;
135
- let l = c.store, m = l.parents, h = /* @__PURE__ */ new Map();
136
- s.forEach((e, t) => {
137
- if (e.node.engine !== c) throw TypeError("Ragdoll bones must all belong to one engine; pass nodes of one skeleton.");
138
- if (h.has(e.node.id)) throw TypeError("Ragdoll bones must be distinct; list each node once.");
139
- h.set(e.node.id, t);
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);
140
141
  });
141
- let v = s.map((e) => {
142
- let t = m[e.node.id];
143
- return t !== -1 && h.has(t) ? h.get(t) : -1;
142
+ let y = c.map((e) => {
143
+ let t = h[e.node.id];
144
+ return t !== -1 && g.has(t) ? g.get(t) : -1;
144
145
  });
145
- if (v.filter((e) => e === -1).length !== 1) throw TypeError("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).");
146
- let x = [], S = (e) => {
147
- x.push(e), s.forEach((t, n) => v[n] === e && S(n));
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));
148
149
  };
149
- S(v.indexOf(-1)), l.tick(!1);
150
- let C = g(c), w = (e, t) => {
151
- let n = e.matrix;
152
- if (!(d(n) > 0)) throw RangeError("Ragdoll bones need positive world scales; remove zero or mirrored bone scales.");
153
- return t[0] = n[12], t[1] = n[13], t[2] = n[14], t.set(f(n, this.q), 3), t;
154
- }, T = [], E = [], D = /* @__PURE__ */ new Map(), O = n.collisionGroups ?? 2147516415;
155
- if (!Number.isInteger(O) || O < 0 || O > 4294967295) throw RangeError("Ragdoll collisionGroups must be a packed 32-bit integer; build it with collisionGroups().");
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().");
156
157
  this.settings = {
157
- collisionGroups: O,
158
- friction: n.friction ?? .6,
159
- linearDamping: n.linearDamping ?? .05,
160
- angularDamping: n.angularDamping ?? .8,
158
+ collisionGroups: k,
159
+ friction: r.friction ?? .6,
160
+ linearDamping: r.linearDamping ?? .05,
161
+ angularDamping: r.angularDamping ?? .8,
161
162
  limitStiffness: this.stiffness
162
163
  };
163
164
  try {
164
- for (let i of x) {
165
- let c = s[i], l = c.node, d = w(l, /* @__PURE__ */ new Float64Array(7)), f = [
166
- d[0],
167
- d[1],
168
- d[2]
169
- ], m = [
170
- d[3],
171
- d[4],
172
- d[5],
173
- d[6]
174
- ], h = t(m), g = (C.get(l.id) ?? []).map((e) => [
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) => [
175
176
  e.matrix[12],
176
177
  e.matrix[13],
177
178
  e.matrix[14]
178
- ]), x = [
179
+ ]), S = [
179
180
  0,
180
181
  1,
181
182
  0
182
- ], S = 0, O = 0;
183
- if (g.length) {
183
+ ], C = 0, k = 0;
184
+ if (_.length) {
184
185
  let e = [
185
186
  0,
186
187
  1,
187
188
  2
188
- ].map((e) => g.reduce((t, n) => t + n[e], 0) / g.length - f[e]), t = e, n = Math.hypot(e[0], e[1], e[2]);
189
- if (n < .001) for (let e of g) {
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 _) {
190
191
  let r = [
191
- e[0] - f[0],
192
- e[1] - f[1],
193
- e[2] - f[2]
192
+ e[0] - p[0],
193
+ e[1] - p[1],
194
+ e[2] - p[2]
194
195
  ], i = Math.hypot(r[0], r[1], r[2]);
195
196
  i > n && (n = i, t = r);
196
197
  }
197
198
  if (n > 1e-6) {
198
- x = r(t.map((e) => e / n), h), S = n;
199
- for (let e of g) {
199
+ S = i(t.map((e) => e / n), g), C = n;
200
+ for (let e of _) {
200
201
  let r = [
201
- e[0] - f[0],
202
- e[1] - f[1],
203
- e[2] - f[2]
202
+ e[0] - p[0],
203
+ e[1] - p[1],
204
+ e[2] - p[2]
204
205
  ], i = (r[0] * t[0] + r[1] * t[1] + r[2] * t[2]) / n;
205
- O = Math.max(O, Math.hypot(r[0] - t[0] / n * i, r[1] - t[1] / n * i, r[2] - t[2] / n * i));
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));
206
207
  }
207
208
  }
208
209
  }
209
- let k = c.length === void 0 ? S : b(c.length, "bone length", "pass metres, or leave it out for a bone with children.");
210
- if (!(k > 0)) throw TypeError(`Ragdoll bone "${l.name || l.id}" has no children to measure; give it a length in metres.`);
211
- let A = c.radius === void 0 ? Math.max(k * a, O / 2) : b(c.radius, "bone radius", "pass metres, or leave it out for thickness times the length."), j = Math.PI * A * A * (2 * Math.max(k / 2 - A, .001) + 4 / 3 * A), M = c.mass === void 0 ? j * o : b(c.mass, "bone mass", "pass kilograms, or leave it out for the capsule volume times density."), N = Float64Array.from(x), P = y(c.shape ?? { type: "capsule" }), F = e.addBody(null, {
212
- type: n.active ? "dynamic" : "kinematicPosition",
213
- position: f,
214
- rotation: m,
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,
215
216
  linearDamping: this.settings.linearDamping,
216
217
  angularDamping: this.settings.angularDamping,
217
218
  userData: {
218
219
  ragdoll: this,
219
- node: l
220
+ node: u
220
221
  },
221
- colliders: this.collider(P, N, k, A, M)
222
+ colliders: this.collider(F, P, A, j, N)
222
223
  });
223
- E.push(F);
224
- let I = v[i] === -1 ? -1 : D.get(v[i]), L = new Float64Array([
224
+ D.push(I);
225
+ let L = y[a] === -1 ? -1 : O.get(y[a]), R = new Float64Array([
225
226
  0,
226
227
  0,
227
228
  0,
228
229
  1
229
- ]), R = null, z = null, B = null;
230
- if (I !== -1) {
231
- let e = T[I], i = t(e.world.subarray(3, 7)), a = r([
232
- f[0] - e.world[0],
233
- f[1] - e.world[1],
234
- f[2] - e.world[2]
235
- ], i);
236
- p(i, m, L);
237
- let o = c.joint ?? n.joint ?? u;
238
- ({joint: R, cone: z, spec: B} = this.join(e.body, F, o, a, L));
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));
239
240
  }
240
- let V = {
241
- node: l,
242
- body: F,
243
- joint: R,
244
- parent: I,
245
- length: k,
246
- radius: A,
247
- mass: M,
248
- shape: P,
249
- axis: N,
250
- rest: L,
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,
251
252
  bind: new Float64Array([
252
253
  0,
253
254
  0,
254
255
  0,
255
256
  1
256
257
  ]),
257
- spec: B,
258
- cone: z,
259
- inertia: Float64Array.from(_(P, N, k, A, M)),
260
- prev: d.slice(),
261
- cur: d.slice(),
258
+ spec: V,
259
+ cone: B,
260
+ inertia: Float64Array.from(v(F, P, A, j, N)),
261
+ prev: f.slice(),
262
+ cur: f.slice(),
262
263
  animated: /* @__PURE__ */ new Float64Array(7),
263
264
  written: (/* @__PURE__ */ new Float64Array(7)).fill(NaN),
264
265
  local: /* @__PURE__ */ new Float64Array(7),
265
- world: d,
266
+ world: f,
266
267
  target: [
267
- d[0],
268
- d[1],
269
- d[2],
270
- d[3],
271
- d[4],
272
- d[5],
273
- d[6]
268
+ f[0],
269
+ f[1],
270
+ f[2],
271
+ f[3],
272
+ f[4],
273
+ f[5],
274
+ f[6]
274
275
  ],
275
- limp: !!n.active,
276
- weight: +!!n.active,
276
+ limp: !!r.active,
277
+ weight: +!!r.active,
277
278
  fade: 0,
278
279
  fadeFrom: 0,
279
280
  fadeAt: 0,
280
281
  landed: !1,
281
- jump: !n.active
282
+ jump: !r.active
282
283
  };
283
- this.readLocal(l, V.animated), V.local.set(V.animated), this.fit(V, T), D.set(i, T.length), T.push(V);
284
+ this.readLocal(u, H.animated), H.local.set(H.animated), this.fit(H, E), O.set(a, E.length), E.push(H);
284
285
  }
285
286
  } catch (e) {
286
- for (let e of T) e.joint?.remove();
287
- for (let e of E) e.remove();
287
+ for (let e of E) e.joint?.remove();
288
+ for (let e of D) e.remove();
288
289
  throw e;
289
290
  }
290
- this.parts = T, this.bones = T, this.omega = new Float64Array(T.length * 3), this.loose = n.active ? T.length : 0, this.unsubscribe.push(e.on("beforestep", () => this.beforeStep()), e.on("afterstep", () => this.afterStep()));
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()));
291
292
  }
292
- static attach(t, r) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
293
- let i = new n(t, r), a = i.engine;
294
- return s(a, a.sceneOf(t)).run(() => {
295
- if (a.hasPlugin(i.name)) throw i.dispose(), new e("attached", "This skeleton already has an attached ragdoll; dispose it first, or use the one you have.");
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.");
296
297
  try {
297
298
  a.use(i);
298
299
  } catch (e) {
@@ -326,7 +327,7 @@ var b = (e, t, n) => {
326
327
  t[1] * (n / 2),
327
328
  t[2] * (n / 2)
328
329
  ],
329
- rotation: [...m([
330
+ rotation: [...h([
330
331
  0,
331
332
  1,
332
333
  0
@@ -334,67 +335,67 @@ var b = (e, t, n) => {
334
335
  ...s
335
336
  };
336
337
  }
337
- setShape(e, t) {
338
+ setShape(t, n) {
338
339
  this.live();
339
- let n = e && "body" in e ? e.node : e, r = this.parts.find((e) => e.node === n);
340
- if (!r) throw RangeError("That node is not a bone of this ragdoll; pass a node or part of ragdoll.bones.");
341
- y(t);
342
- let i = [...r.body.colliders];
343
- r.body.addCollider(this.collider(t, r.axis, r.length, r.radius, r.mass));
344
- for (let e of i) r.body.removeCollider(e);
345
- return r.shape = t, r.inertia.set(_(t, r.axis, r.length, r.radius, r.mass)), this;
346
- }
347
- boneList(e, t) {
348
- if (e.bones !== void 0) {
349
- if (!Array.isArray(e.bones) || !e.bones.length) throw TypeError("Ragdoll bones must be a non-empty array of nodes or {node} objects.");
350
- return e.bones.map((e) => {
351
- let t = e && "node" in e ? e : { node: e };
352
- if (!t.node || typeof t.node.setQuaternion != "function" || !t.node.engine) throw TypeError("Ragdoll bones must be skeleton nodes (meshes) or {node} objects; pass model.nodes[i].");
353
- return t.node.check(), t;
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;
354
355
  });
355
356
  }
356
- let n = e.root;
357
- if (typeof n?.setQuaternion != "function" || !n.engine) throw TypeError("Ragdoll root must be a skeleton node (a mesh), e.g. the hips: model.nodes[i].");
358
- n.check(), n.engine.store.tick(!1);
359
- let r = g(n.engine), i = [], a = (e) => {
360
- let n = r.get(e.id);
361
- if (!n?.length) return;
362
- let o = e.matrix;
363
- if (!(Math.max(...n.map((e) => Math.hypot(e.matrix[12] - o[12], e.matrix[13] - o[13], e.matrix[14] - o[14]))) < t && i.length)) {
364
- i.push({ node: e });
365
- for (let e of n) a(e);
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);
366
367
  }
367
368
  };
368
- if (a(n), !i.length) throw TypeError("Ragdoll root has no child bones; pass the hips of a skeleton, or list bones with lengths.");
369
- return i;
370
- }
371
- join(e, t, n, r, i) {
372
- let a = this.physics, o = r;
373
- if (n?.type === "free" || n?.type === "cone") {
374
- let r = null;
375
- if (n.type === "cone") {
376
- let e = n.swing ?? Math.PI / 4, [t, i] = n.twist ?? [-Math.PI / 6, Math.PI / 6];
377
- if (!(e >= 0 && e <= Math.PI) || !(t <= i && t >= -Math.PI && i <= Math.PI)) throw RangeError("Ragdoll cone limits need a swing from 0 to π and a twist [min, max] within -π to π; pass radians.");
378
- r = {
379
- swing: e,
380
- min: t,
381
- max: i
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
382
383
  };
383
384
  }
384
- if (!r) return {
385
- joint: a.addJoint(e, t, {
385
+ if (!i) return {
386
+ joint: o.addJoint(t, n, {
386
387
  type: "spherical",
387
- anchorA: o,
388
+ anchorA: s,
388
389
  anchorB: [
389
390
  0,
390
391
  0,
391
392
  0
392
393
  ]
393
394
  }),
394
- cone: r,
395
+ cone: i,
395
396
  spec: { type: "free" }
396
397
  };
397
- let s = m([
398
+ let c = h([
398
399
  1,
399
400
  0,
400
401
  0
@@ -402,80 +403,80 @@ var b = (e, t, n) => {
402
403
  0,
403
404
  1,
404
405
  0
405
- ], /* @__PURE__ */ new Float64Array(4)), c = p(i, s, /* @__PURE__ */ new Float64Array(4));
406
+ ], /* @__PURE__ */ new Float64Array(4)), l = m(a, c, /* @__PURE__ */ new Float64Array(4));
406
407
  return {
407
- joint: a.addJoint(e, t, {
408
+ joint: o.addJoint(t, n, {
408
409
  type: "cone",
409
- anchorA: o,
410
+ anchorA: s,
410
411
  anchorB: [
411
412
  0,
412
413
  0,
413
414
  0
414
415
  ],
415
416
  frameA: [
417
+ l[0],
418
+ l[1],
419
+ l[2],
420
+ l[3]
421
+ ],
422
+ frameB: [
416
423
  c[0],
417
424
  c[1],
418
425
  c[2],
419
426
  c[3]
420
427
  ],
421
- frameB: [
422
- s[0],
423
- s[1],
424
- s[2],
425
- s[3]
426
- ],
427
- swing: r.swing,
428
- twist: [r.min, r.max]
428
+ swing: i.swing,
429
+ twist: [i.min, i.max]
429
430
  }),
430
431
  cone: null,
431
432
  spec: {
432
433
  type: "cone",
433
- swing: r.swing,
434
- twist: [r.min, r.max]
434
+ swing: i.swing,
435
+ twist: [i.min, i.max]
435
436
  }
436
437
  };
437
438
  }
438
- if (n?.type !== "hinge") throw TypeError("Unknown ragdoll joint type; use 'cone', 'hinge' or 'free'.");
439
- let s = n.axis, c = Array.isArray(s) && s.length === 3 ? Math.hypot(s[0], s[1], s[2]) : NaN;
440
- if (!(c > 0) || c === Infinity) throw RangeError("Ragdoll hinge axis must be a nonzero finite [x, y, z] in the bone frame; e.g. [1, 0, 0].");
441
- let [l, u] = n.limits ?? [-Math.PI * .75, 0];
442
- if (!(l <= u && l >= -Math.PI && u <= Math.PI)) throw RangeError("Ragdoll hinge limits need [min, max] within -π to π; pass radians.");
443
- let d = [
444
- s[0] / c,
445
- s[1] / c,
446
- s[2] / c
447
- ], f = m([
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([
448
449
  1,
449
450
  0,
450
451
  0
451
- ], d, /* @__PURE__ */ new Float64Array(4)), h = p(i, f, /* @__PURE__ */ new Float64Array(4)), g = (e) => [
452
+ ], f, /* @__PURE__ */ new Float64Array(4)), g = m(a, p, /* @__PURE__ */ new Float64Array(4)), _ = (e) => [
452
453
  e[0],
453
454
  e[1],
454
455
  e[2],
455
456
  e[3]
456
457
  ];
457
458
  return {
458
- joint: a.addJoint(e, t, {
459
+ joint: o.addJoint(t, n, {
459
460
  type: "revolute",
460
- frameA: g(h),
461
- frameB: g(f),
462
- anchorA: o,
461
+ frameA: _(g),
462
+ frameB: _(p),
463
+ anchorA: s,
463
464
  anchorB: [
464
465
  0,
465
466
  0,
466
467
  0
467
468
  ],
468
- limits: [l, u]
469
+ limits: [u, d]
469
470
  }),
470
471
  cone: null,
471
472
  spec: {
472
473
  type: "hinge",
473
474
  axis: [
474
- d[0],
475
- d[1],
476
- d[2]
475
+ f[0],
476
+ f[1],
477
+ f[2]
477
478
  ],
478
- limits: [l, u]
479
+ limits: [u, d]
479
480
  }
480
481
  };
481
482
  }
@@ -487,9 +488,9 @@ var b = (e, t, n) => {
487
488
  for (let t of this.parts) e = Math.max(e, t.weight);
488
489
  return e;
489
490
  }
490
- set weight(e) {
491
- if (!(e >= 0 && e <= 1)) throw RangeError("Ragdoll weight must be from 0 to 1.");
492
- for (let t of this.parts) (t.limp || !this.loose) && (t.weight = e, t.fade = 0);
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);
493
494
  }
494
495
  get rootPosition() {
495
496
  let e = this.live().parts[0].cur;
@@ -499,20 +500,20 @@ var b = (e, t, n) => {
499
500
  e[2]
500
501
  ];
501
502
  }
502
- limbBones(e) {
503
- if (e === void 0) return this.parts;
504
- let t = /* @__PURE__ */ new Set();
505
- for (let n of Array.isArray(e) ? e : [e]) {
506
- let e = n && "body" in n ? n.node : n, r = this.parts.find((t) => t.node === e);
507
- if (!r) throw RangeError("That node is not a bone of this ragdoll; pass nodes or parts of ragdoll.bones.");
508
- t.add(r);
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);
509
510
  }
510
- for (let e of this.parts) e.parent !== -1 && t.has(this.parts[e.parent]) && t.add(e);
511
- return [...t];
511
+ for (let e of this.parts) e.parent !== -1 && n.has(this.parts[e.parent]) && n.add(e);
512
+ return [...n];
512
513
  }
513
514
  activate(e) {
514
515
  this.live();
515
- let n = this.physics.fixedTimestep, r = this.stepped >= 2;
516
+ let t = this.physics.fixedTimestep, r = this.stepped >= 2;
516
517
  this.engine.store.tick(!1);
517
518
  for (let i of this.limbBones(e)) {
518
519
  if (i.limp) continue;
@@ -520,11 +521,11 @@ var b = (e, t, n) => {
520
521
  let { prev: e, cur: a } = i;
521
522
  if (!r) continue;
522
523
  if (i.body.setLinearVelocity([
523
- (a[0] - e[0]) / n,
524
- (a[1] - e[1]) / n,
525
- (a[2] - e[2]) / n
526
- ]), p(a.subarray(3, 7), t(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];
527
- 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 / n : 0;
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;
528
529
  i.body.setAngularVelocity([
529
530
  this.r[0] * c,
530
531
  this.r[1] * c,
@@ -533,48 +534,304 @@ var b = (e, t, n) => {
533
534
  }
534
535
  return this;
535
536
  }
536
- deactivate(e = .3, t) {
537
- if (this.live(), !(e >= 0) || e === Infinity) throw RangeError("Ragdoll blend duration must be a finite number of seconds, 0 or more.");
538
- let n = this.limbBones(t);
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);
539
540
  if (!this.loose) return this;
540
541
  this.physicsLocals();
541
- for (let t of n) t.limp && (t.body.setType("kinematicPosition"), t.limp = !1, t.jump = !0, t.fade = e > 0 ? 1 / e : 0, t.fadeFrom = t.weight, t.fadeAt = 0, e || (t.weight = 0), this.loose--);
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--);
542
543
  return this;
543
544
  }
544
545
  part(e) {
545
546
  return this.parts.find((t) => t.node === e);
546
547
  }
547
- setup(t) {
548
- if (this.disposed) throw new e("disposed", "Ragdoll is disposed; create a new Ragdoll.");
549
- if (t !== this.engine) throw new e("attached", "Ragdoll belongs to its bones' engine; attach it to that engine.");
550
- this.attached = t;
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;
551
808
  }
552
- onPreUpdate(e) {
809
+ onPreUpdate(t) {
553
810
  if (this.disposed) return;
554
811
  if (this.physics.isDisposed || !this.parts[0].body.valid) {
555
812
  this.dispose();
556
813
  return;
557
814
  }
558
- if (!Number.isFinite(e) || e < 0) throw RangeError("Invalid ragdoll delta; pass a finite delta of 0 or more seconds.");
559
- let t = this.engine.store;
560
- t.tick(!1);
561
- for (let t of this.parts) if (this.track(t), !t.limp && t.fade > 0) {
562
- t.fadeAt = Math.min(1, t.fadeAt + t.fade * e);
563
- let n = t.fadeAt;
564
- t.weight = t.fadeFrom * (1 - n * n * (3 - 2 * n)), n >= 1 && (t.weight = 0, t.fade = 0, t.landed = !0);
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);
565
822
  }
566
- this.loose && this.physicsLocals(), this.write(), t.tick(!1);
823
+ this.loose && this.physicsLocals(), this.write(), n.tick(!1);
567
824
  for (let e of this.parts) {
568
825
  if (e.limp) continue;
569
826
  let t = e.node.matrix, n = e.target;
570
- f(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];
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];
571
828
  }
572
829
  }
573
- fit(e, n) {
574
- let r = this.engine.store.parents[e.node.id], i = e.parent === -1 ? r === -1 ? null : this.engine.meshes.get(r) : n[e.parent].node, a = f(e.node.matrix, /* @__PURE__ */ new Float64Array(4));
575
- i && p(t(f(i.matrix, this.q), this.q), a, a);
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);
576
833
  let o = this.readLocal(e.node, this.pose);
577
- p(t(a, a), o.subarray(3, 7), e.bind), Math.hypot(e.bind[0], e.bind[1], e.bind[2]) < c && e.bind.set(l);
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);
578
835
  }
579
836
  readLocal(e, t) {
580
837
  let n = e.engine.store.transforms, r = e.id;
@@ -589,31 +846,31 @@ var b = (e, t, n) => {
589
846
  }
590
847
  physicsLocals() {
591
848
  let e = this.physics.alpha;
592
- for (let n of this.parts) {
593
- let { prev: r, cur: i, world: a, local: o } = n;
849
+ for (let t of this.parts) {
850
+ let { prev: r, cur: i, world: a, local: o } = t;
594
851
  for (let t = 0; t < 3; t++) a[t] = r[t] + (i[t] - r[t]) * e;
595
- if (h(r, 3, i, 3, e, a, 3), !n.limp) continue;
596
- if (o[0] = n.animated[0], o[1] = n.animated[1], o[2] = n.animated[2], n.parent !== -1) {
597
- let e = this.parts[n.parent].world;
598
- t(e.subarray(3, 7), this.q), p(this.q, a.subarray(3, 7), this.r), p(this.r, n.bind, o, 3);
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);
599
856
  continue;
600
857
  }
601
- let s = this.engine.store.parents[n.node.id];
858
+ let s = this.engine.store.parents[t.node.id];
602
859
  if (s === -1) {
603
- o.set(a), p(a.subarray(3, 7), n.bind, o, 3);
860
+ o.set(a), m(a.subarray(3, 7), t.bind, o, 3);
604
861
  continue;
605
862
  }
606
863
  let c = this.engine.meshes.get(s).matrix;
607
- t(f(c, this.q), this.q), p(this.q, a.subarray(3, 7), this.r), p(this.r, n.bind, o, 3);
608
- let l = a[0] - c[12], u = a[1] - c[13], m = a[2] - c[14], g = d(c);
609
- o[0] = (l * (c[5] * c[10] - c[6] * c[9]) + u * (c[8] * c[6] - c[4] * c[10]) + m * (c[4] * c[9] - c[8] * c[5])) / g, o[1] = (l * (c[9] * c[2] - c[1] * c[10]) + u * (c[0] * c[10] - c[8] * c[2]) + m * (c[8] * c[1] - c[0] * c[9])) / g, o[2] = (l * (c[1] * c[6] - c[5] * c[2]) + u * (c[4] * c[2] - c[0] * c[6]) + m * (c[0] * c[5] - c[4] * c[1])) / g;
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;
610
867
  }
611
868
  }
612
869
  write() {
613
870
  for (let e of this.parts) {
614
871
  let { animated: t, local: n, written: r, node: i, weight: a } = e;
615
872
  if (!a && !e.landed) continue;
616
- if (e.landed = !1, h(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;
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;
617
874
  else for (let e = 0; e < 3; e++) r[e] = t[e];
618
875
  i.setPosition([
619
876
  r[0],
@@ -649,16 +906,16 @@ var b = (e, t, n) => {
649
906
  }
650
907
  }
651
908
  limits() {
652
- let e = this.parts, n = this.omega, i = this.stiffness, { q: a, r: o, s, v: c, w: l } = this, u = !1;
653
- for (let t = 0; t < e.length; t++) {
654
- let r = e[t].body.angularVelocity;
655
- n[t * 3] = r[0], n[t * 3 + 1] = r[1], n[t * 3 + 2] = r[2];
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];
656
913
  }
657
- for (let n = 1; n < e.length; n++) {
658
- let d = e[n], f = d.cone;
914
+ for (let t = 1; t < e.length; t++) {
915
+ let d = e[t], f = d.cone;
659
916
  if (!f || !d.limp) continue;
660
- let m = e[d.parent], h = m.body.rotation, g = d.body.rotation;
661
- if (p(h, d.rest, s), t(s, a), p(a, g, o), o[3] < 0) for (let e = 0; e < 4; e++) o[e] = -o[e];
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];
662
919
  let _ = d.axis, v = o[0] * _[0] + o[1] * _[1] + o[2] * _[2], y = 2 * Math.atan2(v, o[3]);
663
920
  y > Math.PI && (y -= 2 * Math.PI);
664
921
  let b = Math.hypot(v, o[3]);
@@ -667,28 +924,28 @@ var b = (e, t, n) => {
667
924
  0,
668
925
  0,
669
926
  1
670
- ]), p(o, a, o);
671
- let x = Math.hypot(o[0], o[1], o[2]), S = 2 * Math.atan2(x, Math.abs(o[3])), C = m.limp;
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;
672
929
  if (S > f.swing && x > 1e-9) {
673
930
  let e = o[3] < 0 ? -1 : 1;
674
- c[0] = o[0] / x * e, c[1] = o[1] / x * e, c[2] = o[2] / x * e, r(c, s, c), u = this.push(n, d.parent, c, C) || u, this.turn(d, c, -(S - f.swing) * i);
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);
675
932
  }
676
933
  if (y > f.max || y < f.min) {
677
- r(_, g, l);
934
+ i(_, g, l);
678
935
  let e = y - f.max;
679
- y < f.min && (e = f.min - y, l[0] = -l[0], l[1] = -l[1], l[2] = -l[2]), u = this.push(n, d.parent, l, C) || u, this.turn(d, l, -e * i);
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);
680
937
  }
681
938
  }
682
- if (u) for (let t = 0; t < e.length; t++) e[t].limp && e[t].body.setAngularVelocity([
683
- n[t * 3],
684
- n[t * 3 + 1],
685
- n[t * 3 + 2]
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]
686
943
  ]);
687
944
  }
688
945
  turn(e, t, n) {
689
946
  let r = n / 2, i = Math.sin(r), a = this.turnBy;
690
947
  a[0] = t[0] * i, a[1] = t[1] * i, a[2] = t[2] * i, a[3] = Math.cos(r);
691
- let o = p(a, e.body.rotation, this.turned);
948
+ let o = m(a, e.body.rotation, this.turned);
692
949
  e.body.setRotation([
693
950
  o[0],
694
951
  o[1],
@@ -706,8 +963,8 @@ var b = (e, t, n) => {
706
963
  return !0;
707
964
  }
708
965
  inverseInertia(e, t) {
709
- let n = this.c, i = e.cur.subarray(3, 7);
710
- r(e.axis, i, n);
966
+ let n = this.c, r = e.cur.subarray(3, 7);
967
+ i(e.axis, r, n);
711
968
  let a = n[0] * t[0] + n[1] * t[1] + n[2] * t[2], o = a * a;
712
969
  return 1 / (o / e.inertia[0] + (1 - o) / e.inertia[1]);
713
970
  }
@@ -722,7 +979,7 @@ var b = (e, t, n) => {
722
979
  }
723
980
  }
724
981
  live() {
725
- if (this.disposed) throw new e("disposed", "Ragdoll is disposed; create a new Ragdoll.");
982
+ if (this.disposed) throw new t("disposed", "Ragdoll is disposed; create a new Ragdoll.");
726
983
  return this;
727
984
  }
728
985
  dispose() {
@@ -738,4 +995,4 @@ var b = (e, t, n) => {
738
995
  }
739
996
  };
740
997
  //#endregion
741
- export { x as Ragdoll };
998
+ export { S as Ragdoll };