@margoengine/ragdoll 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,588 @@
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";
5
+ //#region src/balance.ts
6
+ var s = (e, t) => [
7
+ e[0] + t[0],
8
+ e[1] + t[1],
9
+ e[2] + t[2]
10
+ ], c = (e, t) => [
11
+ e[0] - t[0],
12
+ e[1] - t[1],
13
+ e[2] - t[2]
14
+ ], l = (e, t) => [
15
+ e[0] * t,
16
+ e[1] * t,
17
+ e[2] * 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) => [
19
+ e[1] * t[2] - e[2] * t[1],
20
+ e[2] * t[0] - e[0] * t[2],
21
+ e[0] * t[1] - e[1] * t[0]
22
+ ], m = (e, t) => {
23
+ let n = Math.sin(t / 2);
24
+ return [
25
+ e[0] * n,
26
+ e[1] * n,
27
+ e[2] * n,
28
+ Math.cos(t / 2)
29
+ ];
30
+ };
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);
33
+ return i < 1e-9 ? [
34
+ 1,
35
+ 0,
36
+ 0,
37
+ 0
38
+ ] : [
39
+ n[0] / i,
40
+ n[1] / i,
41
+ n[2] / i,
42
+ r / i
43
+ ];
44
+ }
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 = [
52
+ 0,
53
+ 0,
54
+ 0
55
+ ], this.carried = 0, this.up = [
56
+ 0,
57
+ 1,
58
+ 0
59
+ ], this.disposed = !1, this.settled = 0, this.stanceNow = 1, this.held = 0, this.primed = !1, this.soles = [], this.hipsWere = null, this.hipsSpeed = [
60
+ 0,
61
+ 0,
62
+ 0
63
+ ], this.hipsSpin = [
64
+ 0,
65
+ 0,
66
+ 0
67
+ ];
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;
72
+ };
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;
78
+ return {
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),
88
+ kneeCap: 0,
89
+ kneeInertia: 0,
90
+ bend: 0,
91
+ restBend: Math.acos(Math.max(-1, Math.min(1, u(f(c(p, l)), f(c(m, p)))))),
92
+ sole: {
93
+ front: 0,
94
+ back: 0,
95
+ left: 0,
96
+ right: 0
97
+ },
98
+ corners: [],
99
+ plant: [
100
+ m[0],
101
+ m[1],
102
+ m[2]
103
+ ],
104
+ swing: null,
105
+ pressed: 0,
106
+ load: 0
107
+ };
108
+ };
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 }) => [
111
+ e,
112
+ t,
113
+ n
114
+ ]));
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));
119
+ for (let e of this.legs) this.flatten(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
+ });
130
+ }
131
+ get tilt() {
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))));
134
+ }
135
+ step(e) {
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
+ }
141
+ }
142
+ readHips(e) {
143
+ let t = this.ragdoll.bones[0].body, n = [
144
+ t.position[0],
145
+ t.position[1],
146
+ t.position[2]
147
+ ], r = [...t.rotation], o = this.hipsWere;
148
+ if (o) {
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
154
+ ];
155
+ } else this.hipsSpeed = [
156
+ t.linearVelocity[0],
157
+ t.linearVelocity[1],
158
+ t.linearVelocity[2]
159
+ ], this.hipsSpin = [
160
+ t.angularVelocity[0],
161
+ t.angularVelocity[1],
162
+ t.angularVelocity[2]
163
+ ];
164
+ this.hipsWere = {
165
+ position: n,
166
+ rotation: r
167
+ };
168
+ }
169
+ settle() {
170
+ let { wanted: e, given: t } = this;
171
+ this.ragdoll.bones.forEach((n, r) => {
172
+ let i = r * 6, a = n.body;
173
+ if (a.type !== "dynamic") {
174
+ t.subarray(i, i + 6).some((e) => e !== 0) && a.resetForces(!1), t.fill(0, i, i + 6);
175
+ return;
176
+ }
177
+ let o = [
178
+ e[i] - t[i],
179
+ e[i + 1] - t[i + 1],
180
+ e[i + 2] - t[i + 2]
181
+ ], s = [
182
+ e[i + 3] - t[i + 3],
183
+ e[i + 4] - t[i + 4],
184
+ e[i + 5] - t[i + 5]
185
+ ];
186
+ (o[0] || o[1] || o[2]) && a.addForce(o), (s[0] || s[1] || s[2]) && a.addTorque(s), t.set(e.subarray(i, i + 6), i);
187
+ });
188
+ }
189
+ give(e, t, n) {
190
+ let r = this.ragdoll.bones.indexOf(e) * 6;
191
+ for (let e = 0; e < 3; e++) t && (this.wanted[r + e] += t[e]), n && (this.wanted[r + 3 + e] += n[e]);
192
+ }
193
+ control(e) {
194
+ if (this.state === "fallen" || this.ragdoll.bones[0].body.type !== "dynamic") return;
195
+ if (this.tilt > this.fallTilt) {
196
+ this.fall();
197
+ return;
198
+ }
199
+ for (let e of this.legs) if (!e.swing) {
200
+ let t = [
201
+ e.foot.body.position[0],
202
+ e.foot.body.position[1],
203
+ e.foot.body.position[2]
204
+ ];
205
+ e.plant = s(t, l(this.up, this.floor - u(t, this.up)));
206
+ }
207
+ this.capture = this.capturePoint();
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));
214
+ }
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)));
248
+ }
249
+ massCentre() {
250
+ let e = [
251
+ 0,
252
+ 0,
253
+ 0
254
+ ], t = [
255
+ 0,
256
+ 0,
257
+ 0
258
+ ];
259
+ return this.ragdoll.bones.forEach((n, r) => {
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;
262
+ }), {
263
+ position: l(e, 1 / this.total),
264
+ velocity: l(t, 1 / this.total)
265
+ };
266
+ }
267
+ holdJoints() {
268
+ let e = this.motors.strength;
269
+ for (let t of this.legs) {
270
+ if (this.motors.standing(t.foot, !t.swing), t.swing) {
271
+ let n = this.motors.limbInertia(t.foot);
272
+ t.foot.joint.setRotationMotor(this.swingFoot(t), {
273
+ model: "force",
274
+ stiffness: n * y * y * e,
275
+ damping: 2 * n * y * Math.sqrt(e),
276
+ maxForce: n * g * e
277
+ });
278
+ continue;
279
+ }
280
+ let n = Math.min(1, Math.abs(t.bend - this.kneeAngle(t)) / x) * e;
281
+ n > 0 ? t.lower.joint.setMotor({
282
+ model: "force",
283
+ targetPosition: t.bend,
284
+ targetVelocity: 0,
285
+ stiffness: t.kneeInertia * b * b * n,
286
+ damping: 0,
287
+ maxForce: t.kneeInertia * g * e
288
+ }) : t.lower.joint.clearMotor();
289
+ }
290
+ }
291
+ swingFoot(e) {
292
+ return this.levelFoot(e, this.ragdoll.bones[0].body.rotation, this.standing);
293
+ }
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));
297
+ }
298
+ kneeAngle(e) {
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]);
301
+ }
302
+ planted(e) {
303
+ return this.legs.some((t) => !t.swing && (e === t.upper || e === t.lower || e === t.foot));
304
+ }
305
+ soleOf(e) {
306
+ let t = e.foot, n = t.shape, r = p(this.up, this.forward), i = {
307
+ front: t.length / 2,
308
+ back: t.length / 2,
309
+ left: t.length / 4,
310
+ right: t.length / 4
311
+ };
312
+ if (n.type !== "hull") return i;
313
+ let a = [];
314
+ for (let e = 0; e + 2 < n.points.length; e += 3) a.push(o([
315
+ n.points[e],
316
+ n.points[e + 1],
317
+ n.points[e + 2]
318
+ ], t.body.rotation));
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));
320
+ return {
321
+ front: Math.max(...l),
322
+ back: -Math.min(...l),
323
+ left: Math.max(...d),
324
+ right: -Math.min(...d)
325
+ };
326
+ }
327
+ flatten(e) {
328
+ e.sole = this.soleOf(e);
329
+ let t = e.foot, n = t.shape;
330
+ if (n.type !== "hull") return;
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([
333
+ n.points[e],
334
+ n.points[e + 1],
335
+ n.points[e + 2]
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 = [
338
+ y(h, _),
339
+ y(h, -v),
340
+ y(-g, _),
341
+ y(-g, -v)
342
+ ];
343
+ this.soles.push({
344
+ part: t,
345
+ shape: n
346
+ }), e.corners = b.map((e) => o(e, a)), this.ragdoll.setShape(t, {
347
+ type: "hull",
348
+ points: [...b, ...m].flatMap((e) => [...o(e, a)])
349
+ });
350
+ }
351
+ carryWeight(e, t, n) {
352
+ if (!t.length) return;
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), [
354
+ 0,
355
+ 0,
356
+ 0
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 = [
358
+ 0,
359
+ 0,
360
+ 0
361
+ ];
362
+ for (let e of f) {
363
+ if (this.planted(e)) continue;
364
+ let t = A(e);
365
+ M += e.body.mass;
366
+ for (let n = 0; n < 3; n++) N[n] += t[n] * e.body.mass;
367
+ }
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) : [
369
+ 0,
370
+ 0,
371
+ 0
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) : [
374
+ 0,
375
+ 0,
376
+ 0
377
+ ], a = e.upper.body.position, o = e.lower.body.position, u = e.foot.body.position, d = A(e.upper), f = A(e.lower);
378
+ return {
379
+ leg: e,
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)),
382
+ foot: E * m * n
383
+ };
384
+ }), { turn: B, points: V, centre: H } = this.uprightTurn(e), U = [
385
+ 0,
386
+ 0,
387
+ 0
388
+ ];
389
+ this.upper.forEach((e, t) => {
390
+ let n = c(V[t], H);
391
+ U = s(U, p(n, l(p(B, n), e.body.mass)));
392
+ });
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));
403
+ }
404
+ }
405
+ uprightTurn(e) {
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 = [
407
+ 0,
408
+ 1,
409
+ 2
410
+ ].map((e) => p * (u * u * n[e] * l - f * m[e])), g = this.maxUpright * e, _ = d(h);
411
+ if (_ > g) for (let e = 0; e < 3; e++) h[e] *= g / _;
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 = [
413
+ 0,
414
+ 0,
415
+ 0
416
+ ];
417
+ return this.upper.forEach((e, t) => {
418
+ for (let n = 0; n < 3; n++) b[n] += y[t][n] * e.body.mass / this.upperMass;
419
+ }), {
420
+ turn: h,
421
+ points: y,
422
+ centre: b
423
+ };
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
+ }
439
+ capturePoint() {
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)));
442
+ }
443
+ outside(e, t, n = 0) {
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);
504
+ }
505
+ startStep() {
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;
507
+ l.swing = {
508
+ from: l.plant,
509
+ to: s ? n : r,
510
+ t: 0,
511
+ time: this.stepDuration(s ? t : e)
512
+ }, this.steps++;
513
+ }
514
+ stepDuration(e) {
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);
523
+ }
524
+ lifted(e) {
525
+ return u(e.foot.body.position, this.up) - this.floor > this.lift * k;
526
+ }
527
+ landing(e) {
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;
532
+ }
533
+ driveLeg(e, t, n) {
534
+ let r = this.motors, u = this.ragdoll.bones[0].body, g = e.plant;
535
+ if (e.swing) {
536
+ let t = e.swing;
537
+ t.t += n / t.time;
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);
540
+ }
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;
543
+ }
544
+ assist(e, t) {
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)));
547
+ }
548
+ fall() {
549
+ this.state = "fallen", this.carried = 0;
550
+ for (let e of this.legs) {
551
+ e.swing = null, this.motors.standing(e.foot, !1);
552
+ for (let t of [
553
+ e.upper,
554
+ e.lower,
555
+ e.foot
556
+ ]) this.motors.setTarget(t, null);
557
+ }
558
+ }
559
+ reset() {
560
+ if (this.disposed) throw new r("disposed", "This balance is disposed; call addRagdollBalance again.");
561
+ for (let e of this.legs) {
562
+ let t = e.foot.body.position;
563
+ e.plant = [
564
+ t[0],
565
+ t[1],
566
+ t[2]
567
+ ], e.swing = null;
568
+ }
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;
570
+ }
571
+ dispose() {
572
+ if (!this.disposed && (this.disposed = !0, this.off(), this.offForces(), this.fall(), this.ragdoll.bones[0].body.valid)) {
573
+ this.wanted.fill(0), this.settle();
574
+ for (let { part: e, shape: t } of this.soles) this.ragdoll.setShape(e, t);
575
+ for (let e of this.ragdoll.bones) e.body.additionalPgsIterations = 0;
576
+ for (let { foot: e, lower: t } of this.legs) for (let n of [e, t]) this.motors.addSteadying(n);
577
+ }
578
+ }
579
+ [Symbol.dispose ?? Symbol()]() {
580
+ this.dispose();
581
+ }
582
+ };
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);
586
+ }
587
+ //#endregion
588
+ export { I as RagdollBalance, L as addRagdollBalance };