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