@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/powered.js CHANGED
@@ -1,30 +1,31 @@
1
- import { n as e, r as t, t as n } from "./limits-CCe6Pdej.js";
2
- import { MargoError as r } from "@margoengine/math/errors";
3
- import { conjugateQuaternion as i, multiplyQuaternions as a, normalizeQuaternion as o, rotateByQuaternion as s, rotationToQuaternion as c } from "@margoengine/math/quat";
1
+ import { t as e } from "./errors-CDwcEs-j.js";
2
+ import { n as t, r as n, t as r } from "./limits-CCe6Pdej.js";
3
+ import { MargoError as i } from "@margoengine/math/errors";
4
+ import { conjugateQuaternion as a, multiplyQuaternions as o, normalizeQuaternion as s, rotateByQuaternion as c, rotationToQuaternion as l } from "@margoengine/math/quat";
4
5
  //#region src/powered.ts
5
- var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
6
- if (!(e >= 0 && e <= 1)) throw RangeError(`${t} must be from 0 to 1; pass a share.`);
7
- return e;
8
- }, p = (e, t) => {
9
- if (!(e > 0) || e === Infinity) throw RangeError(`${t} must be a finite number above 0.`);
10
- return e;
11
- }, m = (e) => a([
6
+ var u = Math.PI * 2, d = 8, f = 10, p = (t, n) => {
7
+ if (!(t >= 0 && t <= 1)) throw e(`${n} must be from 0 to 1; pass a share.`);
8
+ return t;
9
+ }, m = (t, n) => {
10
+ if (!(t > 0) || t === Infinity) throw e(`${n} must be a finite number above 0.`);
11
+ return t;
12
+ }, h = (e) => o([
12
13
  e.animated[3],
13
14
  e.animated[4],
14
15
  e.animated[5],
15
16
  e.animated[6]
16
- ], i(e.bind)), h = class {
17
+ ], a(e.bind)), g = class {
17
18
  constructor(e, t) {
18
- this.ragdoll = e, this.error = 0, this.rate = 0, this.targets = /* @__PURE__ */ new Map(), this.disposed = !1, this.omega = l * p(t.frequency ?? 6, "Ragdoll motor frequency"), this.zeta = p(t.damping ?? 1, "Ragdoll motor damping"), this.global = this.goal = f(t.strength ?? 1, "Ragdoll motor strength"), this.rootShare = f(t.root ?? 0, "Ragdoll motor root"), this.acceleration = p(t.maxAcceleration ?? 400, "Ragdoll motor maxAcceleration");
19
+ this.ragdoll = e, this.error = 0, this.rate = 0, this.targets = /* @__PURE__ */ new Map(), this.disposed = !1, this.omega = u * m(t.frequency ?? 6, "Ragdoll motor frequency"), this.zeta = m(t.damping ?? 1, "Ragdoll motor damping"), this.global = this.goal = p(t.strength ?? 1, "Ragdoll motor strength"), this.rootShare = p(t.root ?? 0, "Ragdoll motor root"), this.acceleration = m(t.maxAcceleration ?? 400, "Ragdoll motor maxAcceleration");
19
20
  let n = this.parts = e.bones, r = n.length, i = n.map((e) => {
20
- let t = s(e.axis, e.body.rotation), n = e.body.position;
21
+ let t = c(e.axis, e.body.rotation), n = e.body.position;
21
22
  return [
22
23
  0,
23
24
  1,
24
25
  2
25
26
  ].map((r) => n[r] + t[r] * e.length / 2);
26
27
  }), a = n.map((e) => {
27
- let t = s(e.body.centerOfMass, e.body.rotation), n = e.body.position;
28
+ let t = c(e.body.centerOfMass, e.body.rotation), n = e.body.position;
28
29
  return [
29
30
  0,
30
31
  1,
@@ -40,11 +41,11 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
40
41
  o += r.body.mass * c * c + i(r);
41
42
  }
42
43
  return o;
43
- }, c = (e) => {
44
+ }, s = (e) => {
44
45
  let [t, n, r] = e.body.principalInertia;
45
46
  return (t + n + r) / 3;
46
- }, u = (e) => e.body.mass * e.length * e.length / 12;
47
- for (let e = 1; e < r; e++) this.limbs[e] = o(e, a, c), this.rods[e] = o(e, i, u);
47
+ }, l = (e) => e.body.mass * e.length * e.length / 12;
48
+ for (let e = 1; e < r; e++) this.limbs[e] = o(e, a, s), this.rods[e] = o(e, i, l);
48
49
  this.gains = this.limbs.slice();
49
50
  for (let e of n) this.addSteadying(e);
50
51
  this.off = e.physics.on("beforestep", (e) => this.step(e));
@@ -52,7 +53,7 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
52
53
  addSteadying(e, t = 0) {
53
54
  let n = this.parts, r = n.indexOf(e), i = e.parent === -1 ? 0 : n[e.parent].mass, a = 0;
54
55
  for (let e = r + 1; e < n.length; e++) n[e].parent === r && (i = Math.max(i, n[e].mass), a = Math.max(a, this.rods[e]));
55
- let o = Math.max(0, i / u - e.mass), s = t + a / d;
56
+ let o = Math.max(0, i / d - e.mass), s = t + a / f;
56
57
  e.body.setAdditionalMass(o, [
57
58
  s,
58
59
  s,
@@ -64,13 +65,13 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
64
65
  return e === t;
65
66
  }
66
67
  live() {
67
- if (this.disposed) throw new r("disposed", "Ragdoll motors are disposed; call addRagdollMotors again.");
68
+ if (this.disposed) throw new i("disposed", "Ragdoll motors are disposed; call addRagdollMotors again.");
68
69
  return this;
69
70
  }
70
- index(e) {
71
- let t = "body" in e ? e.node : e, n = this.parts.findIndex((e) => e.node === t);
72
- if (n < 0) throw RangeError("That node is not a bone of this ragdoll; pass one of ragdoll.bones.");
73
- return n;
71
+ index(t) {
72
+ let n = "body" in t ? t.node : t, r = this.parts.findIndex((e) => e.node === n);
73
+ if (r < 0) throw e("That node is not a bone of this ragdoll; pass one of ragdoll.bones.");
74
+ return r;
74
75
  }
75
76
  limbInertia(e) {
76
77
  return this.limbs[this.index(e)];
@@ -79,24 +80,24 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
79
80
  return this.global;
80
81
  }
81
82
  set strength(e) {
82
- this.global = this.goal = f(e, "Ragdoll motor strength"), this.rate = 0;
83
+ this.global = this.goal = p(e, "Ragdoll motor strength"), this.rate = 0;
83
84
  }
84
85
  get root() {
85
86
  return this.rootShare;
86
87
  }
87
88
  set root(e) {
88
- this.rootShare = f(e, "Ragdoll motor root");
89
+ this.rootShare = p(e, "Ragdoll motor root");
89
90
  }
90
- fade(e, t) {
91
- if (this.live(), f(e, "Ragdoll motor strength"), !(t >= 0) || t === Infinity) throw RangeError("Ragdoll motor fade must be a finite number of seconds, 0 or more.");
92
- return this.goal = e, t === 0 ? (this.global = e, this.rate = 0) : this.rate = Math.abs(e - this.global) / t, this;
91
+ fade(t, n) {
92
+ if (this.live(), p(t, "Ragdoll motor strength"), !(n >= 0) || n === Infinity) throw e("Ragdoll motor fade must be a finite number of seconds, 0 or more.");
93
+ return this.goal = t, n === 0 ? (this.global = t, this.rate = 0) : this.rate = Math.abs(t - this.global) / n, this;
93
94
  }
94
95
  setTarget(e, t) {
95
96
  let n = this.index(e);
96
97
  return t ? this.targets.set(n, t) : this.targets.delete(n), this;
97
98
  }
98
99
  setStrength(e, t) {
99
- this.live(), f(t, "Ragdoll motor strength");
100
+ this.live(), p(t, "Ragdoll motor strength");
100
101
  let n = this.index(e);
101
102
  for (let e = n; e < this.parts.length; e++) this.within(e, n) && (this.limb[e] = t);
102
103
  return this;
@@ -105,29 +106,29 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
105
106
  let n = this.index(e);
106
107
  this.gains[n] = (t ? this.rods : this.limbs)[n];
107
108
  }
108
- hit(e, t, n = {}) {
109
+ hit(t, n, r = {}) {
109
110
  this.live();
110
- let r = this.index(e), i = f(n.weaken ?? .8, "Ragdoll hit weaken"), a = n.recover ?? .6;
111
- if (!(a >= 0) || a === Infinity) throw RangeError("Ragdoll hit recover must be a finite number of seconds, 0 or more.");
112
- let o = (e) => {
113
- this.lost[e] = Math.max(this.lost[e], i), this.heal[e] = a > 0 ? this.lost[e] / a : Infinity;
111
+ let i = this.index(t), a = p(r.weaken ?? .8, "Ragdoll hit weaken"), o = r.recover ?? .6;
112
+ if (!(o >= 0) || o === Infinity) throw e("Ragdoll hit recover must be a finite number of seconds, 0 or more.");
113
+ let s = (e) => {
114
+ this.lost[e] = Math.max(this.lost[e], a), this.heal[e] = o > 0 ? this.lost[e] / o : Infinity;
114
115
  };
115
- for (let e = r; e < this.parts.length; e++) this.within(e, r) && o(e);
116
- for (let e = this.parts[r].parent; e !== -1; e = this.parts[e].parent) o(e);
117
- return this.parts[r].body.applyImpulse(t, n.point), this;
116
+ for (let e = i; e < this.parts.length; e++) this.within(e, i) && s(e);
117
+ for (let e = this.parts[i].parent; e !== -1; e = this.parts[e].parent) s(e);
118
+ return this.parts[i].body.applyImpulse(n, r.point), this;
118
119
  }
119
- step(r) {
120
+ step(e) {
120
121
  if (this.disposed || !this.parts[0].body.valid) return;
121
122
  if (this.rate > 0) {
122
- let e = this.goal - this.global, t = this.rate * r;
123
- this.global = Math.abs(e) <= t ? this.goal : this.global + Math.sign(e) * t, this.global === this.goal && (this.rate = 0);
123
+ let t = this.goal - this.global, n = this.rate * e;
124
+ this.global = Math.abs(t) <= n ? this.goal : this.global + Math.sign(t) * n, this.global === this.goal && (this.rate = 0);
124
125
  }
125
- let { parts: o, omega: s, zeta: c, lost: l, heal: u, running: d } = this, f = 0;
126
- for (let p = 1; p < o.length; p++) {
127
- let h = o[p], g = h.joint;
128
- l[p] > 0 && (l[p] = Math.max(0, l[p] - u[p] * r));
126
+ let { parts: i, omega: s, zeta: c, lost: l, heal: u, running: d } = this, f = 0;
127
+ for (let p = 1; p < i.length; p++) {
128
+ let m = i[p], g = m.joint;
129
+ l[p] > 0 && (l[p] = Math.max(0, l[p] - u[p] * e));
129
130
  let _ = this.shares[p] = this.global * this.limb[p] * (1 - l[p]);
130
- if (h.body.type !== "dynamic" || _ <= 0) {
131
+ if (m.body.type !== "dynamic" || _ <= 0) {
131
132
  d[p] && g.clearMotor(), d[p] = 0;
132
133
  continue;
133
134
  }
@@ -137,43 +138,43 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
137
138
  y[1],
138
139
  y[2],
139
140
  y[3]
140
- ] : m(h), x = {
141
+ ] : h(m), x = {
141
142
  model: "force",
142
143
  stiffness: v * s * s * _,
143
144
  damping: 2 * c * v * s * Math.sqrt(_),
144
145
  maxForce: v * this.acceleration * _
145
- }, S = h.spec;
146
+ }, S = m.spec;
146
147
  if (S.type === "hinge") {
147
- let [e, o] = S.limits, s = a(i(h.rest), b), { twist: c } = t(s, S.axis), l = Math.min(o, Math.max(e, c));
148
+ let [t, i] = S.limits, s = o(a(m.rest), b), { twist: c } = n(s, S.axis), l = Math.min(i, Math.max(t, c));
148
149
  g.setMotor({
149
150
  targetPosition: l,
150
- targetVelocity: this.hingeSpeed(p, l, r),
151
+ targetVelocity: this.hingeSpeed(p, l, e),
151
152
  ...x
152
- }), f = Math.max(f, this.apart(p, a(h.rest, n(S.axis, l))));
153
+ }), f = Math.max(f, this.apart(p, o(m.rest, r(S.axis, l))));
153
154
  } else {
154
- let t = e(h, b);
155
- g.setRotationMotor(t, {
155
+ let n = t(m, b);
156
+ g.setRotationMotor(n, {
156
157
  ...x,
157
- targetVelocity: this.ballSpeed(p, t, r)
158
- }), f = Math.max(f, this.apart(p, t));
158
+ targetVelocity: this.ballSpeed(p, n, e)
159
+ }), f = Math.max(f, this.apart(p, n));
159
160
  }
160
161
  }
161
- this.error = f, this.rootShare > 0 && o[0].body.type === "dynamic" && this.pullRoot(r);
162
+ this.error = f, this.rootShare > 0 && i[0].body.type === "dynamic" && this.pullRoot(e);
162
163
  }
163
164
  ballSpeed(e, t, n) {
164
- let r = this.last.subarray(e * 4, e * 4 + 4), o = this.spin.subarray(e * 3, e * 3 + 3);
165
+ let r = this.last.subarray(e * 4, e * 4 + 4), i = this.spin.subarray(e * 3, e * 3 + 3);
165
166
  if (this.waited[e] += n, r[0] === t[0] && r[1] === t[1] && r[2] === t[2] && r[3] === t[3]) this.settle(e, n);
166
167
  else {
167
168
  if (this.seen[e]) {
168
- let n = a(i(r), t), s = n[3] < 0 ? -1 : 1, c = Math.hypot(n[0], n[1], n[2]), l = c > 1e-9 ? 2 * Math.atan2(c, n[3] * s) / c * s : 2 * s;
169
- for (let t = 0; t < 3; t++) o[t] = n[t] * l / this.waited[e];
169
+ let n = o(a(r), t), s = n[3] < 0 ? -1 : 1, c = Math.hypot(n[0], n[1], n[2]), l = c > 1e-9 ? 2 * Math.atan2(c, n[3] * s) / c * s : 2 * s;
170
+ for (let t = 0; t < 3; t++) i[t] = n[t] * l / this.waited[e];
170
171
  }
171
172
  r.set(t), this.moved(e);
172
173
  }
173
174
  return [
174
- o[0],
175
- o[1],
176
- o[2]
175
+ i[0],
176
+ i[1],
177
+ i[2]
177
178
  ];
178
179
  }
179
180
  moved(e) {
@@ -186,28 +187,28 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
186
187
  return this.waited[e] += n, this.last[e * 4] === t ? this.settle(e, n) : (this.seen[e] && (this.spin[e * 3] = (t - this.last[e * 4]) / this.waited[e]), this.last[e * 4] = t, this.moved(e)), this.spin[e * 3];
187
188
  }
188
189
  apart(e, t) {
189
- let n = this.parts[e], r = a(i(this.parts[n.parent].body.rotation), n.body.rotation), o = Math.abs(r[0] * t[0] + r[1] * t[1] + r[2] * t[2] + r[3] * t[3]);
190
- return 2 * Math.acos(Math.min(1, o));
190
+ let n = this.parts[e], r = o(a(this.parts[n.parent].body.rotation), n.body.rotation), i = Math.abs(r[0] * t[0] + r[1] * t[1] + r[2] * t[2] + r[3] * t[3]);
191
+ return 2 * Math.acos(Math.min(1, i));
191
192
  }
192
193
  pullRoot(e) {
193
- let t = this.parts[0], n = t.node, r = n.engine.store.parents[n.id], s = t.animated, l = [
194
- s[0],
195
- s[1],
196
- s[2]
197
- ], u = m(t);
194
+ let t = this.parts[0], n = t.node, r = n.engine.store.parents[n.id], i = t.animated, c = [
195
+ i[0],
196
+ i[1],
197
+ i[2]
198
+ ], u = h(t);
198
199
  if (r !== -1) {
199
- let e = n.engine.meshes.get(r).matrix, t = Math.hypot(e[0], e[1], e[2]), i = Math.hypot(e[4], e[5], e[6]), d = Math.hypot(e[8], e[9], e[10]), f = o(c(e, /* @__PURE__ */ new Float64Array(4), t, i, d));
200
- u = a(f, u), l = [
200
+ let e = n.engine.meshes.get(r).matrix, t = Math.hypot(e[0], e[1], e[2]), a = Math.hypot(e[4], e[5], e[6]), d = Math.hypot(e[8], e[9], e[10]), f = s(l(e, /* @__PURE__ */ new Float64Array(4), t, a, d));
201
+ u = o(f, u), c = [
201
202
  0,
202
203
  1,
203
204
  2
204
- ].map((t) => e[t] * s[0] + e[t + 4] * s[1] + e[t + 8] * s[2] + e[t + 12]);
205
+ ].map((t) => e[t] * i[0] + e[t + 4] * i[1] + e[t + 8] * i[2] + e[t + 12]);
205
206
  }
206
- let d = this.omega * Math.sqrt(this.rootShare), f = 2 * this.zeta * d, p = e / (1 + f * e + d * d * e * e), h = t.body.position, g = t.body.linearVelocity, _ = t.body.angularVelocity, v = a(u, i(t.body.rotation)), y = v[3] < 0 ? -1 : 1, b = Math.hypot(v[0], v[1], v[2]), x = b > 1e-9 ? 2 * Math.atan2(b, v[3] * y) / b * y : 2 * y, S = [
207
+ let d = this.omega * Math.sqrt(this.rootShare), f = 2 * this.zeta * d, p = e / (1 + f * e + d * d * e * e), m = t.body.position, g = t.body.linearVelocity, _ = t.body.angularVelocity, v = o(u, a(t.body.rotation)), y = v[3] < 0 ? -1 : 1, b = Math.hypot(v[0], v[1], v[2]), x = b > 1e-9 ? 2 * Math.atan2(b, v[3] * y) / b * y : 2 * y, S = [
207
208
  0,
208
209
  1,
209
210
  2
210
- ].map((e) => p * (d * d * (l[e] - h[e]) - f * g[e])), C = [
211
+ ].map((e) => p * (d * d * (c[e] - m[e]) - f * g[e])), C = [
211
212
  0,
212
213
  1,
213
214
  2
@@ -215,7 +216,7 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
215
216
  for (let e = 0; e < this.parts.length; e++) {
216
217
  let t = this.parts[e], n = e ? this.shares[e] : 1;
217
218
  if (n <= 0 || t.body.type !== "dynamic") continue;
218
- let r = t.body.position, i = r[0] - h[0], a = r[1] - h[1], o = r[2] - h[2], s = t.body.linearVelocity, c = t.body.angularVelocity;
219
+ let r = t.body.position, i = r[0] - m[0], a = r[1] - m[1], o = r[2] - m[2], s = t.body.linearVelocity, c = t.body.angularVelocity;
219
220
  t.body.setLinearVelocity([
220
221
  s[0] + n * (S[0] + C[1] * o - C[2] * a),
221
222
  s[1] + n * (S[1] + C[2] * i - C[0] * o),
@@ -237,9 +238,9 @@ var l = Math.PI * 2, u = 8, d = 10, f = (e, t) => {
237
238
  this.dispose();
238
239
  }
239
240
  };
240
- function g(e, t = {}) {
241
- if (!e?.bones || typeof e.physics?.on != "function") throw TypeError("addRagdollMotors needs a Ragdoll: addRagdollMotors(ragdoll, {root: 1}).");
242
- return new h(e, t ?? {});
241
+ function _(t, n = {}) {
242
+ if (!t?.bones || typeof t.physics?.on != "function") throw e("addRagdollMotors needs a Ragdoll: addRagdollMotors(ragdoll, {root: 1}).");
243
+ return new g(t, n ?? {});
243
244
  }
244
245
  //#endregion
245
- export { h as RagdollMotors, g as addRagdollMotors };
246
+ export { g as RagdollMotors, _ as addRagdollMotors };
package/dist/profile.js CHANGED
@@ -1,23 +1,24 @@
1
- import { conjugateQuaternion as e, normalizeQuaternion as t, rotateByQuaternion as n, rotationToQuaternion as r } from "@margoengine/math/quat";
2
- import { detectSkeleton as i, humanoidBones as a } from "@margoengine/rigging/retarget";
1
+ import { t as e } from "./errors-CDwcEs-j.js";
2
+ import { conjugateQuaternion as t, normalizeQuaternion as n, rotateByQuaternion as r, rotationToQuaternion as i } from "@margoengine/math/quat";
3
+ import { detectSkeleton as a, humanoidBones as o } from "@margoengine/rigging/retarget";
3
4
  //#region src/profile.ts
4
- var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
5
+ var s = "margoengine-ragdoll", c = 1, l = (e, t) => ({
5
6
  type: "cone",
6
7
  swing: e,
7
8
  twist: [-t, t]
8
- }), l = {
9
+ }), u = {
9
10
  trunk: {
10
11
  share: .4346,
11
- joint: c(.35, .35)
12
+ joint: l(.35, .35)
12
13
  },
13
14
  head: {
14
15
  share: .0694,
15
- joint: c(.7, .9),
16
+ joint: l(.7, .9),
16
17
  length: .13
17
18
  },
18
19
  upperArm: {
19
20
  share: .0271,
20
- joint: c(1.75, 1.2)
21
+ joint: l(1.75, 1.2)
21
22
  },
22
23
  lowerArm: {
23
24
  share: .0162,
@@ -25,12 +26,12 @@ var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
25
26
  },
26
27
  hand: {
27
28
  share: .0061,
28
- joint: c(.9, .3),
29
+ joint: l(.9, .3),
29
30
  length: .108
30
31
  },
31
32
  upperLeg: {
32
33
  share: .1416,
33
- joint: c(1.3, .5)
34
+ joint: l(1.3, .5)
34
35
  },
35
36
  lowerLeg: {
36
37
  share: .0433,
@@ -38,10 +39,10 @@ var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
38
39
  },
39
40
  foot: {
40
41
  share: .0137,
41
- joint: c(.6, .2),
42
+ joint: l(.6, .2),
42
43
  length: .152
43
44
  }
44
- }, u = 2.4, d = 2.5, f = .53, p = [
45
+ }, d = 2.4, f = 2.5, p = .53, m = [
45
46
  "hips",
46
47
  "head",
47
48
  "leftUpperArm",
@@ -52,7 +53,7 @@ var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
52
53
  "rightUpperLeg",
53
54
  "leftLowerLeg",
54
55
  "rightLowerLeg"
55
- ], m = [
56
+ ], h = [
56
57
  [
57
58
  "hips",
58
59
  "trunk",
@@ -120,19 +121,19 @@ var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
120
121
  `${e}Foot`
121
122
  ]
122
123
  ])
123
- ], h = (e) => [
124
+ ], g = (e) => [
124
125
  e.matrix[12],
125
126
  e.matrix[13],
126
127
  e.matrix[14]
127
- ], g = (e, t) => [
128
+ ], _ = (e, t) => [
128
129
  e[0] - t[0],
129
130
  e[1] - t[1],
130
131
  e[2] - t[2]
131
- ], _ = (e, t) => [
132
+ ], v = (e, t) => [
132
133
  e[1] * t[2] - e[2] * t[1],
133
134
  e[2] * t[0] - e[0] * t[2],
134
135
  e[0] * t[1] - e[1] * t[0]
135
- ], v = (e) => {
136
+ ], y = (e) => {
136
137
  let t = Math.hypot(e[0], e[1], e[2]);
137
138
  return [
138
139
  e[0] / t,
@@ -140,91 +141,91 @@ var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
140
141
  e[2] / t
141
142
  ];
142
143
  };
143
- function y(e) {
144
- let n = e.matrix;
145
- return t(r(n, [
144
+ function b(e) {
145
+ let t = e.matrix;
146
+ return n(i(t, [
146
147
  0,
147
148
  0,
148
149
  0,
149
150
  1
150
- ], Math.hypot(n[0], n[1], n[2]), Math.hypot(n[4], n[5], n[6]), Math.hypot(n[8], n[9], n[10])));
151
+ ], Math.hypot(t[0], t[1], t[2]), Math.hypot(t[4], t[5], t[6]), Math.hypot(t[8], t[9], t[10])));
151
152
  }
152
- var b = (e) => e.type === "hull" ? {
153
+ var x = (e) => e.type === "hull" ? {
153
154
  type: "hull",
154
155
  points: Array.from(e.points)
155
156
  } : e;
156
- function x(e) {
157
- if (!e?.bones) throw TypeError("saveRagdollProfile needs a Ragdoll: saveRagdollProfile(ragdoll).");
158
- let t = /* @__PURE__ */ new Set();
157
+ function S(t) {
158
+ if (!t?.bones) throw e("saveRagdollProfile needs a Ragdoll: saveRagdollProfile(ragdoll).");
159
+ let n = /* @__PURE__ */ new Set();
159
160
  return {
160
- format: o,
161
- version: s,
162
- settings: { ...e.settings },
163
- bones: e.bones.map((e) => {
164
- let { name: n } = e.node;
165
- if (!n) throw RangeError("A ragdoll profile finds bones by name; name every bone node before saving.");
166
- if (t.has(n)) throw RangeError(`Two ragdoll bones are named "${n}"; rename one, a profile finds bones by name.`);
167
- return t.add(n), {
168
- name: n,
169
- ...e.spec ? { joint: e.spec } : {},
170
- length: e.length,
171
- radius: e.radius,
172
- mass: e.mass,
173
- shape: b(e.shape)
161
+ format: s,
162
+ version: c,
163
+ settings: { ...t.settings },
164
+ bones: t.bones.map((t) => {
165
+ let { name: r } = t.node;
166
+ if (!r) throw e("A ragdoll profile finds bones by name; name every bone node before saving.");
167
+ if (n.has(r)) throw e(`Two ragdoll bones are named "${r}"; rename one, a profile finds bones by name.`);
168
+ return n.add(r), {
169
+ name: r,
170
+ ...t.spec ? { joint: t.spec } : {},
171
+ length: t.length,
172
+ radius: t.radius,
173
+ mass: t.mass,
174
+ shape: x(t.shape)
174
175
  };
175
176
  })
176
177
  };
177
178
  }
178
- function S(e, t) {
179
- if (!e || !Array.isArray(t)) throw TypeError("ragdollOptions needs a profile and the nodes: ragdollOptions(profile, model.nodes).");
180
- if (e.format !== o) throw RangeError(`That is not a ragdoll profile: its format is not "${o}"; pass what saveRagdollProfile made.`);
181
- if (!(e.version <= s)) throw RangeError(`This profile is version ${e.version}; this package reads up to ${s}. Update it.`);
182
- if (!Array.isArray(e.bones) || !e.bones.length) throw RangeError("A ragdoll profile needs a non-empty bones list; pass what saveRagdollProfile made.");
183
- let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Set();
184
- for (let e of t) n.has(e.name) && r.add(e.name), n.set(e.name, e);
185
- let i = e.bones.map(({ name: e, ...t }) => {
186
- let i = n.get(e);
187
- if (!i) throw RangeError(`The profile's bone "${e}" is not in this skeleton; pass its nodes.`);
188
- if (r.has(e)) throw RangeError(`Two nodes are named "${e}", so the profile cannot tell them apart; rename one.`);
179
+ function C(t, n) {
180
+ if (!t || !Array.isArray(n)) throw e("ragdollOptions needs a profile and the nodes: ragdollOptions(profile, model.nodes).");
181
+ if (t.format !== s) throw e(`That is not a ragdoll profile: its format is not "${s}"; pass what saveRagdollProfile made.`);
182
+ if (!(t.version <= c)) throw e(`This profile is version ${t.version}; this package reads up to ${c}. Update it.`);
183
+ if (!Array.isArray(t.bones) || !t.bones.length) throw e("A ragdoll profile needs a non-empty bones list; pass what saveRagdollProfile made.");
184
+ let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set();
185
+ for (let e of n) r.has(e.name) && i.add(e.name), r.set(e.name, e);
186
+ let a = t.bones.map(({ name: t, ...n }) => {
187
+ let a = r.get(t);
188
+ if (!a) throw e(`The profile's bone "${t}" is not in this skeleton; pass its nodes.`);
189
+ if (i.has(t)) throw e(`Two nodes are named "${t}", so the profile cannot tell them apart; rename one.`);
189
190
  return {
190
- node: i,
191
- ...t
191
+ node: a,
192
+ ...n
192
193
  };
193
194
  });
194
195
  return {
195
- ...e.settings,
196
- bones: i
196
+ ...t.settings,
197
+ bones: a
197
198
  };
198
199
  }
199
- function C(t, r = {}) {
200
- if (!Array.isArray(t)) throw TypeError("humanoidProfile needs the skeleton nodes: humanoidProfile(model.nodes).");
201
- let i = r.mass ?? 70;
202
- if (!(i > 0) || i === Infinity) throw RangeError("Humanoid mass must be positive kilograms, e.g. 70.");
203
- let a = r.bones ?? w(t), b = p.filter((e) => !a[e]);
204
- if (b.length) throw RangeError(`This rig has no ${b.join(", ")}; pass them as options.bones: {hips: node, ...}.`);
205
- let x = a.hips, { parents: S } = x.engine.store;
206
- x.engine.store.tick(!1);
207
- let C = new Map(t.map((e) => [e.id, e])), T = /* @__PURE__ */ new Map();
208
- for (let [e, t, n] of m) {
209
- let r = a[e];
210
- r && T.set(r, {
200
+ function w(n, i = {}) {
201
+ if (!Array.isArray(n)) throw e("humanoidProfile needs the skeleton nodes: humanoidProfile(model.nodes).");
202
+ let a = i.mass ?? 70;
203
+ if (!(a > 0) || a === Infinity) throw e("Humanoid mass must be positive kilograms, e.g. 70.");
204
+ let o = i.bones ?? T(n), x = m.filter((e) => !o[e]);
205
+ if (x.length) throw e(`This rig has no ${x.join(", ")}; pass them as options.bones: {hips: node, ...}.`);
206
+ let S = o.hips, { parents: C } = S.engine.store;
207
+ S.engine.store.tick(!1);
208
+ let w = new Map(n.map((e) => [e.id, e])), E = /* @__PURE__ */ new Map();
209
+ for (let [e, t, n] of h) {
210
+ let r = o[e];
211
+ r && E.set(r, {
211
212
  segment: t,
212
213
  group: n
213
214
  });
214
215
  }
215
- for (let [e, t, n] of m) {
216
- let r = a[e];
217
- for (; r && r !== x;) {
218
- let i = C.get(S[r.id]);
219
- if (!i) throw RangeError(`Humanoid bone ${e} is not under the hips; check the bones passed as options.bones.`);
220
- T.has(i) || T.set(i, {
221
- segment: t,
222
- group: n
223
- }), r = i;
216
+ for (let [t, n, r] of h) {
217
+ let i = o[t];
218
+ for (; i && i !== S;) {
219
+ let a = w.get(C[i.id]);
220
+ if (!a) throw e(`Humanoid bone ${t} is not under the hips; check the bones passed as options.bones.`);
221
+ E.has(a) || E.set(a, {
222
+ segment: n,
223
+ group: r
224
+ }), i = a;
224
225
  }
225
226
  }
226
- let E = v(g(h(a.head), h(x))), D = v(_(v(g(h(a.leftUpperLeg), h(a.rightUpperLeg))), E)), O = a.leftFoot ?? a.leftLowerLeg, k = g(h(x), h(O)).reduce((e, t, n) => e + t * E[n], 0) / f, A = (t, r, i, a) => {
227
- let o = v(g(h(r ?? t), h(t))), s = n(v(_(o, i)), e(y(t)));
227
+ let D = y(_(g(o.head), g(S))), O = y(v(y(_(g(o.leftUpperLeg), g(o.rightUpperLeg))), D)), k = o.leftFoot ?? o.leftLowerLeg, A = _(g(S), g(k)).reduce((e, t, n) => e + t * D[n], 0) / p, j = (e, n, i, a) => {
228
+ let o = y(_(g(n ?? e), g(e))), s = r(y(v(o, i)), t(b(e)));
228
229
  return {
229
230
  type: "hinge",
230
231
  axis: [
@@ -234,34 +235,34 @@ function C(t, r = {}) {
234
235
  ],
235
236
  limits: [0, a]
236
237
  };
237
- }, j = /* @__PURE__ */ new Map();
238
- for (let { group: e } of T.values()) j.set(e, (j.get(e) ?? 0) + 1);
239
- let M = /* @__PURE__ */ new Map();
240
- for (let e of ["left", "right"]) M.set(a[`${e}LowerLeg`], a[`${e}Foot`]), M.set(a[`${e}LowerArm`], a[`${e}Hand`]);
241
- let N = [...T].map(([e, { segment: t, group: n }]) => {
242
- let r = l[t], i = m.some(([t]) => a[t] === e), o = i ? r.joint : c(.35, .35);
243
- return o === "knee" ? o = A(e, M.get(e), [
244
- -D[0],
245
- -D[1],
246
- -D[2]
247
- ], u) : o === "elbow" && (o = A(e, M.get(e), D, d)), {
238
+ }, M = /* @__PURE__ */ new Map();
239
+ for (let { group: e } of E.values()) M.set(e, (M.get(e) ?? 0) + 1);
240
+ let N = /* @__PURE__ */ new Map();
241
+ for (let e of ["left", "right"]) N.set(o[`${e}LowerLeg`], o[`${e}Foot`]), N.set(o[`${e}LowerArm`], o[`${e}Hand`]);
242
+ let P = [...E].map(([e, { segment: t, group: n }]) => {
243
+ let r = u[t], i = h.some(([t]) => o[t] === e), a = i ? r.joint : l(.35, .35);
244
+ return a === "knee" ? a = j(e, N.get(e), [
245
+ -O[0],
246
+ -O[1],
247
+ -O[2]
248
+ ], d) : a === "elbow" && (a = j(e, N.get(e), O, f)), {
248
249
  name: e.name,
249
- ...e === x ? {} : { joint: o },
250
- ...i && r.length !== void 0 && e !== a.neck ? { length: r.length * k } : {},
251
- mass: r.share / j.get(n)
250
+ ...e === S ? {} : { joint: a },
251
+ ...i && r.length !== void 0 && e !== o.neck ? { length: r.length * A } : {},
252
+ mass: r.share / M.get(n)
252
253
  };
253
- }), P = N.reduce((e, t) => e + t.mass, 0);
254
- for (let e of N) e.mass = e.mass / P * i;
254
+ }), F = P.reduce((e, t) => e + t.mass, 0);
255
+ for (let e of P) e.mass = e.mass / F * a;
255
256
  return {
256
- format: o,
257
- version: s,
258
- bones: N
257
+ format: s,
258
+ version: c,
259
+ bones: P
259
260
  };
260
261
  }
261
- function w(e) {
262
- let t = e.map((e) => e.name ?? ""), n = i(t), r = {};
263
- if (n) for (let [i, o] of a(t, n.preset)) r[i] = e[o];
262
+ function T(e) {
263
+ let t = e.map((e) => e.name ?? ""), n = a(t), r = {};
264
+ if (n) for (let [i, a] of o(t, n.preset)) r[i] = e[a];
264
265
  return r;
265
266
  }
266
267
  //#endregion
267
- export { C as humanoidProfile, S as ragdollOptions, x as saveRagdollProfile };
268
+ export { w as humanoidProfile, C as ragdollOptions, S as saveRagdollProfile };
package/dist/ragdoll.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference lib="esnext.disposable" preserve="true" />
2
2
  import type { Engine, EnginePlugin, Mesh } from '@margoengine/core/extension';
3
- import type { Physics, PhysicsBody, PhysicsJoint } from '@margoengine/physics';
3
+ import type { DebugLines, Physics, PhysicsBody, PhysicsJoint } from '@margoengine/physics';
4
4
  type Vec3 = readonly [number, number, number];
5
5
  /**
6
6
  * How a bone turns against its parent bone, measured from the rest pose the ragdoll was built in.
@@ -138,6 +138,17 @@ export interface RagdollPart {
138
138
  }
139
139
  /** One bone or several, as nodes or parts: each stands for itself and every bone under it. */
140
140
  export type RagdollLimb = Mesh | RagdollPart | readonly (Mesh | RagdollPart)[];
141
+ /** What {@link Ragdoll.debugRender} draws. */
142
+ export interface RagdollDebugOptions {
143
+ /** Draw each bone's collider shape (capsule, box or hull), tinted by kinematic/limp/blending state. Default true. */
144
+ capsules?: boolean;
145
+ /** Draw joint limits (swing cones for cone joints, limit arcs and axis for hinges). Default true. */
146
+ limits?: boolean;
147
+ /** Draw bone axis lines and parent-child connectors. Default true. */
148
+ bones?: boolean;
149
+ /** Size in metres of swing cones and hinge arcs. Default 0.12. */
150
+ limitScale?: number;
151
+ }
141
152
  /**
142
153
  * A ragdoll on a skeleton: one capsule body per bone, joined at the bone origins with swing and twist limits. Built
143
154
  * kinematic, it follows the animation (hit boxes that push the world); `activate()` makes it limp, so physics drives
@@ -224,6 +235,11 @@ export declare class Ragdoll implements EnginePlugin {
224
235
  deactivate(duration?: number, limb?: RagdollLimb): this;
225
236
  /** The part whose bone is `node`, or undefined. */
226
237
  part(node: Mesh): RagdollPart | undefined;
238
+ /**
239
+ * Debug line segments for this ragdoll: bone colliders tinted by mode (kinematic blue, limp green, partial amber,
240
+ * blending cyan), joint swing cones and hinge limit arcs, and bone axes. Same layout as `physics.debugRender()`.
241
+ */
242
+ debugRender(options?: RagdollDebugOptions): DebugLines;
227
243
  setup(engine: Engine): void;
228
244
  /**
229
245
  * Runs each frame after the animation: reads the animated pose, sets the kinematic targets, and writes the blended