@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/balance.js CHANGED
@@ -1,24 +1,25 @@
1
- import { n as e, r as t } from "./limits-CCe6Pdej.js";
2
- import { MargoError as n } from "@margoengine/math/errors";
3
- import { conjugateQuaternion as r, multiplyQuaternions as i, rotateByQuaternion as a } from "@margoengine/math/quat";
1
+ import { t as e } from "./errors-CDwcEs-j.js";
2
+ import { n as t, r as n } from "./limits-CCe6Pdej.js";
3
+ import { MargoError as r } from "@margoengine/math/errors";
4
+ import { conjugateQuaternion as i, multiplyQuaternions as a, rotateByQuaternion as o } from "@margoengine/math/quat";
4
5
  //#region src/balance.ts
5
- var o = (e, t) => [
6
+ var s = (e, t) => [
6
7
  e[0] + t[0],
7
8
  e[1] + t[1],
8
9
  e[2] + t[2]
9
- ], s = (e, t) => [
10
+ ], c = (e, t) => [
10
11
  e[0] - t[0],
11
12
  e[1] - t[1],
12
13
  e[2] - t[2]
13
- ], c = (e, t) => [
14
+ ], l = (e, t) => [
14
15
  e[0] * t,
15
16
  e[1] * t,
16
17
  e[2] * t
17
- ], l = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], u = (e) => Math.hypot(e[0], e[1], e[2]), d = (e) => c(e, 1 / u(e)), f = (e, t) => [
18
+ ], u = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], d = (e) => Math.hypot(e[0], e[1], e[2]), f = (e) => l(e, 1 / d(e)), p = (e, t) => [
18
19
  e[1] * t[2] - e[2] * t[1],
19
20
  e[2] * t[0] - e[0] * t[2],
20
21
  e[0] * t[1] - e[1] * t[0]
21
- ], p = (e, t) => {
22
+ ], m = (e, t) => {
22
23
  let n = Math.sin(t / 2);
23
24
  return [
24
25
  e[0] * n,
@@ -27,8 +28,8 @@ var o = (e, t) => [
27
28
  Math.cos(t / 2)
28
29
  ];
29
30
  };
30
- function m(e, t) {
31
- let n = f(e, t), r = 1 + l(e, t), i = Math.hypot(n[0], n[1], n[2], r);
31
+ function h(e, t) {
32
+ let n = p(e, t), r = 1 + u(e, t), i = Math.hypot(n[0], n[1], n[2], r);
32
33
  return i < 1e-9 ? [
33
34
  1,
34
35
  0,
@@ -41,13 +42,13 @@ function m(e, t) {
41
42
  r / i
42
43
  ];
43
44
  }
44
- var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w = .25, T = .25, E = .1, D = .8, O = .25, k = .4, A = (e) => e * e * (3 - 2 * e), j = (e, t, n, r, i) => {
45
- let a = e ?? t;
46
- if (!(a >= n && a <= r)) throw RangeError(`Ragdoll balance ${i} must be from ${n} to ${r}.`);
47
- return a;
48
- }, M = class {
49
- constructor(e, t, n) {
50
- this.ragdoll = e, this.motors = t, this.state = "standing", this.steps = 0, this.capture = [
45
+ var g = 400, _ = 12, v = 6, y = 30, b = 50, x = .15, S = .9, C = .012, w = 16, T = .25, E = .25, D = .1, O = .8, k = .25, A = .4, j = .25, M = .785, N = .53, P = (e) => e * e * (3 - 2 * e), F = (t, n, r, i, a) => {
46
+ let o = t ?? n;
47
+ if (!(o >= r && o <= i)) throw e(`Ragdoll balance ${a} must be from ${r} to ${i}.`);
48
+ return o;
49
+ }, I = class {
50
+ constructor(t, n, r) {
51
+ this.ragdoll = t, this.motors = n, this.state = "standing", this.steps = 0, this.capture = [
51
52
  0,
52
53
  0,
53
54
  0
@@ -55,7 +56,7 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
55
56
  0,
56
57
  1,
57
58
  0
58
- ], this.disposed = !1, this.settled = 0, this.stanceNow = 1, this.primed = !1, this.soles = [], this.hipsWere = null, this.hipsSpeed = [
59
+ ], this.disposed = !1, this.settled = 0, this.stanceNow = 1, this.held = 0, this.primed = !1, this.soles = [], this.hipsWere = null, this.hipsSpeed = [
59
60
  0,
60
61
  0,
61
62
  0
@@ -64,83 +65,92 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
64
65
  0,
65
66
  0
66
67
  ];
67
- let r = e.bones, i = r[0], a = (e) => {
68
- let t = e && "body" in e ? e.node : e, n = r.find((e) => e.node === t);
69
- if (!n) throw RangeError("That node is not a bone of this ragdoll; pass feet from ragdoll.bones.");
70
- return n;
68
+ let i = t.bones, a = i[0], o = (t) => {
69
+ let n = t && "body" in t ? t.node : t, r = i.find((e) => e.node === n);
70
+ if (!r) throw e("That node is not a bone of this ragdoll; pass feet from ragdoll.bones.");
71
+ return r;
71
72
  };
72
- if (!Array.isArray(n?.feet) || n.feet.length !== 2) throw TypeError("addRagdollBalance needs two feet: addRagdollBalance(ragdoll, motors, {feet: [left, right]}).");
73
- let p = (e) => {
74
- let t = a(e), n = r[t.parent], o = n && r[n.parent];
75
- if (!o || o === i || n.spec?.type !== "hinge") throw RangeError("Each foot needs a knee hinge and a thigh above it; build the ragdoll with humanoidProfile.");
76
- let c = o.body.position, f = n.body.position, p = t.body.position;
73
+ if (!Array.isArray(r?.feet) || r.feet.length !== 2) throw e("addRagdollBalance needs two feet: addRagdollBalance(ragdoll, motors, {feet: [left, right]}).");
74
+ let m = (t) => {
75
+ let n = o(t), r = i[n.parent], s = r && i[r.parent];
76
+ if (!s || s === a || r.spec?.type !== "hinge") throw e("Each foot needs a knee hinge and a thigh above it; build the ragdoll with humanoidProfile.");
77
+ let l = s.body.position, p = r.body.position, m = n.body.position;
77
78
  return {
78
- upper: o,
79
- lower: n,
80
- foot: t,
81
- thigh: u(s(f, c)),
82
- shin: u(s(p, f)),
83
- rest: d(s(f, c)),
84
- upperRest: [...o.body.rotation],
85
- footRest: [...t.body.rotation],
86
- hip: s(c, i.body.position),
79
+ upper: s,
80
+ lower: r,
81
+ foot: n,
82
+ thigh: d(c(p, l)),
83
+ shin: d(c(m, p)),
84
+ rest: f(c(p, l)),
85
+ upperRest: [...s.body.rotation],
86
+ footRest: [...n.body.rotation],
87
+ hip: c(l, a.body.position),
87
88
  kneeCap: 0,
88
89
  kneeInertia: 0,
89
90
  bend: 0,
90
- restBend: Math.acos(Math.max(-1, Math.min(1, l(d(s(f, c)), d(s(p, f)))))),
91
+ restBend: Math.acos(Math.max(-1, Math.min(1, u(f(c(p, l)), f(c(m, p)))))),
91
92
  sole: {
92
93
  front: 0,
93
94
  back: 0,
94
95
  left: 0,
95
96
  right: 0
96
97
  },
98
+ corners: [],
97
99
  plant: [
98
- p[0],
99
- p[1],
100
- p[2]
100
+ m[0],
101
+ m[1],
102
+ m[2]
101
103
  ],
102
- swing: null
104
+ swing: null,
105
+ pressed: 0,
106
+ load: 0
103
107
  };
104
108
  };
105
- this.legs = [p(n.feet[0]), p(n.feet[1])];
106
- let [m, h] = this.legs, g = new Set(this.legs.flatMap(({ upper: e, lower: t, foot: n }) => [
109
+ this.legs = [m(r.feet[0]), m(r.feet[1])];
110
+ let [h, g] = this.legs, _ = new Set(this.legs.flatMap(({ upper: e, lower: t, foot: n }) => [
107
111
  e,
108
112
  t,
109
113
  n
110
114
  ]));
111
- for (let e of r) g.has(r[e.parent]) && g.add(e);
112
- this.upper = r.filter((e) => !g.has(e)), this.standing = [...i.body.rotation];
113
- let _ = (m.thigh + m.shin + h.thigh + h.shin) / 2, v = c(o(m.plant, h.plant), .5), y = s(m.plant, h.plant);
114
- this.hipsOverFeet = s(i.body.position, v), this.floor = l(v, this.up), this.stanceLine = y, this.forward = d(f(d(y), this.up));
115
+ for (let e of i) _.has(i[e.parent]) && _.add(e);
116
+ this.upper = i.filter((e) => !_.has(e)), this.standing = [...a.body.rotation];
117
+ let v = (h.thigh + h.shin + g.thigh + g.shin) / 2, y = l(s(h.plant, g.plant), .5), b = c(h.plant, g.plant);
118
+ this.hipsOverFeet = c(a.body.position, y), this.floor = u(y, this.up), this.stanceLine = b, this.forward = f(p(f(b), this.up));
115
119
  for (let e of this.legs) this.flatten(e);
116
- this.total = r.reduce((e, t) => e + t.body.mass, 0), this.upperMass = this.upper.reduce((e, t) => e + t.body.mass, 0), this.upperShare = this.upperMass / this.total, this.height = l(this.hipsOverFeet, this.up), this.massPoints = r.map((e) => e.body.centerOfMass);
117
- let b = l(s(this.massCentre().position, v), this.up);
118
- this.gravity = j(n.gravity, 9.81, .01, 1e3, "gravity");
119
- let x = S * this.total * (m.thigh + m.shin) ** 2, w = Math.max(0, C - e.physics.solverIterations);
120
- for (let e of r) e.body.additionalPgsIterations = w;
121
- for (let { foot: e, lower: n } of this.legs) for (let r of [e, n]) t.addSteadying(r, x);
122
- for (let e of this.legs) e.kneeCap = this.total * this.gravity * Math.min(e.thigh, e.shin), e.kneeInertia = t.limbInertia(e.lower);
123
- this.natural = Math.sqrt(this.gravity / b), this.stepTime = j(n.stepTime, .28, .05, 2, "stepTime"), this.lift = j(n.stepHeight, .15, 0, 1, "stepHeight") * _, this.trigger = j(n.stepTrigger, .2, 0, 2, "stepTrigger") * _, this.stance = j(n.stance, .97, .5, 1, "stance"), this.carry = j(n.carry, 0, 0, 1, "carry"), this.upright = j(n.upright, 10, 0, 1e3, "upright"), this.maxUpright = j(n.maxUpright, 40, 0, 1e6, "maxUpright"), this.fallTilt = j(n.fallTilt, .8, .01, Math.PI, "fallTilt"), this.wanted = new Float64Array(r.length * 6), this.given = new Float64Array(r.length * 6), this.off = e.physics.on("beforestep", (e) => this.step(e));
120
+ this.total = i.reduce((e, t) => e + t.body.mass, 0), this.upperMass = this.upper.reduce((e, t) => e + t.body.mass, 0), this.upperShare = this.upperMass / this.total, this.height = u(this.hipsOverFeet, this.up), this.massPoints = i.map((e) => e.body.centerOfMass);
121
+ let x = u(c(this.massCentre().position, y), this.up);
122
+ this.gravity = F(r.gravity, 9.81, .01, 1e3, "gravity");
123
+ let S = C * this.total * (h.thigh + h.shin) ** 2, T = Math.max(0, w - t.physics.solverIterations);
124
+ for (let e of i) e.body.additionalPgsIterations = T;
125
+ for (let { foot: e, lower: t } of this.legs) for (let r of [e, t]) n.addSteadying(r, S);
126
+ for (let e of this.legs) e.kneeCap = this.total * this.gravity * Math.min(e.thigh, e.shin), e.kneeInertia = n.limbInertia(e.lower);
127
+ this.natural = Math.sqrt(this.gravity / x), this.stepTime = F(r.stepTime, .28, .05, 2, "stepTime"), this.lift = F(r.stepHeight, .15, 0, 1, "stepHeight") * v, this.trigger = F(r.stepTrigger, .2, 0, 2, "stepTrigger") * v, this.stance = F(r.stance, .97, .5, 1, "stance"), this.carry = F(r.carry, 0, 0, 1, "carry"), this.upright = F(r.upright, 10, 0, 1e3, "upright"), this.maxUpright = F(r.maxUpright, 40, 0, 1e6, "maxUpright"), this.fallTilt = F(r.fallTilt, .8, .01, Math.PI, "fallTilt"), this.wanted = new Float64Array(i.length * 6), this.given = new Float64Array(i.length * 6), this.off = t.physics.on("beforestep", (e) => this.step(e)), this.offForces = t.physics.on("contactforce", (e) => {
128
+ for (let t of this.legs) (e.bodyA === t.foot.body || e.bodyB === t.foot.body) && (t.pressed += Math.abs(u(e.totalForce, this.up)));
129
+ });
124
130
  }
125
131
  get tilt() {
126
- let e = a(this.up, i(this.ragdoll.bones[0].body.rotation, r(this.standing)));
127
- return Math.acos(Math.min(1, Math.max(-1, l(e, this.up))));
132
+ let e = o(this.up, a(this.ragdoll.bones[0].body.rotation, i(this.standing)));
133
+ return Math.acos(Math.min(1, Math.max(-1, u(e, this.up))));
128
134
  }
129
135
  step(e) {
130
- !this.disposed && this.ragdoll.bones[0].body.valid && (this.wanted.fill(0), this.readHips(e), this.control(e), this.settle());
136
+ if (!this.disposed && this.ragdoll.bones[0].body.valid) {
137
+ this.wanted.fill(0);
138
+ for (let e of this.legs) e.load = e.pressed, e.pressed = 0;
139
+ this.readHips(e), this.control(e), this.settle();
140
+ }
131
141
  }
132
142
  readHips(e) {
133
143
  let t = this.ragdoll.bones[0].body, n = [
134
144
  t.position[0],
135
145
  t.position[1],
136
146
  t.position[2]
137
- ], a = [...t.rotation], o = this.hipsWere;
147
+ ], r = [...t.rotation], o = this.hipsWere;
138
148
  if (o) {
139
- let t = i(a, r(o.rotation)), l = t[3] < 0 ? -2 / e : 2 / e;
140
- this.hipsSpeed = c(s(n, o.position), 1 / e), this.hipsSpin = [
141
- t[0] * l,
142
- t[1] * l,
143
- t[2] * l
149
+ let t = a(r, i(o.rotation)), s = t[3] < 0 ? -2 / e : 2 / e;
150
+ this.hipsSpeed = l(c(n, o.position), 1 / e), this.hipsSpin = [
151
+ t[0] * s,
152
+ t[1] * s,
153
+ t[2] * s
144
154
  ];
145
155
  } else this.hipsSpeed = [
146
156
  t.linearVelocity[0],
@@ -153,7 +163,7 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
153
163
  ];
154
164
  this.hipsWere = {
155
165
  position: n,
156
- rotation: a
166
+ rotation: r
157
167
  };
158
168
  }
159
169
  settle() {
@@ -192,20 +202,49 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
192
202
  e.foot.body.position[1],
193
203
  e.foot.body.position[2]
194
204
  ];
195
- e.plant = o(t, c(this.up, this.floor - l(t, this.up)));
205
+ e.plant = s(t, l(this.up, this.floor - u(t, this.up)));
196
206
  }
197
207
  this.capture = this.capturePoint();
198
- let [t, n] = this.legs, r = c(o(t.plant, n.plant), .5);
199
- !(t.swing ? t : n.swing && n) && this.outside(t.plant, n.plant, w) > this.trigger && this.startStep();
200
- let i = t.swing ? t : n.swing ? n : null, a = r;
201
- if (i) {
202
- let e = i === t ? n : t, s = A(Math.min(1, i.swing.t)), l = c(o(i.swing.to, e.plant), .5);
203
- a = o(c(r, 1 - s), c(l, s));
208
+ let [t, n] = this.legs, r = l(s(this.aim(t, t.plant), this.aim(n, n.plant)), .5), i = t.swing ? t : n.swing ? n : null;
209
+ !i && (this.outside(t.plant, n.plant, T) > this.trigger || this.offSupport()) ? this.startStep() : i || this.gather();
210
+ let a = t.swing ? t : n.swing ? n : null, o = r, d = a ? null : this.carryOver();
211
+ if (d && (o = this.aim(d, d.plant)), a) {
212
+ let e = a === t ? n : t, i = P(Math.min(1, a.swing.t)), c = l(s(this.aim(a, a.swing.to), this.aim(e, e.plant)), .5);
213
+ o = s(l(r, 1 - i), l(c, i));
204
214
  }
205
- this.settled = Math.min(1, this.settled + e / x), this.stanceNow = 1 + (this.stance - 1) * A(this.settled) ** 2;
206
- let u = s(this.hipsOverFeet, c(this.up, this.height)), d = o(o(a, u), c(this.up, this.height * this.stanceNow));
207
- for (let t of this.legs) this.driveLeg(t, d, e);
208
- this.state = this.legs.some((e) => e.swing) ? "stepping" : "standing", this.holdJoints(), this.primed && this.carryWeight(e, this.legs.filter((e) => !e.swing), a), this.primed = !0, this.assist(e, r);
215
+ this.settled = Math.min(1, this.settled + e / S), this.stanceNow = 1 + (this.stance - 1) * P(this.settled) ** 2;
216
+ let f = c(this.hipsOverFeet, l(this.up, this.height));
217
+ if (this.held = this.height * this.stanceNow, a) {
218
+ let e = a === t ? n : t, r = s(l(s(this.aim(a, a.swing.to), this.aim(e, e.plant)), .5), f);
219
+ this.held = Math.min(this.held, this.reachHeight(a, r, a.swing.to));
220
+ }
221
+ let p = s(s(o, f), l(this.up, this.held));
222
+ for (let t of this.legs) this.driveLeg(t, p, e);
223
+ this.state = this.legs.some((e) => e.swing) ? "stepping" : "standing", this.holdJoints(), this.primed && this.carryWeight(e, this.legs.filter((e) => !e.swing), o), this.primed = !0, this.assist(e, r);
224
+ }
225
+ longest(e) {
226
+ let t = e.lower.spec, n = Math.max(0, e.restBend + t.limits[0]), { thigh: r, shin: i } = e;
227
+ return Math.min((r + i) * .999, Math.sqrt(r * r + i * i + 2 * r * i * Math.cos(n)));
228
+ }
229
+ reachHeight(e, t, n) {
230
+ let r = c(n, s(t, e.hip)), i = d(c(r, l(this.up, u(r, this.up)))), a = this.longest(e);
231
+ return Math.sqrt(Math.max(0, a * a - i * i)) - u(e.hip, this.up);
232
+ }
233
+ stepLength(e) {
234
+ let t = e.foot.shape, n = 0;
235
+ if (t.type === "hull") {
236
+ n = Infinity;
237
+ for (let r = 0; r + 2 < t.points.length; r += 3) n = Math.min(n, u(o([
238
+ t.points[r],
239
+ t.points[r + 1],
240
+ t.points[r + 2]
241
+ ], e.foot.body.rotation), this.up));
242
+ }
243
+ return M * (this.height + u(e.hip, this.up) - n) / N;
244
+ }
245
+ aim(e, t) {
246
+ let { front: n, back: r } = e.sole;
247
+ return s(t, l(this.forward, Math.max(0, j * (n + r) - r)));
209
248
  }
210
249
  massCentre() {
211
250
  let e = [
@@ -218,11 +257,11 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
218
257
  0
219
258
  ];
220
259
  return this.ragdoll.bones.forEach((n, r) => {
221
- let i = n.body, s = o(i.position, a(this.massPoints[r], i.rotation)), c = i.linearVelocity, l = i.mass;
222
- for (let n = 0; n < 3; n++) e[n] += s[n] * l, t[n] += c[n] * l;
260
+ let i = n.body, a = s(i.position, o(this.massPoints[r], i.rotation)), c = i.linearVelocity, l = i.mass;
261
+ for (let n = 0; n < 3; n++) e[n] += a[n] * l, t[n] += c[n] * l;
223
262
  }), {
224
- position: c(e, 1 / this.total),
225
- velocity: c(t, 1 / this.total)
263
+ position: l(e, 1 / this.total),
264
+ velocity: l(t, 1 / this.total)
226
265
  };
227
266
  }
228
267
  holdJoints() {
@@ -232,55 +271,55 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
232
271
  let n = this.motors.limbInertia(t.foot);
233
272
  t.foot.joint.setRotationMotor(this.swingFoot(t), {
234
273
  model: "force",
235
- stiffness: n * v * v * e,
236
- damping: 2 * n * v * Math.sqrt(e),
237
- maxForce: n * h * e
274
+ stiffness: n * y * y * e,
275
+ damping: 2 * n * y * Math.sqrt(e),
276
+ maxForce: n * g * e
238
277
  });
239
278
  continue;
240
279
  }
241
- let n = Math.min(1, Math.abs(t.bend - this.kneeAngle(t)) / b) * e;
280
+ let n = Math.min(1, Math.abs(t.bend - this.kneeAngle(t)) / x) * e;
242
281
  n > 0 ? t.lower.joint.setMotor({
243
282
  model: "force",
244
283
  targetPosition: t.bend,
245
284
  targetVelocity: 0,
246
- stiffness: t.kneeInertia * y * y * n,
285
+ stiffness: t.kneeInertia * b * b * n,
247
286
  damping: 0,
248
- maxForce: t.kneeInertia * h * e
287
+ maxForce: t.kneeInertia * g * e
249
288
  }) : t.lower.joint.clearMotor();
250
289
  }
251
290
  }
252
291
  swingFoot(e) {
253
292
  return this.levelFoot(e, this.ragdoll.bones[0].body.rotation, this.standing);
254
293
  }
255
- levelFoot(n, a, o = n.footRest) {
256
- let s = t(i(a, r(o)), this.up).twist, c = i(p(this.up, s), n.footRest);
257
- return e(n.foot, i(r(n.lower.body.rotation), c));
294
+ levelFoot(e, r, o = e.footRest) {
295
+ let s = n(a(r, i(o)), this.up).twist, c = a(m(this.up, s), e.footRest);
296
+ return t(e.foot, a(i(e.lower.body.rotation), c));
258
297
  }
259
298
  kneeAngle(e) {
260
- let t = e.lower.spec.axis, n = i(r(e.lower.rest), i(r(e.upper.body.rotation), e.lower.body.rotation));
261
- return 2 * Math.atan2(l(n, t), n[3]);
299
+ let t = e.lower.spec.axis, n = a(i(e.lower.rest), a(i(e.upper.body.rotation), e.lower.body.rotation));
300
+ return 2 * Math.atan2(u(n, t), n[3]);
262
301
  }
263
302
  planted(e) {
264
303
  return this.legs.some((t) => !t.swing && (e === t.upper || e === t.lower || e === t.foot));
265
304
  }
266
305
  soleOf(e) {
267
- let t = e.foot, n = t.shape, r = f(this.up, this.forward), i = {
306
+ let t = e.foot, n = t.shape, r = p(this.up, this.forward), i = {
268
307
  front: t.length / 2,
269
308
  back: t.length / 2,
270
309
  left: t.length / 4,
271
310
  right: t.length / 4
272
311
  };
273
312
  if (n.type !== "hull") return i;
274
- let o = [];
275
- for (let e = 0; e + 2 < n.points.length; e += 3) o.push(a([
313
+ let a = [];
314
+ for (let e = 0; e + 2 < n.points.length; e += 3) a.push(o([
276
315
  n.points[e],
277
316
  n.points[e + 1],
278
317
  n.points[e + 2]
279
318
  ], t.body.rotation));
280
- let s = Math.min(...o.map((e) => l(e, this.up))), c = o.filter((e) => l(e, this.up) < s + t.length * E), u = c.map((e) => l(e, this.forward)), d = c.map((e) => l(e, r));
319
+ let s = Math.min(...a.map((e) => u(e, this.up))), c = a.filter((e) => u(e, this.up) < s + t.length * D), l = c.map((e) => u(e, this.forward)), d = c.map((e) => u(e, r));
281
320
  return {
282
- front: Math.max(...u),
283
- back: -Math.min(...u),
321
+ front: Math.max(...l),
322
+ back: -Math.min(...l),
284
323
  left: Math.max(...d),
285
324
  right: -Math.min(...d)
286
325
  };
@@ -289,13 +328,13 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
289
328
  e.sole = this.soleOf(e);
290
329
  let t = e.foot, n = t.shape;
291
330
  if (n.type !== "hull") return;
292
- let i = t.body.rotation, s = r(i), u = f(this.up, this.forward), d = [];
293
- for (let e = 0; e + 2 < n.points.length; e += 3) d.push(a([
331
+ let r = t.body.rotation, a = i(r), c = p(this.up, this.forward), d = [];
332
+ for (let e = 0; e + 2 < n.points.length; e += 3) d.push(o([
294
333
  n.points[e],
295
334
  n.points[e + 1],
296
335
  n.points[e + 2]
297
- ], i));
298
- let p = Math.min(...d.map((e) => l(e, this.up))), m = d.filter((e) => l(e, this.up) >= p + t.length * E), { front: h, back: g, left: _, right: v } = e.sole, y = (e, t) => o(o(c(this.forward, e), c(u, t)), c(this.up, p)), b = [
336
+ ], r));
337
+ let f = Math.min(...d.map((e) => u(e, this.up))), m = d.filter((e) => u(e, this.up) >= f + t.length * D), { front: h, back: g, left: _, right: v } = e.sole, y = (e, t) => s(s(l(this.forward, e), l(c, t)), l(this.up, f)), b = [
299
338
  y(h, _),
300
339
  y(h, -v),
301
340
  y(-g, _),
@@ -304,42 +343,42 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
304
343
  this.soles.push({
305
344
  part: t,
306
345
  shape: n
307
- }), this.ragdoll.setShape(t, {
346
+ }), e.corners = b.map((e) => o(e, a)), this.ragdoll.setShape(t, {
308
347
  type: "hull",
309
- points: [...b, ...m].flatMap((e) => [...a(e, s)])
348
+ points: [...b, ...m].flatMap((e) => [...o(e, a)])
310
349
  });
311
350
  }
312
351
  carryWeight(e, t, n) {
313
352
  if (!t.length) return;
314
- let { position: r, velocity: i } = this.massCentre(), d = this.ragdoll.bones[0].body, p = this.ragdoll.bones, m = t.length, h = this.motors.strength, v = f(this.up, this.forward), y = this.ragdoll.settings.friction, b = (t) => 1 / (1 + 2 * t * e + t * t * e * e), x = l(c(t.reduce((e, t) => o(e, t.plant), [
353
+ let { position: r, velocity: i } = this.massCentre(), a = this.ragdoll.bones[0].body, f = this.ragdoll.bones, m = t.length, h = this.motors.strength, g = p(this.up, this.forward), y = this.ragdoll.settings.friction, b = (t) => 1 / (1 + 2 * t * e + t * t * e * e), x = u(l(t.reduce((e, t) => s(e, t.plant), [
315
354
  0,
316
355
  0,
317
356
  0
318
- ]), 1 / m), this.up) + this.height * this.stanceNow, S = b(g) * (144 * (x - l(d.position, this.up)) - 24 * l(this.hipsSpeed, this.up)), C = _, w = c(s(c(s(n, r), 36), c(i, 12)), b(C)), T = s(w, c(this.up, l(w, this.up))), E = this.total * Math.max(0, this.gravity + S) / m, O = u(T) * this.total / m, k = c(T, this.total / m * Math.min(1, D * y * E / Math.max(O, 1e-9))), A = (e) => o(e.body.position, a(this.massPoints[p.indexOf(e)], e.body.rotation)), j = (e) => c(this.up, e.body.mass * this.gravity), M = 0, N = [
357
+ ]), 1 / m), this.up) + this.held, S = b(_) * (144 * (x - u(a.position, this.up)) - 24 * u(this.hipsSpeed, this.up)), C = v, w = l(c(l(c(n, r), 36), l(i, 12)), b(C)), T = c(w, l(this.up, u(w, this.up))), E = this.total * Math.max(0, this.gravity + S) / m, D = d(T) * this.total / m, k = l(T, this.total / m * Math.min(1, O * y * E / Math.max(D, 1e-9))), A = (e) => s(e.body.position, o(this.massPoints[f.indexOf(e)], e.body.rotation)), j = (e) => l(this.up, e.body.mass * this.gravity), M = 0, N = [
319
358
  0,
320
359
  0,
321
360
  0
322
361
  ];
323
- for (let e of p) {
362
+ for (let e of f) {
324
363
  if (this.planted(e)) continue;
325
364
  let t = A(e);
326
365
  M += e.body.mass;
327
366
  for (let n = 0; n < 3; n++) N[n] += t[n] * e.body.mass;
328
367
  }
329
- let P = c(N, 1 / M), F = M * Math.max(0, this.gravity + S), I = m === 2 ? s(t[1].plant, t[0].plant) : [
368
+ let P = l(N, 1 / M), F = M * Math.max(0, this.gravity + S), I = m === 2 ? c(t[1].plant, t[0].plant) : [
330
369
  0,
331
370
  0,
332
371
  0
333
- ], L = s(I, c(this.up, l(I, this.up))), R = m === 2 ? Math.min(1, Math.max(0, l(s(r, t[0].plant), L) / Math.max(l(L, L), 1e-9))) : 0, z = t.map((e, t) => {
334
- let n = m === 2 ? t ? R : 1 - R : 1, r = c(this.up, F * n), i = m === 2 ? c(L, t ? R - 1 : R) : [
372
+ ], L = c(I, l(this.up, u(I, this.up))), R = m === 2 ? Math.min(1, Math.max(0, u(c(r, t[0].plant), L) / Math.max(u(L, L), 1e-9))) : 0, z = t.map((e, t) => {
373
+ let n = m === 2 ? t ? R : 1 - R : 1, r = l(this.up, F * n), i = m === 2 ? l(L, t ? R - 1 : R) : [
335
374
  0,
336
375
  0,
337
376
  0
338
- ], a = e.upper.body.position, l = e.lower.body.position, u = e.foot.body.position, d = A(e.upper), p = A(e.lower);
377
+ ], a = e.upper.body.position, o = e.lower.body.position, u = e.foot.body.position, d = A(e.upper), f = A(e.lower);
339
378
  return {
340
379
  leg: e,
341
- atKnee: o(o(f(s(P, l), r), f(s(d, l), j(e.upper))), f(s(a, l), k)),
342
- atAnkle: o(o(o(f(s(s(P, i), u), r), f(s(d, u), j(e.upper))), f(s(p, u), j(e.lower))), f(s(a, u), k)),
380
+ atKnee: s(s(p(c(P, o), r), p(c(d, o), j(e.upper))), p(c(a, o), k)),
381
+ atAnkle: s(s(s(p(c(c(P, i), u), r), p(c(d, u), j(e.upper))), p(c(f, u), j(e.lower))), p(c(a, u), k)),
343
382
  foot: E * m * n
344
383
  };
345
384
  }), { turn: B, points: V, centre: H } = this.uprightTurn(e), U = [
@@ -348,26 +387,29 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
348
387
  0
349
388
  ];
350
389
  this.upper.forEach((e, t) => {
351
- let n = s(V[t], H);
352
- U = o(U, f(n, c(f(B, n), e.body.mass)));
390
+ let n = c(V[t], H);
391
+ U = s(U, p(n, l(p(B, n), e.body.mass)));
353
392
  });
354
- let W = c(U, 1 / (e * m)), G = m === 2, K = 1, q = (e, t, n, r) => Math.abs(t) < 1e-9 ? 1 : Math.max(0, Math.min(1, ((t > 0 ? r : n) - e) / t));
355
- if (G) for (let { leg: e, atAnkle: t, foot: n } of z) K = Math.min(K, q(l(t, v), l(W, v), -e.sole.front * n, e.sole.back * n)), K = Math.min(K, q(l(t, this.forward), l(W, this.forward), -e.sole.right * n, e.sole.left * n));
356
- let J = c(W, K), Y = (e, t, n) => G ? Math.max(t, Math.min(n, e)) : e;
357
- this.upper.forEach((t, n) => this.give(t, c(f(c(B, K), s(V[n], H)), t.body.mass / e), null));
358
- for (let { leg: e, atKnee: t, atAnkle: n, foot: r } of z) {
359
- let i = a(e.lower.spec.axis, e.lower.body.rotation), u = l(o(t, J), i), d = c(i, Math.max(-e.kneeCap, Math.min(e.kneeCap, u)) * h), f = o(n, J), p = Y(l(f, v), -e.sole.front * r, e.sole.back * r), m = Y(l(f, this.forward), -e.sole.right * r, e.sole.left * r), g = .5 * y * r * e.sole.left, _ = Y(l(f, this.up), -g, g), b = c(o(o(c(v, p), c(this.forward, m)), c(this.up, _)), h);
360
- this.give(e.upper, null, s(d, J)), this.give(e.lower, null, s(b, d)), this.give(e.foot, null, c(b, -1));
393
+ let W = l(U, 1 / (e * m)), G = m === 2, K = t.map((e) => this.soleNow(e)), q = 1, J = (e, t, n, r) => Math.abs(t) < 1e-9 ? 1 : Math.max(0, Math.min(1, ((t > 0 ? r : n) - e) / t));
394
+ G && z.forEach(({ atAnkle: e, foot: t }, n) => {
395
+ let r = K[n];
396
+ q = Math.min(q, J(u(e, g), u(W, g), -r.front * t, r.back * t)), q = Math.min(q, J(u(e, this.forward), u(W, this.forward), -r.right * t, r.left * t));
397
+ });
398
+ let Y = l(W, q), X = (e, t, n) => Math.max(t, Math.min(n, e));
399
+ this.upper.forEach((t, n) => this.give(t, l(p(l(B, q), c(V[n], H)), t.body.mass / e), null));
400
+ for (let [e, { leg: t, atKnee: n, atAnkle: r, foot: i }] of z.entries()) {
401
+ let a = K[e], d = o(t.lower.spec.axis, t.lower.body.rotation), f = u(s(n, Y), d), p = l(d, Math.max(-t.kneeCap, Math.min(t.kneeCap, f)) * h), m = s(r, Y), _ = X(u(m, g), -a.front * i, a.back * i), v = X(u(m, this.forward), -a.right * i, a.left * i), b = .5 * y * i * t.sole.left, x = X(u(m, this.up), -b, b), S = l(s(s(l(g, _), l(this.forward, v)), l(this.up, x)), h);
402
+ this.give(t.upper, null, c(p, Y)), this.give(t.lower, null, c(S, p)), this.give(t.foot, null, l(S, -1));
361
403
  }
362
404
  }
363
405
  uprightTurn(e) {
364
- let t = this.ragdoll.bones[0].body, n = i(this.standing, r(t.rotation)), 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) * s / c : 2 * s, d = this.upright, f = 2 * d, p = e / (1 + f * e + d * d * e * e), m = this.hipsSpin, h = [
406
+ let t = this.ragdoll.bones[0].body, n = a(this.standing, i(t.rotation)), r = n[3] < 0 ? -1 : 1, c = Math.hypot(n[0], n[1], n[2]), l = c > 1e-9 ? 2 * Math.atan2(c, n[3] * r) * r / c : 2 * r, u = this.upright, f = 2 * u, p = e / (1 + f * e + u * u * e * e), m = this.hipsSpin, h = [
365
407
  0,
366
408
  1,
367
409
  2
368
- ].map((e) => p * (d * d * n[e] * l - f * m[e])), g = this.maxUpright * e, _ = u(h);
410
+ ].map((e) => p * (u * u * n[e] * l - f * m[e])), g = this.maxUpright * e, _ = d(h);
369
411
  if (_ > g) for (let e = 0; e < 3; e++) h[e] *= g / _;
370
- let v = this.ragdoll.bones, y = this.upper.map((e) => o(e.body.position, a(this.massPoints[v.indexOf(e)], e.body.rotation))), b = [
412
+ let v = this.ragdoll.bones, y = this.upper.map((e) => s(e.body.position, o(this.massPoints[v.indexOf(e)], e.body.rotation))), b = [
371
413
  0,
372
414
  0,
373
415
  0
@@ -380,50 +422,128 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
380
422
  centre: b
381
423
  };
382
424
  }
425
+ soleNow(e) {
426
+ if (!e.corners.length) return e.sole;
427
+ let t = p(this.up, this.forward), n = e.foot.body.rotation, r = -Infinity, i = -Infinity, a = -Infinity, s = -Infinity;
428
+ for (let c of e.corners) {
429
+ let e = o(c, n), l = u(e, this.forward), d = u(e, t);
430
+ r = Math.max(r, l), i = Math.max(i, -l), a = Math.max(a, d), s = Math.max(s, -d);
431
+ }
432
+ return {
433
+ front: r,
434
+ back: i,
435
+ left: a,
436
+ right: s
437
+ };
438
+ }
383
439
  capturePoint() {
384
- let { position: e, velocity: t } = this.massCentre(), n = o(e, c(t, 1 / this.natural)), r = Math.min(l(this.legs[0].plant, this.up), l(this.legs[1].plant, this.up));
385
- return o(n, c(this.up, r - l(n, this.up)));
440
+ let { position: e, velocity: t } = this.massCentre(), n = s(e, l(t, 1 / this.natural)), r = Math.min(u(this.legs[0].plant, this.up), u(this.legs[1].plant, this.up));
441
+ return s(n, l(this.up, r - u(n, this.up)));
386
442
  }
387
443
  outside(e, t, n = 0) {
388
- let r = s(t, e), i = s(r, c(this.up, l(r, this.up))), a = Math.min(1 - n, Math.max(n, l(s(this.capture, e), i) / Math.max(l(i, i), 1e-9))), d = s(this.capture, o(e, c(i, a)));
389
- return u(s(d, c(this.up, l(d, this.up))));
444
+ let r = c(t, e), i = c(r, l(this.up, u(r, this.up))), a = Math.min(1 - n, Math.max(n, u(c(this.capture, e), i) / Math.max(u(i, i), 1e-9))), o = c(this.capture, s(e, l(i, a)));
445
+ return d(c(o, l(this.up, u(o, this.up))));
446
+ }
447
+ over(e) {
448
+ let t = p(this.up, this.forward), n = c(this.capture, e.plant), r = u(n, this.forward), i = u(n, t);
449
+ return r <= e.sole.front && r >= -e.sole.back && i <= e.sole.left && i >= -e.sole.right;
450
+ }
451
+ gather() {
452
+ let [e, t] = this.legs;
453
+ for (let [n, r, i] of [[
454
+ e,
455
+ t,
456
+ -1
457
+ ], [
458
+ t,
459
+ e,
460
+ 1
461
+ ]]) {
462
+ if (!this.over(n) || this.over(r) && r.load > n.load) continue;
463
+ let e = s(n.plant, l(this.stanceLine, i));
464
+ e = s(e, l(this.up, this.floor - u(e, this.up)));
465
+ let t = c(e, r.plant);
466
+ if (!(d(c(t, l(this.up, u(t, this.up)))) <= this.trigger)) {
467
+ r.swing = {
468
+ from: r.plant,
469
+ to: e,
470
+ t: 0,
471
+ time: this.stepDuration(n)
472
+ }, this.steps++;
473
+ return;
474
+ }
475
+ }
476
+ }
477
+ carryOver() {
478
+ let [e, t] = this.legs;
479
+ if (!this.legs.some((e) => j * (e.sole.front + e.sole.back) > e.sole.back) || this.over(e) || this.over(t)) return null;
480
+ let n = c(c(e.plant, t.plant), this.stanceLine);
481
+ if (d(c(n, l(this.up, u(n, this.up)))) <= this.trigger) return null;
482
+ let r = (e) => {
483
+ let t = c(this.capture, this.aim(e, e.plant));
484
+ return d(c(t, l(this.up, u(t, this.up))));
485
+ };
486
+ return r(e) <= r(t) ? e : t;
487
+ }
488
+ offSupport() {
489
+ let e = p(this.up, this.forward), t = [];
490
+ for (let n of this.legs) {
491
+ let r = u(n.plant, this.forward), i = u(n.plant, e), { front: a, back: o, left: s, right: c } = n.sole;
492
+ t.push([r + a, i + s], [r + a, i - c], [r - o, i + s], [r - o, i - c]);
493
+ }
494
+ t.sort((e, t) => e[0] - t[0] || e[1] - t[1]);
495
+ let n = (e, t, n) => (t[0] - e[0]) * (n[1] - e[1]) - (t[1] - e[1]) * (n[0] - e[0]), r = (e) => {
496
+ let t = [];
497
+ for (let r of e) {
498
+ for (; t.length > 1 && n(t[t.length - 2], t[t.length - 1], r) <= 0;) t.pop();
499
+ t.push(r);
500
+ }
501
+ return t.pop(), t;
502
+ }, i = [...r(t), ...r([...t].reverse())], a = [u(this.capture, this.forward), u(this.capture, e)];
503
+ return i.some((e, t) => n(e, i[(t + 1) % i.length], a) < 0);
390
504
  }
391
505
  startStep() {
392
- let [e, t] = this.legs, n = this.landing(e), r = this.landing(t), i = this.outside(n, t.plant), a = this.outside(r, e.plant), o = this.lifted(e), c = o === this.lifted(t) ? Math.abs(i - a) > this.trigger * T ? i < a : u(s(n, e.plant)) >= u(s(r, t.plant)) : o, l = c ? e : t;
506
+ let [e, t] = this.legs, n = this.landing(e), r = this.landing(t), i = this.outside(n, t.plant), a = this.outside(r, e.plant), o = this.lifted(e), s = o === this.lifted(t) ? Math.abs(i - a) > this.trigger * E ? i < a : d(c(n, e.plant)) >= d(c(r, t.plant)) : o, l = s ? e : t;
393
507
  l.swing = {
394
508
  from: l.plant,
395
- to: c ? n : r,
509
+ to: s ? n : r,
396
510
  t: 0,
397
- time: this.stepDuration(c ? t : e)
511
+ time: this.stepDuration(s ? t : e)
398
512
  }, this.steps++;
399
513
  }
400
514
  stepDuration(e) {
401
- let t = s(this.capture, e.plant), n = u(s(t, c(this.up, l(t, this.up)))), r = this.height * this.stance, i = e.thigh + e.shin, a = Math.sqrt(Math.max(0, i * i * .9 - r * r)) + u(this.stanceLine) / 2, o = k / this.natural;
402
- return n > 1e-6 ? Math.max(o, Math.min(this.stepTime, Math.log(Math.max(1, a / n)) / this.natural)) : this.stepTime;
515
+ let t = c(this.capture, e.plant), n = d(c(t, l(this.up, u(t, this.up)))), r = this.stepLength(e), i = A / this.natural;
516
+ if (!(n > 1e-6)) return this.stepTime;
517
+ let a = Math.min(this.stepTime, Math.log(Math.max(1, r / n)) / this.natural), o = p(this.up, this.forward), s = e === this.legs[0] ? this.legs[1] : this.legs[0], f = u(t, this.forward), m = u(t, o), h = f - Math.max(-e.sole.back, Math.min(e.sole.front, f)), g = m - Math.max(-e.sole.right, Math.min(e.sole.left, m)), _ = Math.hypot(h, g);
518
+ if (_ > 1e-6) {
519
+ let e = (h > 0 ? s.sole.front : s.sole.back) * Math.abs(h / _) + (g > 0 ? s.sole.left : s.sole.right) * Math.abs(g / _);
520
+ a = Math.min(a, Math.log(1 + e / _) / this.natural);
521
+ }
522
+ return Math.max(i, a);
403
523
  }
404
524
  lifted(e) {
405
- return l(e.foot.body.position, this.up) - this.floor > this.lift * O;
525
+ return u(e.foot.body.position, this.up) - this.floor > this.lift * k;
406
526
  }
407
527
  landing(e) {
408
- let t = o(this.ragdoll.bones[0].body.position, e.hip), n = this.height * this.stance, r = e === this.legs[0] ? .5 : -.5, i = o(this.capture, c(this.stanceLine, r));
409
- i = o(i, c(this.up, this.floor - l(i, this.up)));
410
- let a = Math.sqrt(Math.max(0, (e.thigh + e.shin) ** 2 * .9 - n * n)), d = s(i, t), f = s(d, c(this.up, l(d, this.up))), p = u(f);
411
- return p > a && (i = o(i, c(f, a / p - 1))), i;
528
+ let t = e === this.legs[0] ? this.legs[1] : this.legs[0], n = e === this.legs[0] ? .5 : -.5, r = s(this.capture, l(this.stanceLine, n));
529
+ r = s(r, l(this.up, this.floor - u(r, this.up)));
530
+ let i = this.stepLength(t), a = c(r, t.plant), o = c(a, l(this.up, u(a, this.up))), f = d(o);
531
+ return f > i && (r = s(r, l(o, i / f - 1))), r;
412
532
  }
413
533
  driveLeg(e, t, n) {
414
- let l = this.motors, h = this.ragdoll.bones[0].body, g = e.plant;
534
+ let r = this.motors, u = this.ragdoll.bones[0].body, g = e.plant;
415
535
  if (e.swing) {
416
536
  let t = e.swing;
417
537
  t.t += n / t.time;
418
- let r = Math.min(1, t.t), i = A(r);
419
- g = o(o(c(t.from, 1 - i), c(t.to, i)), c(this.up, this.lift * Math.sin(Math.PI * r))), t.t >= 1 && (e.swing = null);
538
+ let r = Math.min(1, t.t), i = P(r);
539
+ g = s(s(l(t.from, 1 - i), l(t.to, i)), l(this.up, this.lift * Math.sin(Math.PI * r))), t.t >= 1 && (e.swing = null);
420
540
  }
421
- let _ = s(g, o(t, e.hip)), { thigh: v, shin: y } = e, b = Math.min(u(_), (v + y) * .999), x = d(_), S = Math.acos(Math.max(-1, Math.min(1, (v * v + y * y - b * b) / (2 * v * y)))), C = Math.acos(Math.max(-1, Math.min(1, (v * v + b * b - y * y) / (2 * v * b)))), w = a(x, p(d(f(x, this.forward)), C)), T = i(m(e.rest, w), e.upperRest), E = e.lower.spec, D = Math.min(E.limits[1], Math.max(E.limits[0], Math.PI - S - e.restBend));
422
- l.setTarget(e.upper, i(r(h.rotation), T)), l.setTarget(e.lower, i(e.lower.rest, p(E.axis, D))), l.setTarget(e.foot, this.levelFoot(e, e.foot.body.rotation)), e.bend = D;
541
+ let _ = c(g, s(t, e.hip)), { thigh: v, shin: y } = e, b = Math.min(d(_), (v + y) * .999), x = f(_), S = Math.acos(Math.max(-1, Math.min(1, (v * v + y * y - b * b) / (2 * v * y)))), C = Math.acos(Math.max(-1, Math.min(1, (v * v + b * b - y * y) / (2 * v * b)))), w = o(x, m(f(p(x, this.forward)), C)), T = a(h(e.rest, w), e.upperRest), E = e.lower.spec, D = Math.min(E.limits[1], Math.max(E.limits[0], Math.PI - S - e.restBend));
542
+ r.setTarget(e.upper, a(i(u.rotation), T)), r.setTarget(e.lower, a(e.lower.rest, m(E.axis, D))), r.setTarget(e.foot, this.levelFoot(e, e.foot.body.rotation)), e.bend = D;
423
543
  }
424
544
  assist(e, t) {
425
- let n = this.ragdoll.bones[0].body, r = e / (1 + 16 * e + 64 * e * e), i = l(t, this.up) + this.height * this.stance, a = l(n.position, this.up), s = this.gravity * e * this.carry, u = Math.max(0, Math.min(r * (64 * (i - a) - 16 * l(n.linearVelocity, this.up)) + s, s * 1.5));
426
- if (this.carried = u / (this.gravity * e) * this.upperShare, u > 0) for (let e of this.upper) e.body.setLinearVelocity(o(e.body.linearVelocity, c(this.up, u)));
545
+ let n = this.ragdoll.bones[0].body, r = e / (1 + 16 * e + 64 * e * e), i = u(t, this.up) + this.height * this.stance, a = u(n.position, this.up), o = this.gravity * e * this.carry, c = Math.max(0, Math.min(r * (64 * (i - a) - 16 * u(n.linearVelocity, this.up)) + o, o * 1.5));
546
+ if (this.carried = c / (this.gravity * e) * this.upperShare, c > 0) for (let e of this.upper) e.body.setLinearVelocity(s(e.body.linearVelocity, l(this.up, c)));
427
547
  }
428
548
  fall() {
429
549
  this.state = "fallen", this.carried = 0;
@@ -437,7 +557,7 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
437
557
  }
438
558
  }
439
559
  reset() {
440
- if (this.disposed) throw new n("disposed", "This balance is disposed; call addRagdollBalance again.");
560
+ if (this.disposed) throw new r("disposed", "This balance is disposed; call addRagdollBalance again.");
441
561
  for (let e of this.legs) {
442
562
  let t = e.foot.body.position;
443
563
  e.plant = [
@@ -446,10 +566,10 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
446
566
  t[2]
447
567
  ], e.swing = null;
448
568
  }
449
- return this.floor = l(c(o(this.legs[0].plant, this.legs[1].plant), .5), this.up), this.hipsWere = null, this.state = "standing", this;
569
+ return this.floor = u(l(s(this.legs[0].plant, this.legs[1].plant), .5), this.up), this.hipsWere = null, this.state = "standing", this;
450
570
  }
451
571
  dispose() {
452
- if (!this.disposed && (this.disposed = !0, this.off(), this.fall(), this.ragdoll.bones[0].body.valid)) {
572
+ if (!this.disposed && (this.disposed = !0, this.off(), this.offForces(), this.fall(), this.ragdoll.bones[0].body.valid)) {
453
573
  this.wanted.fill(0), this.settle();
454
574
  for (let { part: e, shape: t } of this.soles) this.ragdoll.setShape(e, t);
455
575
  for (let e of this.ragdoll.bones) e.body.additionalPgsIterations = 0;
@@ -460,9 +580,9 @@ var h = 400, g = 12, _ = 6, v = 30, y = 50, b = .15, x = .9, S = .012, C = 16, w
460
580
  this.dispose();
461
581
  }
462
582
  };
463
- function N(e, t, n) {
464
- if (!e?.bones || typeof t?.setTarget != "function") throw TypeError("addRagdollBalance needs a Ragdoll and its motors: addRagdollBalance(ragdoll, motors, {feet}).");
465
- return new M(e, t, n);
583
+ function L(t, n, r) {
584
+ if (!t?.bones || typeof n?.setTarget != "function") throw e("addRagdollBalance needs a Ragdoll and its motors: addRagdollBalance(ragdoll, motors, {feet}).");
585
+ return new I(t, n, r);
466
586
  }
467
587
  //#endregion
468
- export { M as RagdollBalance, N as addRagdollBalance };
588
+ export { I as RagdollBalance, L as addRagdollBalance };