@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/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +431 -381
- package/dist/balance.d.ts +57 -0
- package/dist/balance.js +272 -152
- package/dist/character.d.ts +200 -0
- package/dist/character.js +325 -0
- package/dist/errors-CDwcEs-j.js +5 -0
- package/dist/errors.d.ts +3 -0
- package/dist/getup.js +57 -56
- package/dist/hulls.js +85 -82
- package/dist/index.d.ts +1 -1
- package/dist/index.js +559 -302
- package/dist/powered.js +79 -78
- package/dist/profile.js +100 -99
- package/dist/ragdoll.d.ts +17 -1
- package/package.json +86 -82
package/dist/balance.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
|
6
|
+
var s = (e, t) => [
|
|
6
7
|
e[0] + t[0],
|
|
7
8
|
e[1] + t[1],
|
|
8
9
|
e[2] + t[2]
|
|
9
|
-
],
|
|
10
|
+
], c = (e, t) => [
|
|
10
11
|
e[0] - t[0],
|
|
11
12
|
e[1] - t[1],
|
|
12
13
|
e[2] - t[2]
|
|
13
|
-
],
|
|
14
|
+
], l = (e, t) => [
|
|
14
15
|
e[0] * t,
|
|
15
16
|
e[1] * t,
|
|
16
17
|
e[2] * t
|
|
17
|
-
],
|
|
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
|
-
],
|
|
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
|
|
31
|
-
let n =
|
|
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
|
|
45
|
-
let
|
|
46
|
-
if (!(
|
|
47
|
-
return
|
|
48
|
-
},
|
|
49
|
-
constructor(
|
|
50
|
-
this.ragdoll =
|
|
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
|
|
68
|
-
let
|
|
69
|
-
if (!
|
|
70
|
-
return
|
|
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(
|
|
73
|
-
let
|
|
74
|
-
let
|
|
75
|
-
if (!
|
|
76
|
-
let
|
|
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:
|
|
79
|
-
lower:
|
|
80
|
-
foot:
|
|
81
|
-
thigh:
|
|
82
|
-
shin:
|
|
83
|
-
rest:
|
|
84
|
-
upperRest: [...
|
|
85
|
-
footRest: [...
|
|
86
|
-
hip:
|
|
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,
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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 = [
|
|
106
|
-
let [
|
|
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
|
|
112
|
-
this.upper =
|
|
113
|
-
let
|
|
114
|
-
this.hipsOverFeet =
|
|
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 =
|
|
117
|
-
let
|
|
118
|
-
this.gravity =
|
|
119
|
-
let
|
|
120
|
-
for (let e of
|
|
121
|
-
for (let { foot: e, lower:
|
|
122
|
-
for (let e of this.legs) e.kneeCap = this.total * this.gravity * Math.min(e.thigh, e.shin), e.kneeInertia =
|
|
123
|
-
this.natural = Math.sqrt(this.gravity /
|
|
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 =
|
|
127
|
-
return Math.acos(Math.min(1, Math.max(-1,
|
|
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
|
|
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
|
-
],
|
|
147
|
+
], r = [...t.rotation], o = this.hipsWere;
|
|
138
148
|
if (o) {
|
|
139
|
-
let t =
|
|
140
|
-
this.hipsSpeed = c(
|
|
141
|
-
t[0] *
|
|
142
|
-
t[1] *
|
|
143
|
-
t[2] *
|
|
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:
|
|
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 =
|
|
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 =
|
|
199
|
-
!
|
|
200
|
-
let
|
|
201
|
-
if (
|
|
202
|
-
let e =
|
|
203
|
-
|
|
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 /
|
|
206
|
-
let
|
|
207
|
-
|
|
208
|
-
|
|
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,
|
|
222
|
-
for (let n = 0; n < 3; n++) e[n] +=
|
|
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:
|
|
225
|
-
velocity:
|
|
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 *
|
|
236
|
-
damping: 2 * n *
|
|
237
|
-
maxForce: n *
|
|
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)) /
|
|
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 *
|
|
285
|
+
stiffness: t.kneeInertia * b * b * n,
|
|
247
286
|
damping: 0,
|
|
248
|
-
maxForce: t.kneeInertia *
|
|
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(
|
|
256
|
-
let s =
|
|
257
|
-
return e
|
|
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(
|
|
261
|
-
return 2 * Math.atan2(
|
|
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 =
|
|
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
|
|
275
|
-
for (let e = 0; e + 2 < n.points.length; e += 3)
|
|
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(...
|
|
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(...
|
|
283
|
-
back: -Math.min(...
|
|
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
|
|
293
|
-
for (let e = 0; e + 2 < n.points.length; e += 3) d.push(
|
|
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
|
-
],
|
|
298
|
-
let
|
|
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) => [...
|
|
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(),
|
|
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.
|
|
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
|
|
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 =
|
|
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 =
|
|
334
|
-
let n = m === 2 ? t ? R : 1 - 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,
|
|
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:
|
|
342
|
-
atAnkle:
|
|
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 =
|
|
352
|
-
U =
|
|
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 =
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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 =
|
|
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 * (
|
|
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) =>
|
|
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 =
|
|
385
|
-
return
|
|
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 =
|
|
389
|
-
return
|
|
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),
|
|
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:
|
|
509
|
+
to: s ? n : r,
|
|
396
510
|
t: 0,
|
|
397
|
-
time: this.stepDuration(
|
|
511
|
+
time: this.stepDuration(s ? t : e)
|
|
398
512
|
}, this.steps++;
|
|
399
513
|
}
|
|
400
514
|
stepDuration(e) {
|
|
401
|
-
let t =
|
|
402
|
-
|
|
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
|
|
525
|
+
return u(e.foot.body.position, this.up) - this.floor > this.lift * k;
|
|
406
526
|
}
|
|
407
527
|
landing(e) {
|
|
408
|
-
let t =
|
|
409
|
-
|
|
410
|
-
let
|
|
411
|
-
return
|
|
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
|
|
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 =
|
|
419
|
-
g =
|
|
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 _ =
|
|
422
|
-
|
|
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 =
|
|
426
|
-
if (this.carried =
|
|
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
|
|
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(
|
|
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
|
|
464
|
-
if (!
|
|
465
|
-
return new
|
|
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 {
|
|
588
|
+
export { I as RagdollBalance, L as addRagdollBalance };
|