@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/index.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { t as e } from "./errors-CDwcEs-j.js";
|
|
5
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
6
|
+
import { conjugateQuaternion as n, normalizeQuaternion as r, rotateByQuaternion as i, rotationToQuaternion as a } from "@margoengine/math/quat";
|
|
6
7
|
//#region ../shared/scene-scope.js
|
|
7
|
-
var
|
|
8
|
-
let n = t +
|
|
8
|
+
var o = 0, s = (e, t) => {
|
|
9
|
+
let n = t + o++;
|
|
9
10
|
for (; e.plugins?.has(n);) n += ":";
|
|
10
11
|
return n;
|
|
11
12
|
};
|
|
12
|
-
function
|
|
13
|
+
function c(e, t) {
|
|
13
14
|
return {
|
|
14
15
|
scene: t,
|
|
15
16
|
run(n) {
|
|
16
17
|
if (!t) return n();
|
|
17
|
-
let r, i = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("
|
|
18
|
-
name:
|
|
18
|
+
let r, i = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
19
|
+
name: s(e, "scene-scope:"),
|
|
19
20
|
setup() {
|
|
20
21
|
r = n();
|
|
21
22
|
}
|
|
@@ -26,28 +27,28 @@ function s(e, t) {
|
|
|
26
27
|
}
|
|
27
28
|
//#endregion
|
|
28
29
|
//#region src/ragdoll.ts
|
|
29
|
-
var
|
|
30
|
+
var l = 2e-6, u = [
|
|
30
31
|
0,
|
|
31
32
|
0,
|
|
32
33
|
0,
|
|
33
34
|
1
|
|
34
|
-
],
|
|
35
|
+
], d = {
|
|
35
36
|
type: "cone",
|
|
36
37
|
swing: Math.PI / 4,
|
|
37
38
|
twist: [-Math.PI / 6, Math.PI / 6]
|
|
38
|
-
},
|
|
39
|
+
}, f = (e) => e[0] * (e[5] * e[10] - e[6] * e[9]) - e[4] * (e[1] * e[10] - e[2] * e[9]) + e[8] * (e[1] * e[6] - e[2] * e[5]), p = (e, t) => r(a(e, t, Math.hypot(e[0], e[1], e[2]), Math.hypot(e[4], e[5], e[6]), Math.hypot(e[8], e[9], e[10])), t), m = (e, t, n, r = 0) => {
|
|
39
40
|
let i = e[0], a = e[1], o = e[2], s = e[3], c = t[0], l = t[1], u = t[2], d = t[3];
|
|
40
41
|
return n[r] = s * c + i * d + a * u - o * l, n[r + 1] = s * l - i * u + a * d + o * c, n[r + 2] = s * u + i * l - a * c + o * d, n[r + 3] = s * d - i * c - a * l - o * u, n;
|
|
41
42
|
};
|
|
42
|
-
function
|
|
43
|
+
function h(e, t, n) {
|
|
43
44
|
let i = e[0] * t[0] + e[1] * t[1] + e[2] * t[2];
|
|
44
45
|
if (i < -.999999) {
|
|
45
|
-
let t = 0,
|
|
46
|
-
Math.hypot(
|
|
47
|
-
} else
|
|
48
|
-
return n
|
|
46
|
+
let t = 0, r = -e[2], i = e[1];
|
|
47
|
+
Math.hypot(r, i) < 1e-6 && (t = e[2], r = 0, i = -e[0]), n[0] = t, n[1] = r, n[2] = i, n[3] = 0;
|
|
48
|
+
} else n[0] = e[1] * t[2] - e[2] * t[1], n[1] = e[2] * t[0] - e[0] * t[2], n[2] = e[0] * t[1] - e[1] * t[0], n[3] = 1 + i;
|
|
49
|
+
return r(n, n);
|
|
49
50
|
}
|
|
50
|
-
function
|
|
51
|
+
function g(e, t, n, r, i, a, o) {
|
|
51
52
|
let s = e[t] * n[r] + e[t + 1] * n[r + 1] + e[t + 2] * n[r + 2] + e[t + 3] * n[r + 3], c = 1;
|
|
52
53
|
s < 0 && (c = -1, s = -s);
|
|
53
54
|
let l = 1 - i, u = i * c;
|
|
@@ -63,7 +64,7 @@ function h(e, t, n, r, i, a, o) {
|
|
|
63
64
|
d = Math.sqrt(d);
|
|
64
65
|
for (let e = 0; e < 4; e++) a[o + e] /= d;
|
|
65
66
|
}
|
|
66
|
-
function
|
|
67
|
+
function _(e) {
|
|
67
68
|
let t = e.store.parents, n = /* @__PURE__ */ new Map();
|
|
68
69
|
for (let r of e.meshes.values()) {
|
|
69
70
|
let e = t[r.id];
|
|
@@ -73,8 +74,8 @@ function g(e) {
|
|
|
73
74
|
}
|
|
74
75
|
return n;
|
|
75
76
|
}
|
|
76
|
-
function
|
|
77
|
-
let a = n, o = r, s =
|
|
77
|
+
function v(e, t, n, r, i) {
|
|
78
|
+
let a = n, o = r, s = y(e);
|
|
78
79
|
if (s) {
|
|
79
80
|
let e = Infinity, n = -Infinity, r = 0, i = s.length / 3, c = [
|
|
80
81
|
0,
|
|
@@ -93,206 +94,206 @@ function _(e, t, n, r, i) {
|
|
|
93
94
|
}
|
|
94
95
|
return [2 / (i * o * o), 12 / (i * (3 * o * o + a * a))];
|
|
95
96
|
}
|
|
96
|
-
function
|
|
97
|
+
function y(e) {
|
|
97
98
|
if (e.type === "hull") return e.points;
|
|
98
99
|
if (e.type === "capsule") return null;
|
|
99
100
|
let t = /* @__PURE__ */ new Float64Array(24);
|
|
100
101
|
for (let n = 0; n < 8; n++) {
|
|
101
|
-
let
|
|
102
|
+
let r = i([
|
|
102
103
|
0,
|
|
103
104
|
1,
|
|
104
105
|
2
|
|
105
106
|
].map((t) => e.halfExtents[t] * (n >> t & 1 ? 1 : -1)), e.rotation);
|
|
106
|
-
for (let
|
|
107
|
+
for (let i = 0; i < 3; i++) t[n * 3 + i] = r[i] + e.center[i];
|
|
107
108
|
}
|
|
108
109
|
return t;
|
|
109
110
|
}
|
|
110
|
-
function
|
|
111
|
-
let
|
|
112
|
-
if (
|
|
113
|
-
if (
|
|
114
|
-
if (!
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
if (
|
|
118
|
-
if (!(
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
throw
|
|
111
|
+
function b(t) {
|
|
112
|
+
let n = (e) => Array.prototype.every.call(e, Number.isFinite);
|
|
113
|
+
if (t?.type === "capsule") return t;
|
|
114
|
+
if (t?.type === "hull") {
|
|
115
|
+
if (!t.points || t.points.length < 12 || t.points.length % 3 || !n(t.points)) throw e("A ragdoll hull needs at least four finite xyz points; pass points that span a volume.");
|
|
116
|
+
return t;
|
|
117
|
+
}
|
|
118
|
+
if (t?.type === "box") {
|
|
119
|
+
if (!(t.halfExtents?.length === 3 && n(t.halfExtents) && t.halfExtents.every((e) => e > 0)) || !(t.center?.length === 3 && n(t.center)) || !(t.rotation?.length === 4 && n(t.rotation)) || Math.abs(Math.hypot(...t.rotation) - 1) > .001) throw e("A ragdoll box needs positive halfExtents, a finite center and a unit rotation; pass {center, halfExtents, rotation}.");
|
|
120
|
+
return t;
|
|
121
|
+
}
|
|
122
|
+
throw e("A ragdoll shape must be of type 'capsule', 'hull' or 'box'.");
|
|
122
123
|
}
|
|
123
|
-
var
|
|
124
|
-
if (typeof
|
|
125
|
-
return
|
|
126
|
-
},
|
|
127
|
-
constructor(
|
|
128
|
-
if (this.physics =
|
|
129
|
-
if (!
|
|
130
|
-
let
|
|
131
|
-
if (!(
|
|
132
|
-
if (
|
|
133
|
-
let
|
|
134
|
-
this.engine =
|
|
135
|
-
let
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
if (
|
|
139
|
-
|
|
124
|
+
var x = (t, n, r) => {
|
|
125
|
+
if (typeof t != "number" || !(t > 0) || t === Infinity) throw e(`Ragdoll ${n} must be a positive finite number; ${r}`);
|
|
126
|
+
return t;
|
|
127
|
+
}, S = class r {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
128
|
+
constructor(t, r) {
|
|
129
|
+
if (this.physics = t, this.unsubscribe = [], this.loose = 0, this.stepped = 0, this.disposed = !1, this.q = /* @__PURE__ */ new Float64Array(4), this.r = /* @__PURE__ */ new Float64Array(4), this.s = /* @__PURE__ */ new Float64Array(4), this.v = /* @__PURE__ */ new Float64Array(3), this.w = /* @__PURE__ */ new Float64Array(3), this.c = /* @__PURE__ */ new Float64Array(3), this.pose = /* @__PURE__ */ new Float64Array(7), this.turnBy = /* @__PURE__ */ new Float64Array(4), this.turned = /* @__PURE__ */ new Float64Array(4), this.omega = /* @__PURE__ */ new Float64Array(), typeof t?.addBody != "function" || t.isDisposed) throw e("A ragdoll needs a live Physics world: new Ragdoll(physics, {root: hips}).");
|
|
130
|
+
if (!r || r.root === void 0 == (r.bones === void 0)) throw e("Ragdoll options need exactly one of root (the hips node) or bones (a list of nodes).");
|
|
131
|
+
let a = r.minLength ?? .05, o = r.thickness ?? .2, s = r.density ?? 1e3;
|
|
132
|
+
if (!(a >= 0) || a === Infinity) throw e("Ragdoll minLength must be a finite number of at least 0; pass metres, e.g. 0.05.");
|
|
133
|
+
if (x(o, "thickness", "pass a fraction of the bone length, e.g. 0.2."), x(s, "density", "pass kilograms per cubic metre, e.g. 1000."), this.stiffness = r.limitStiffness ?? .2, !(this.stiffness >= 0 && this.stiffness <= 1)) throw e("Ragdoll limitStiffness must be from 0 to 1; pass the share of the error removed per step.");
|
|
134
|
+
let c = this.boneList(r, a), l = c[0].node.engine;
|
|
135
|
+
this.engine = l, this.name = "ragdoll:" + c[0].node.id;
|
|
136
|
+
let u = l.store, h = u.parents, g = /* @__PURE__ */ new Map();
|
|
137
|
+
c.forEach((t, n) => {
|
|
138
|
+
if (t.node.engine !== l) throw e("Ragdoll bones must all belong to one engine; pass nodes of one skeleton.");
|
|
139
|
+
if (g.has(t.node.id)) throw e("Ragdoll bones must be distinct; list each node once.");
|
|
140
|
+
g.set(t.node.id, n);
|
|
140
141
|
});
|
|
141
|
-
let
|
|
142
|
-
let t =
|
|
143
|
-
return t !== -1 &&
|
|
142
|
+
let y = c.map((e) => {
|
|
143
|
+
let t = h[e.node.id];
|
|
144
|
+
return t !== -1 && g.has(t) ? g.get(t) : -1;
|
|
144
145
|
});
|
|
145
|
-
if (
|
|
146
|
-
let
|
|
147
|
-
|
|
146
|
+
if (y.filter((e) => e === -1).length !== 1) throw e("Ragdoll bones must form one tree; list every node between the root bone and each other bone (each bone's parent node is a bone).");
|
|
147
|
+
let S = [], C = (e) => {
|
|
148
|
+
S.push(e), c.forEach((t, n) => y[n] === e && C(n));
|
|
148
149
|
};
|
|
149
|
-
|
|
150
|
-
let
|
|
151
|
-
let
|
|
152
|
-
if (!(
|
|
153
|
-
return
|
|
154
|
-
},
|
|
155
|
-
if (!Number.isInteger(
|
|
150
|
+
C(y.indexOf(-1)), u.tick(!1);
|
|
151
|
+
let w = _(l), T = (t, n) => {
|
|
152
|
+
let r = t.matrix;
|
|
153
|
+
if (!(f(r) > 0)) throw e("Ragdoll bones need positive world scales; remove zero or mirrored bone scales.");
|
|
154
|
+
return n[0] = r[12], n[1] = r[13], n[2] = r[14], n.set(p(r, this.q), 3), n;
|
|
155
|
+
}, E = [], D = [], O = /* @__PURE__ */ new Map(), k = r.collisionGroups ?? 2147516415;
|
|
156
|
+
if (!Number.isInteger(k) || k < 0 || k > 4294967295) throw e("Ragdoll collisionGroups must be a packed 32-bit integer; build it with collisionGroups().");
|
|
156
157
|
this.settings = {
|
|
157
|
-
collisionGroups:
|
|
158
|
-
friction:
|
|
159
|
-
linearDamping:
|
|
160
|
-
angularDamping:
|
|
158
|
+
collisionGroups: k,
|
|
159
|
+
friction: r.friction ?? .6,
|
|
160
|
+
linearDamping: r.linearDamping ?? .05,
|
|
161
|
+
angularDamping: r.angularDamping ?? .8,
|
|
161
162
|
limitStiffness: this.stiffness
|
|
162
163
|
};
|
|
163
164
|
try {
|
|
164
|
-
for (let
|
|
165
|
-
let
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
],
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
],
|
|
165
|
+
for (let a of S) {
|
|
166
|
+
let l = c[a], u = l.node, f = T(u, /* @__PURE__ */ new Float64Array(7)), p = [
|
|
167
|
+
f[0],
|
|
168
|
+
f[1],
|
|
169
|
+
f[2]
|
|
170
|
+
], h = [
|
|
171
|
+
f[3],
|
|
172
|
+
f[4],
|
|
173
|
+
f[5],
|
|
174
|
+
f[6]
|
|
175
|
+
], g = n(h), _ = (w.get(u.id) ?? []).map((e) => [
|
|
175
176
|
e.matrix[12],
|
|
176
177
|
e.matrix[13],
|
|
177
178
|
e.matrix[14]
|
|
178
|
-
]),
|
|
179
|
+
]), S = [
|
|
179
180
|
0,
|
|
180
181
|
1,
|
|
181
182
|
0
|
|
182
|
-
],
|
|
183
|
-
if (
|
|
183
|
+
], C = 0, k = 0;
|
|
184
|
+
if (_.length) {
|
|
184
185
|
let e = [
|
|
185
186
|
0,
|
|
186
187
|
1,
|
|
187
188
|
2
|
|
188
|
-
].map((e) =>
|
|
189
|
-
if (n < .001) for (let e of
|
|
189
|
+
].map((e) => _.reduce((t, n) => t + n[e], 0) / _.length - p[e]), t = e, n = Math.hypot(e[0], e[1], e[2]);
|
|
190
|
+
if (n < .001) for (let e of _) {
|
|
190
191
|
let r = [
|
|
191
|
-
e[0] -
|
|
192
|
-
e[1] -
|
|
193
|
-
e[2] -
|
|
192
|
+
e[0] - p[0],
|
|
193
|
+
e[1] - p[1],
|
|
194
|
+
e[2] - p[2]
|
|
194
195
|
], i = Math.hypot(r[0], r[1], r[2]);
|
|
195
196
|
i > n && (n = i, t = r);
|
|
196
197
|
}
|
|
197
198
|
if (n > 1e-6) {
|
|
198
|
-
|
|
199
|
-
for (let e of
|
|
199
|
+
S = i(t.map((e) => e / n), g), C = n;
|
|
200
|
+
for (let e of _) {
|
|
200
201
|
let r = [
|
|
201
|
-
e[0] -
|
|
202
|
-
e[1] -
|
|
203
|
-
e[2] -
|
|
202
|
+
e[0] - p[0],
|
|
203
|
+
e[1] - p[1],
|
|
204
|
+
e[2] - p[2]
|
|
204
205
|
], i = (r[0] * t[0] + r[1] * t[1] + r[2] * t[2]) / n;
|
|
205
|
-
|
|
206
|
+
k = Math.max(k, Math.hypot(r[0] - t[0] / n * i, r[1] - t[1] / n * i, r[2] - t[2] / n * i));
|
|
206
207
|
}
|
|
207
208
|
}
|
|
208
209
|
}
|
|
209
|
-
let
|
|
210
|
-
if (!(
|
|
211
|
-
let
|
|
212
|
-
type:
|
|
213
|
-
position:
|
|
214
|
-
rotation:
|
|
210
|
+
let A = l.length === void 0 ? C : x(l.length, "bone length", "pass metres, or leave it out for a bone with children.");
|
|
211
|
+
if (!(A > 0)) throw e(`Ragdoll bone "${u.name || u.id}" has no children to measure; give it a length in metres.`);
|
|
212
|
+
let j = l.radius === void 0 ? Math.max(A * o, k / 2) : x(l.radius, "bone radius", "pass metres, or leave it out for thickness times the length."), M = Math.PI * j * j * (2 * Math.max(A / 2 - j, .001) + 4 / 3 * j), N = l.mass === void 0 ? M * s : x(l.mass, "bone mass", "pass kilograms, or leave it out for the capsule volume times density."), P = Float64Array.from(S), F = b(l.shape ?? { type: "capsule" }), I = t.addBody(null, {
|
|
213
|
+
type: r.active ? "dynamic" : "kinematicPosition",
|
|
214
|
+
position: p,
|
|
215
|
+
rotation: h,
|
|
215
216
|
linearDamping: this.settings.linearDamping,
|
|
216
217
|
angularDamping: this.settings.angularDamping,
|
|
217
218
|
userData: {
|
|
218
219
|
ragdoll: this,
|
|
219
|
-
node:
|
|
220
|
+
node: u
|
|
220
221
|
},
|
|
221
|
-
colliders: this.collider(
|
|
222
|
+
colliders: this.collider(F, P, A, j, N)
|
|
222
223
|
});
|
|
223
|
-
|
|
224
|
-
let
|
|
224
|
+
D.push(I);
|
|
225
|
+
let L = y[a] === -1 ? -1 : O.get(y[a]), R = new Float64Array([
|
|
225
226
|
0,
|
|
226
227
|
0,
|
|
227
228
|
0,
|
|
228
229
|
1
|
|
229
|
-
]),
|
|
230
|
-
if (
|
|
231
|
-
let e =
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
],
|
|
236
|
-
|
|
237
|
-
let o =
|
|
238
|
-
({joint:
|
|
230
|
+
]), z = null, B = null, V = null;
|
|
231
|
+
if (L !== -1) {
|
|
232
|
+
let e = E[L], t = n(e.world.subarray(3, 7)), a = i([
|
|
233
|
+
p[0] - e.world[0],
|
|
234
|
+
p[1] - e.world[1],
|
|
235
|
+
p[2] - e.world[2]
|
|
236
|
+
], t);
|
|
237
|
+
m(t, h, R);
|
|
238
|
+
let o = l.joint ?? r.joint ?? d;
|
|
239
|
+
({joint: z, cone: B, spec: V} = this.join(e.body, I, o, a, R));
|
|
239
240
|
}
|
|
240
|
-
let
|
|
241
|
-
node:
|
|
242
|
-
body:
|
|
243
|
-
joint:
|
|
244
|
-
parent:
|
|
245
|
-
length:
|
|
246
|
-
radius:
|
|
247
|
-
mass:
|
|
248
|
-
shape:
|
|
249
|
-
axis:
|
|
250
|
-
rest:
|
|
241
|
+
let H = {
|
|
242
|
+
node: u,
|
|
243
|
+
body: I,
|
|
244
|
+
joint: z,
|
|
245
|
+
parent: L,
|
|
246
|
+
length: A,
|
|
247
|
+
radius: j,
|
|
248
|
+
mass: N,
|
|
249
|
+
shape: F,
|
|
250
|
+
axis: P,
|
|
251
|
+
rest: R,
|
|
251
252
|
bind: new Float64Array([
|
|
252
253
|
0,
|
|
253
254
|
0,
|
|
254
255
|
0,
|
|
255
256
|
1
|
|
256
257
|
]),
|
|
257
|
-
spec:
|
|
258
|
-
cone:
|
|
259
|
-
inertia: Float64Array.from(
|
|
260
|
-
prev:
|
|
261
|
-
cur:
|
|
258
|
+
spec: V,
|
|
259
|
+
cone: B,
|
|
260
|
+
inertia: Float64Array.from(v(F, P, A, j, N)),
|
|
261
|
+
prev: f.slice(),
|
|
262
|
+
cur: f.slice(),
|
|
262
263
|
animated: /* @__PURE__ */ new Float64Array(7),
|
|
263
264
|
written: (/* @__PURE__ */ new Float64Array(7)).fill(NaN),
|
|
264
265
|
local: /* @__PURE__ */ new Float64Array(7),
|
|
265
|
-
world:
|
|
266
|
+
world: f,
|
|
266
267
|
target: [
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
268
|
+
f[0],
|
|
269
|
+
f[1],
|
|
270
|
+
f[2],
|
|
271
|
+
f[3],
|
|
272
|
+
f[4],
|
|
273
|
+
f[5],
|
|
274
|
+
f[6]
|
|
274
275
|
],
|
|
275
|
-
limp: !!
|
|
276
|
-
weight: +!!
|
|
276
|
+
limp: !!r.active,
|
|
277
|
+
weight: +!!r.active,
|
|
277
278
|
fade: 0,
|
|
278
279
|
fadeFrom: 0,
|
|
279
280
|
fadeAt: 0,
|
|
280
281
|
landed: !1,
|
|
281
|
-
jump: !
|
|
282
|
+
jump: !r.active
|
|
282
283
|
};
|
|
283
|
-
this.readLocal(
|
|
284
|
+
this.readLocal(u, H.animated), H.local.set(H.animated), this.fit(H, E), O.set(a, E.length), E.push(H);
|
|
284
285
|
}
|
|
285
286
|
} catch (e) {
|
|
286
|
-
for (let e of
|
|
287
|
-
for (let e of
|
|
287
|
+
for (let e of E) e.joint?.remove();
|
|
288
|
+
for (let e of D) e.remove();
|
|
288
289
|
throw e;
|
|
289
290
|
}
|
|
290
|
-
this.parts =
|
|
291
|
+
this.parts = E, this.bones = E, this.omega = new Float64Array(E.length * 3), this.loose = r.active ? E.length : 0, this.unsubscribe.push(t.on("beforestep", () => this.beforeStep()), t.on("afterstep", () => this.afterStep()));
|
|
291
292
|
}
|
|
292
|
-
static attach(
|
|
293
|
-
let i = new
|
|
294
|
-
return
|
|
295
|
-
if (a.hasPlugin(i.name)) throw i.dispose(), new
|
|
293
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
294
|
+
let i = new r(e, n), a = i.engine;
|
|
295
|
+
return c(a, a.sceneOf(e)).run(() => {
|
|
296
|
+
if (a.hasPlugin(i.name)) throw i.dispose(), new t("attached", "This skeleton already has an attached ragdoll; dispose it first, or use the one you have.");
|
|
296
297
|
try {
|
|
297
298
|
a.use(i);
|
|
298
299
|
} catch (e) {
|
|
@@ -326,7 +327,7 @@ var b = (e, t, n) => {
|
|
|
326
327
|
t[1] * (n / 2),
|
|
327
328
|
t[2] * (n / 2)
|
|
328
329
|
],
|
|
329
|
-
rotation: [...
|
|
330
|
+
rotation: [...h([
|
|
330
331
|
0,
|
|
331
332
|
1,
|
|
332
333
|
0
|
|
@@ -334,67 +335,67 @@ var b = (e, t, n) => {
|
|
|
334
335
|
...s
|
|
335
336
|
};
|
|
336
337
|
}
|
|
337
|
-
setShape(
|
|
338
|
+
setShape(t, n) {
|
|
338
339
|
this.live();
|
|
339
|
-
let
|
|
340
|
-
if (!
|
|
341
|
-
|
|
342
|
-
let
|
|
343
|
-
|
|
344
|
-
for (let e of
|
|
345
|
-
return
|
|
346
|
-
}
|
|
347
|
-
boneList(
|
|
348
|
-
if (
|
|
349
|
-
if (!Array.isArray(
|
|
350
|
-
return
|
|
351
|
-
let
|
|
352
|
-
if (!
|
|
353
|
-
return
|
|
340
|
+
let r = t && "body" in t ? t.node : t, i = this.parts.find((e) => e.node === r);
|
|
341
|
+
if (!i) throw e("That node is not a bone of this ragdoll; pass a node or part of ragdoll.bones.");
|
|
342
|
+
b(n);
|
|
343
|
+
let a = [...i.body.colliders];
|
|
344
|
+
i.body.addCollider(this.collider(n, i.axis, i.length, i.radius, i.mass));
|
|
345
|
+
for (let e of a) i.body.removeCollider(e);
|
|
346
|
+
return i.shape = n, i.inertia.set(v(n, i.axis, i.length, i.radius, i.mass)), this;
|
|
347
|
+
}
|
|
348
|
+
boneList(t, n) {
|
|
349
|
+
if (t.bones !== void 0) {
|
|
350
|
+
if (!Array.isArray(t.bones) || !t.bones.length) throw e("Ragdoll bones must be a non-empty array of nodes or {node} objects.");
|
|
351
|
+
return t.bones.map((t) => {
|
|
352
|
+
let n = t && "node" in t ? t : { node: t };
|
|
353
|
+
if (!n.node || typeof n.node.setQuaternion != "function" || !n.node.engine) throw e("Ragdoll bones must be skeleton nodes (meshes) or {node} objects; pass model.nodes[i].");
|
|
354
|
+
return n.node.check(), n;
|
|
354
355
|
});
|
|
355
356
|
}
|
|
356
|
-
let
|
|
357
|
-
if (typeof
|
|
358
|
-
|
|
359
|
-
let
|
|
360
|
-
let
|
|
361
|
-
if (!
|
|
362
|
-
let
|
|
363
|
-
if (!(Math.max(...
|
|
364
|
-
|
|
365
|
-
for (let e of
|
|
357
|
+
let r = t.root;
|
|
358
|
+
if (typeof r?.setQuaternion != "function" || !r.engine) throw e("Ragdoll root must be a skeleton node (a mesh), e.g. the hips: model.nodes[i].");
|
|
359
|
+
r.check(), r.engine.store.tick(!1);
|
|
360
|
+
let i = _(r.engine), a = [], o = (e) => {
|
|
361
|
+
let t = i.get(e.id);
|
|
362
|
+
if (!t?.length) return;
|
|
363
|
+
let r = e.matrix;
|
|
364
|
+
if (!(Math.max(...t.map((e) => Math.hypot(e.matrix[12] - r[12], e.matrix[13] - r[13], e.matrix[14] - r[14]))) < n && a.length)) {
|
|
365
|
+
a.push({ node: e });
|
|
366
|
+
for (let e of t) o(e);
|
|
366
367
|
}
|
|
367
368
|
};
|
|
368
|
-
if (
|
|
369
|
-
return
|
|
370
|
-
}
|
|
371
|
-
join(
|
|
372
|
-
let
|
|
373
|
-
if (
|
|
374
|
-
let
|
|
375
|
-
if (
|
|
376
|
-
let
|
|
377
|
-
if (!(
|
|
378
|
-
|
|
379
|
-
swing:
|
|
380
|
-
min:
|
|
381
|
-
max:
|
|
369
|
+
if (o(r), !a.length) throw e("Ragdoll root has no child bones; pass the hips of a skeleton, or list bones with lengths.");
|
|
370
|
+
return a;
|
|
371
|
+
}
|
|
372
|
+
join(t, n, r, i, a) {
|
|
373
|
+
let o = this.physics, s = i;
|
|
374
|
+
if (r?.type === "free" || r?.type === "cone") {
|
|
375
|
+
let i = null;
|
|
376
|
+
if (r.type === "cone") {
|
|
377
|
+
let t = r.swing ?? Math.PI / 4, [n, a] = r.twist ?? [-Math.PI / 6, Math.PI / 6];
|
|
378
|
+
if (!(t >= 0 && t <= Math.PI) || !(n <= a && n >= -Math.PI && a <= Math.PI)) throw e("Ragdoll cone limits need a swing from 0 to π and a twist [min, max] within -π to π; pass radians.");
|
|
379
|
+
i = {
|
|
380
|
+
swing: t,
|
|
381
|
+
min: n,
|
|
382
|
+
max: a
|
|
382
383
|
};
|
|
383
384
|
}
|
|
384
|
-
if (!
|
|
385
|
-
joint:
|
|
385
|
+
if (!i) return {
|
|
386
|
+
joint: o.addJoint(t, n, {
|
|
386
387
|
type: "spherical",
|
|
387
|
-
anchorA:
|
|
388
|
+
anchorA: s,
|
|
388
389
|
anchorB: [
|
|
389
390
|
0,
|
|
390
391
|
0,
|
|
391
392
|
0
|
|
392
393
|
]
|
|
393
394
|
}),
|
|
394
|
-
cone:
|
|
395
|
+
cone: i,
|
|
395
396
|
spec: { type: "free" }
|
|
396
397
|
};
|
|
397
|
-
let
|
|
398
|
+
let c = h([
|
|
398
399
|
1,
|
|
399
400
|
0,
|
|
400
401
|
0
|
|
@@ -402,80 +403,80 @@ var b = (e, t, n) => {
|
|
|
402
403
|
0,
|
|
403
404
|
1,
|
|
404
405
|
0
|
|
405
|
-
], /* @__PURE__ */ new Float64Array(4)),
|
|
406
|
+
], /* @__PURE__ */ new Float64Array(4)), l = m(a, c, /* @__PURE__ */ new Float64Array(4));
|
|
406
407
|
return {
|
|
407
|
-
joint:
|
|
408
|
+
joint: o.addJoint(t, n, {
|
|
408
409
|
type: "cone",
|
|
409
|
-
anchorA:
|
|
410
|
+
anchorA: s,
|
|
410
411
|
anchorB: [
|
|
411
412
|
0,
|
|
412
413
|
0,
|
|
413
414
|
0
|
|
414
415
|
],
|
|
415
416
|
frameA: [
|
|
417
|
+
l[0],
|
|
418
|
+
l[1],
|
|
419
|
+
l[2],
|
|
420
|
+
l[3]
|
|
421
|
+
],
|
|
422
|
+
frameB: [
|
|
416
423
|
c[0],
|
|
417
424
|
c[1],
|
|
418
425
|
c[2],
|
|
419
426
|
c[3]
|
|
420
427
|
],
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
s[1],
|
|
424
|
-
s[2],
|
|
425
|
-
s[3]
|
|
426
|
-
],
|
|
427
|
-
swing: r.swing,
|
|
428
|
-
twist: [r.min, r.max]
|
|
428
|
+
swing: i.swing,
|
|
429
|
+
twist: [i.min, i.max]
|
|
429
430
|
}),
|
|
430
431
|
cone: null,
|
|
431
432
|
spec: {
|
|
432
433
|
type: "cone",
|
|
433
|
-
swing:
|
|
434
|
-
twist: [
|
|
434
|
+
swing: i.swing,
|
|
435
|
+
twist: [i.min, i.max]
|
|
435
436
|
}
|
|
436
437
|
};
|
|
437
438
|
}
|
|
438
|
-
if (
|
|
439
|
-
let
|
|
440
|
-
if (!(
|
|
441
|
-
let [
|
|
442
|
-
if (!(
|
|
443
|
-
let
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
],
|
|
439
|
+
if (r?.type !== "hinge") throw e("Unknown ragdoll joint type; use 'cone', 'hinge' or 'free'.");
|
|
440
|
+
let c = r.axis, l = Array.isArray(c) && c.length === 3 ? Math.hypot(c[0], c[1], c[2]) : NaN;
|
|
441
|
+
if (!(l > 0) || l === Infinity) throw e("Ragdoll hinge axis must be a nonzero finite [x, y, z] in the bone frame; e.g. [1, 0, 0].");
|
|
442
|
+
let [u, d] = r.limits ?? [-Math.PI * .75, 0];
|
|
443
|
+
if (!(u <= d && u >= -Math.PI && d <= Math.PI)) throw e("Ragdoll hinge limits need [min, max] within -π to π; pass radians.");
|
|
444
|
+
let f = [
|
|
445
|
+
c[0] / l,
|
|
446
|
+
c[1] / l,
|
|
447
|
+
c[2] / l
|
|
448
|
+
], p = h([
|
|
448
449
|
1,
|
|
449
450
|
0,
|
|
450
451
|
0
|
|
451
|
-
],
|
|
452
|
+
], f, /* @__PURE__ */ new Float64Array(4)), g = m(a, p, /* @__PURE__ */ new Float64Array(4)), _ = (e) => [
|
|
452
453
|
e[0],
|
|
453
454
|
e[1],
|
|
454
455
|
e[2],
|
|
455
456
|
e[3]
|
|
456
457
|
];
|
|
457
458
|
return {
|
|
458
|
-
joint:
|
|
459
|
+
joint: o.addJoint(t, n, {
|
|
459
460
|
type: "revolute",
|
|
460
|
-
frameA: g
|
|
461
|
-
frameB:
|
|
462
|
-
anchorA:
|
|
461
|
+
frameA: _(g),
|
|
462
|
+
frameB: _(p),
|
|
463
|
+
anchorA: s,
|
|
463
464
|
anchorB: [
|
|
464
465
|
0,
|
|
465
466
|
0,
|
|
466
467
|
0
|
|
467
468
|
],
|
|
468
|
-
limits: [
|
|
469
|
+
limits: [u, d]
|
|
469
470
|
}),
|
|
470
471
|
cone: null,
|
|
471
472
|
spec: {
|
|
472
473
|
type: "hinge",
|
|
473
474
|
axis: [
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
475
|
+
f[0],
|
|
476
|
+
f[1],
|
|
477
|
+
f[2]
|
|
477
478
|
],
|
|
478
|
-
limits: [
|
|
479
|
+
limits: [u, d]
|
|
479
480
|
}
|
|
480
481
|
};
|
|
481
482
|
}
|
|
@@ -487,9 +488,9 @@ var b = (e, t, n) => {
|
|
|
487
488
|
for (let t of this.parts) e = Math.max(e, t.weight);
|
|
488
489
|
return e;
|
|
489
490
|
}
|
|
490
|
-
set weight(
|
|
491
|
-
if (!(
|
|
492
|
-
for (let
|
|
491
|
+
set weight(t) {
|
|
492
|
+
if (!(t >= 0 && t <= 1)) throw e("Ragdoll weight must be from 0 to 1.");
|
|
493
|
+
for (let e of this.parts) (e.limp || !this.loose) && (e.weight = t, e.fade = 0);
|
|
493
494
|
}
|
|
494
495
|
get rootPosition() {
|
|
495
496
|
let e = this.live().parts[0].cur;
|
|
@@ -499,20 +500,20 @@ var b = (e, t, n) => {
|
|
|
499
500
|
e[2]
|
|
500
501
|
];
|
|
501
502
|
}
|
|
502
|
-
limbBones(
|
|
503
|
-
if (
|
|
504
|
-
let
|
|
505
|
-
for (let
|
|
506
|
-
let
|
|
507
|
-
if (!
|
|
508
|
-
|
|
503
|
+
limbBones(t) {
|
|
504
|
+
if (t === void 0) return this.parts;
|
|
505
|
+
let n = /* @__PURE__ */ new Set();
|
|
506
|
+
for (let r of Array.isArray(t) ? t : [t]) {
|
|
507
|
+
let t = r && "body" in r ? r.node : r, i = this.parts.find((e) => e.node === t);
|
|
508
|
+
if (!i) throw e("That node is not a bone of this ragdoll; pass nodes or parts of ragdoll.bones.");
|
|
509
|
+
n.add(i);
|
|
509
510
|
}
|
|
510
|
-
for (let e of this.parts) e.parent !== -1 &&
|
|
511
|
-
return [...
|
|
511
|
+
for (let e of this.parts) e.parent !== -1 && n.has(this.parts[e.parent]) && n.add(e);
|
|
512
|
+
return [...n];
|
|
512
513
|
}
|
|
513
514
|
activate(e) {
|
|
514
515
|
this.live();
|
|
515
|
-
let
|
|
516
|
+
let t = this.physics.fixedTimestep, r = this.stepped >= 2;
|
|
516
517
|
this.engine.store.tick(!1);
|
|
517
518
|
for (let i of this.limbBones(e)) {
|
|
518
519
|
if (i.limp) continue;
|
|
@@ -520,11 +521,11 @@ var b = (e, t, n) => {
|
|
|
520
521
|
let { prev: e, cur: a } = i;
|
|
521
522
|
if (!r) continue;
|
|
522
523
|
if (i.body.setLinearVelocity([
|
|
523
|
-
(a[0] - e[0]) /
|
|
524
|
-
(a[1] - e[1]) /
|
|
525
|
-
(a[2] - e[2]) /
|
|
526
|
-
]),
|
|
527
|
-
let o = Math.hypot(this.r[0], this.r[1], this.r[2]), s = 2 * Math.atan2(o, this.r[3]), c = o > 1e-9 ? s / o /
|
|
524
|
+
(a[0] - e[0]) / t,
|
|
525
|
+
(a[1] - e[1]) / t,
|
|
526
|
+
(a[2] - e[2]) / t
|
|
527
|
+
]), m(a.subarray(3, 7), n(e.subarray(3, 7), this.q), this.r), this.r[3] < 0) for (let e = 0; e < 4; e++) this.r[e] = -this.r[e];
|
|
528
|
+
let o = Math.hypot(this.r[0], this.r[1], this.r[2]), s = 2 * Math.atan2(o, this.r[3]), c = o > 1e-9 ? s / o / t : 0;
|
|
528
529
|
i.body.setAngularVelocity([
|
|
529
530
|
this.r[0] * c,
|
|
530
531
|
this.r[1] * c,
|
|
@@ -533,48 +534,304 @@ var b = (e, t, n) => {
|
|
|
533
534
|
}
|
|
534
535
|
return this;
|
|
535
536
|
}
|
|
536
|
-
deactivate(
|
|
537
|
-
if (this.live(), !(
|
|
538
|
-
let
|
|
537
|
+
deactivate(t = .3, n) {
|
|
538
|
+
if (this.live(), !(t >= 0) || t === Infinity) throw e("Ragdoll blend duration must be a finite number of seconds, 0 or more.");
|
|
539
|
+
let r = this.limbBones(n);
|
|
539
540
|
if (!this.loose) return this;
|
|
540
541
|
this.physicsLocals();
|
|
541
|
-
for (let
|
|
542
|
+
for (let e of r) e.limp && (e.body.setType("kinematicPosition"), e.limp = !1, e.jump = !0, e.fade = t > 0 ? 1 / t : 0, e.fadeFrom = e.weight, e.fadeAt = 0, t || (e.weight = 0), this.loose--);
|
|
542
543
|
return this;
|
|
543
544
|
}
|
|
544
545
|
part(e) {
|
|
545
546
|
return this.parts.find((t) => t.node === e);
|
|
546
547
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
548
|
+
debugRender(t = {}) {
|
|
549
|
+
this.live();
|
|
550
|
+
let { capsules: n = !0, limits: r = !0, bones: a = !0, limitScale: o = .12 } = t;
|
|
551
|
+
if (!(o > 0) || !Number.isFinite(o)) throw e(`Ragdoll debug limitScale must be a positive finite number of metres (got ${o}).`);
|
|
552
|
+
let s = [], c = [], l = (e, t, n) => {
|
|
553
|
+
s.push(e[0], e[1], e[2], t[0], t[1], t[2]), c.push(n[0], n[1], n[2], n[3], n[0], n[1], n[2], n[3]);
|
|
554
|
+
}, u = (e) => {
|
|
555
|
+
let t = Math.abs(e[1]) < .9 ? [
|
|
556
|
+
0,
|
|
557
|
+
1,
|
|
558
|
+
0
|
|
559
|
+
] : [
|
|
560
|
+
1,
|
|
561
|
+
0,
|
|
562
|
+
0
|
|
563
|
+
], n = t[1] * e[2] - t[2] * e[1], r = t[2] * e[0] - t[0] * e[2], i = t[0] * e[1] - t[1] * e[0], a = Math.hypot(n, r, i) || 1;
|
|
564
|
+
return n /= a, r /= a, i /= a, [[
|
|
565
|
+
n,
|
|
566
|
+
r,
|
|
567
|
+
i
|
|
568
|
+
], [
|
|
569
|
+
e[1] * i - e[2] * r,
|
|
570
|
+
e[2] * n - e[0] * i,
|
|
571
|
+
e[0] * r - e[1] * n
|
|
572
|
+
]];
|
|
573
|
+
}, d = (e, t, n = 1) => [
|
|
574
|
+
e[0] + t[0] * n,
|
|
575
|
+
e[1] + t[1] * n,
|
|
576
|
+
e[2] + t[2] * n
|
|
577
|
+
], f = this.parts.map((e) => ({
|
|
578
|
+
pos: e.body.valid ? e.body.position : [
|
|
579
|
+
e.cur[0],
|
|
580
|
+
e.cur[1],
|
|
581
|
+
e.cur[2]
|
|
582
|
+
],
|
|
583
|
+
rot: e.body.valid ? e.body.rotation : [
|
|
584
|
+
e.cur[3],
|
|
585
|
+
e.cur[4],
|
|
586
|
+
e.cur[5],
|
|
587
|
+
e.cur[6]
|
|
588
|
+
]
|
|
589
|
+
}));
|
|
590
|
+
for (let e = 0; e < this.parts.length; e++) {
|
|
591
|
+
let t = this.parts[e], { pos: s, rot: c } = f[e], p = i(t.axis, c), h = t.limp ? t.weight < 1 ? [
|
|
592
|
+
1,
|
|
593
|
+
.58,
|
|
594
|
+
.15,
|
|
595
|
+
1
|
|
596
|
+
] : [
|
|
597
|
+
.25,
|
|
598
|
+
.9,
|
|
599
|
+
.4,
|
|
600
|
+
1
|
|
601
|
+
] : t.weight > 0 ? [
|
|
602
|
+
.2,
|
|
603
|
+
.85,
|
|
604
|
+
1,
|
|
605
|
+
1
|
|
606
|
+
] : [
|
|
607
|
+
.35,
|
|
608
|
+
.6,
|
|
609
|
+
1,
|
|
610
|
+
1
|
|
611
|
+
];
|
|
612
|
+
if (n) {
|
|
613
|
+
if (t.shape.type === "capsule") {
|
|
614
|
+
let e = t.radius, n = Math.max(t.length / 2 - e, .001), r = d(s, p, t.length / 2), i = d(r, p, n), a = d(r, p, -n), [o, c] = u(p);
|
|
615
|
+
for (let t = 0; t < 16; t++) {
|
|
616
|
+
let n = t / 16 * Math.PI * 2, r = (t + 1) / 16 * Math.PI * 2, s = Math.cos(n) * e, u = Math.sin(n) * e, f = Math.cos(r) * e, p = Math.sin(r) * e, m = [
|
|
617
|
+
o[0] * s + c[0] * u,
|
|
618
|
+
o[1] * s + c[1] * u,
|
|
619
|
+
o[2] * s + c[2] * u
|
|
620
|
+
], g = [
|
|
621
|
+
o[0] * f + c[0] * p,
|
|
622
|
+
o[1] * f + c[1] * p,
|
|
623
|
+
o[2] * f + c[2] * p
|
|
624
|
+
];
|
|
625
|
+
l(d(i, m), d(i, g), h), l(d(a, m), d(a, g), h);
|
|
626
|
+
}
|
|
627
|
+
for (let t of [
|
|
628
|
+
o,
|
|
629
|
+
c,
|
|
630
|
+
[
|
|
631
|
+
-o[0],
|
|
632
|
+
-o[1],
|
|
633
|
+
-o[2]
|
|
634
|
+
],
|
|
635
|
+
[
|
|
636
|
+
-c[0],
|
|
637
|
+
-c[1],
|
|
638
|
+
-c[2]
|
|
639
|
+
]
|
|
640
|
+
]) {
|
|
641
|
+
let n = d(i, t, e), r = d(a, t, e);
|
|
642
|
+
l(n, r, h), l(n, d(i, p, e), h), l(r, d(a, p, -e), h);
|
|
643
|
+
}
|
|
644
|
+
} else if (t.shape.type === "box") {
|
|
645
|
+
let e = [], { center: n, halfExtents: r, rotation: a } = t.shape;
|
|
646
|
+
for (let t = 0; t < 8; t++) {
|
|
647
|
+
let o = i([
|
|
648
|
+
r[0] * (t & 1 ? 1 : -1),
|
|
649
|
+
r[1] * (t & 2 ? 1 : -1),
|
|
650
|
+
r[2] * (t & 4 ? 1 : -1)
|
|
651
|
+
], a), l = [
|
|
652
|
+
o[0] + n[0],
|
|
653
|
+
o[1] + n[1],
|
|
654
|
+
o[2] + n[2]
|
|
655
|
+
], u = i(l, c);
|
|
656
|
+
e.push([
|
|
657
|
+
s[0] + u[0],
|
|
658
|
+
s[1] + u[1],
|
|
659
|
+
s[2] + u[2]
|
|
660
|
+
]);
|
|
661
|
+
}
|
|
662
|
+
for (let t = 0; t < 8; t++) for (let n of [
|
|
663
|
+
1,
|
|
664
|
+
2,
|
|
665
|
+
4
|
|
666
|
+
]) t & n || l(e[t], e[t | n], h);
|
|
667
|
+
} else {
|
|
668
|
+
let e = t.shape.points, n = Math.floor(e.length / 3), r = [];
|
|
669
|
+
for (let t = 0; t < n; t++) {
|
|
670
|
+
let n = i([
|
|
671
|
+
e[t * 3],
|
|
672
|
+
e[t * 3 + 1],
|
|
673
|
+
e[t * 3 + 2]
|
|
674
|
+
], c);
|
|
675
|
+
r.push([
|
|
676
|
+
s[0] + n[0],
|
|
677
|
+
s[1] + n[1],
|
|
678
|
+
s[2] + n[2]
|
|
679
|
+
]);
|
|
680
|
+
}
|
|
681
|
+
for (let e = 0; e < n; e++) l(r[e], r[(e + 1) % n], h);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
if (a && (l(s, d(s, p, t.length), [
|
|
685
|
+
.95,
|
|
686
|
+
.95,
|
|
687
|
+
.85,
|
|
688
|
+
1
|
|
689
|
+
]), t.parent !== -1 && l(f[t.parent].pos, s, [
|
|
690
|
+
.9,
|
|
691
|
+
.78,
|
|
692
|
+
.3,
|
|
693
|
+
.9
|
|
694
|
+
])), r && t.parent !== -1 && t.spec) {
|
|
695
|
+
let e = m(f[t.parent].rot, t.rest, /* @__PURE__ */ new Float64Array(4)), n = [
|
|
696
|
+
1,
|
|
697
|
+
.8,
|
|
698
|
+
.2,
|
|
699
|
+
1
|
|
700
|
+
];
|
|
701
|
+
if (t.spec.type === "cone") {
|
|
702
|
+
let r = i(t.axis, e), a = t.spec.swing ?? Math.PI / 4, [c, f] = u(r), m = d(s, r, o * Math.cos(a)), h = o * Math.sin(a);
|
|
703
|
+
for (let e = 0; e < 16; e++) {
|
|
704
|
+
let t = e / 16 * Math.PI * 2, r = (e + 1) / 16 * Math.PI * 2, i = [
|
|
705
|
+
m[0] + (c[0] * Math.cos(t) + f[0] * Math.sin(t)) * h,
|
|
706
|
+
m[1] + (c[1] * Math.cos(t) + f[1] * Math.sin(t)) * h,
|
|
707
|
+
m[2] + (c[2] * Math.cos(t) + f[2] * Math.sin(t)) * h
|
|
708
|
+
];
|
|
709
|
+
l(i, [
|
|
710
|
+
m[0] + (c[0] * Math.cos(r) + f[0] * Math.sin(r)) * h,
|
|
711
|
+
m[1] + (c[1] * Math.cos(r) + f[1] * Math.sin(r)) * h,
|
|
712
|
+
m[2] + (c[2] * Math.cos(r) + f[2] * Math.sin(r)) * h
|
|
713
|
+
], n), e % 4 == 0 && l(s, i, n);
|
|
714
|
+
}
|
|
715
|
+
let g = Math.acos(Math.max(-1, Math.min(1, p[0] * r[0] + p[1] * r[1] + p[2] * r[2])));
|
|
716
|
+
l(s, d(s, p, o), g > a + .02 ? [
|
|
717
|
+
1,
|
|
718
|
+
.25,
|
|
719
|
+
.2,
|
|
720
|
+
1
|
|
721
|
+
] : [
|
|
722
|
+
.3,
|
|
723
|
+
1,
|
|
724
|
+
.5,
|
|
725
|
+
1
|
|
726
|
+
]);
|
|
727
|
+
} else if (t.spec.type === "hinge") {
|
|
728
|
+
let r = i(t.spec.axis, e), a = i(t.axis, e), c = a[0] * r[0] + a[1] * r[1] + a[2] * r[2], f = a[0] - r[0] * c, m = a[1] - r[1] * c, h = a[2] - r[2] * c, g = Math.hypot(f, m, h), _ = g > 1e-4 ? [
|
|
729
|
+
f / g,
|
|
730
|
+
m / g,
|
|
731
|
+
h / g
|
|
732
|
+
] : u(r)[0], v = [
|
|
733
|
+
r[1] * _[2] - r[2] * _[1],
|
|
734
|
+
r[2] * _[0] - r[0] * _[2],
|
|
735
|
+
r[0] * _[1] - r[1] * _[0]
|
|
736
|
+
], [y, b] = t.spec.limits ?? [-Math.PI * .75, 0], x = (e) => [
|
|
737
|
+
s[0] + (_[0] * Math.cos(e) + v[0] * Math.sin(e)) * o,
|
|
738
|
+
s[1] + (_[1] * Math.cos(e) + v[1] * Math.sin(e)) * o,
|
|
739
|
+
s[2] + (_[2] * Math.cos(e) + v[2] * Math.sin(e)) * o
|
|
740
|
+
];
|
|
741
|
+
l(d(s, r, -o * .5), d(s, r, o * .5), [
|
|
742
|
+
1,
|
|
743
|
+
.5,
|
|
744
|
+
.15,
|
|
745
|
+
1
|
|
746
|
+
]), l(s, x(y), n), l(s, x(b), n);
|
|
747
|
+
for (let e = 0; e < 12; e++) l(x(y + (b - y) * e / 12), x(y + (b - y) * (e + 1) / 12), n);
|
|
748
|
+
l(s, d(s, p, o), [
|
|
749
|
+
.3,
|
|
750
|
+
1,
|
|
751
|
+
.5,
|
|
752
|
+
1
|
|
753
|
+
]);
|
|
754
|
+
} else {
|
|
755
|
+
let e = o * .35;
|
|
756
|
+
l([
|
|
757
|
+
s[0] - e,
|
|
758
|
+
s[1],
|
|
759
|
+
s[2]
|
|
760
|
+
], [
|
|
761
|
+
s[0] + e,
|
|
762
|
+
s[1],
|
|
763
|
+
s[2]
|
|
764
|
+
], [
|
|
765
|
+
.8,
|
|
766
|
+
.85,
|
|
767
|
+
.95,
|
|
768
|
+
1
|
|
769
|
+
]), l([
|
|
770
|
+
s[0],
|
|
771
|
+
s[1] - e,
|
|
772
|
+
s[2]
|
|
773
|
+
], [
|
|
774
|
+
s[0],
|
|
775
|
+
s[1] + e,
|
|
776
|
+
s[2]
|
|
777
|
+
], [
|
|
778
|
+
.8,
|
|
779
|
+
.85,
|
|
780
|
+
.95,
|
|
781
|
+
1
|
|
782
|
+
]), l([
|
|
783
|
+
s[0],
|
|
784
|
+
s[1],
|
|
785
|
+
s[2] - e
|
|
786
|
+
], [
|
|
787
|
+
s[0],
|
|
788
|
+
s[1],
|
|
789
|
+
s[2] + e
|
|
790
|
+
], [
|
|
791
|
+
.8,
|
|
792
|
+
.85,
|
|
793
|
+
.95,
|
|
794
|
+
1
|
|
795
|
+
]);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return {
|
|
800
|
+
vertices: new Float32Array(s),
|
|
801
|
+
colors: new Float32Array(c)
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
setup(e) {
|
|
805
|
+
if (this.disposed) throw new t("disposed", "Ragdoll is disposed; create a new Ragdoll.");
|
|
806
|
+
if (e !== this.engine) throw new t("attached", "Ragdoll belongs to its bones' engine; attach it to that engine.");
|
|
807
|
+
this.attached = e;
|
|
551
808
|
}
|
|
552
|
-
onPreUpdate(
|
|
809
|
+
onPreUpdate(t) {
|
|
553
810
|
if (this.disposed) return;
|
|
554
811
|
if (this.physics.isDisposed || !this.parts[0].body.valid) {
|
|
555
812
|
this.dispose();
|
|
556
813
|
return;
|
|
557
814
|
}
|
|
558
|
-
if (!Number.isFinite(
|
|
559
|
-
let
|
|
560
|
-
|
|
561
|
-
for (let
|
|
562
|
-
|
|
563
|
-
let n =
|
|
564
|
-
|
|
815
|
+
if (!Number.isFinite(t) || t < 0) throw e("Invalid ragdoll delta; pass a finite delta of 0 or more seconds.");
|
|
816
|
+
let n = this.engine.store;
|
|
817
|
+
n.tick(!1);
|
|
818
|
+
for (let e of this.parts) if (this.track(e), !e.limp && e.fade > 0) {
|
|
819
|
+
e.fadeAt = Math.min(1, e.fadeAt + e.fade * t);
|
|
820
|
+
let n = e.fadeAt;
|
|
821
|
+
e.weight = e.fadeFrom * (1 - n * n * (3 - 2 * n)), n >= 1 && (e.weight = 0, e.fade = 0, e.landed = !0);
|
|
565
822
|
}
|
|
566
|
-
this.loose && this.physicsLocals(), this.write(),
|
|
823
|
+
this.loose && this.physicsLocals(), this.write(), n.tick(!1);
|
|
567
824
|
for (let e of this.parts) {
|
|
568
825
|
if (e.limp) continue;
|
|
569
826
|
let t = e.node.matrix, n = e.target;
|
|
570
|
-
|
|
827
|
+
p(t, this.q), n[0] = t[12], n[1] = t[13], n[2] = t[14], n[3] = this.q[0], n[4] = this.q[1], n[5] = this.q[2], n[6] = this.q[3];
|
|
571
828
|
}
|
|
572
829
|
}
|
|
573
|
-
fit(e,
|
|
574
|
-
let r = this.engine.store.parents[e.node.id], i = e.parent === -1 ? r === -1 ? null : this.engine.meshes.get(r) :
|
|
575
|
-
i &&
|
|
830
|
+
fit(e, t) {
|
|
831
|
+
let r = this.engine.store.parents[e.node.id], i = e.parent === -1 ? r === -1 ? null : this.engine.meshes.get(r) : t[e.parent].node, a = p(e.node.matrix, /* @__PURE__ */ new Float64Array(4));
|
|
832
|
+
i && m(n(p(i.matrix, this.q), this.q), a, a);
|
|
576
833
|
let o = this.readLocal(e.node, this.pose);
|
|
577
|
-
|
|
834
|
+
m(n(a, a), o.subarray(3, 7), e.bind), Math.hypot(e.bind[0], e.bind[1], e.bind[2]) < l && e.bind.set(u);
|
|
578
835
|
}
|
|
579
836
|
readLocal(e, t) {
|
|
580
837
|
let n = e.engine.store.transforms, r = e.id;
|
|
@@ -589,31 +846,31 @@ var b = (e, t, n) => {
|
|
|
589
846
|
}
|
|
590
847
|
physicsLocals() {
|
|
591
848
|
let e = this.physics.alpha;
|
|
592
|
-
for (let
|
|
593
|
-
let { prev: r, cur: i, world: a, local: o } =
|
|
849
|
+
for (let t of this.parts) {
|
|
850
|
+
let { prev: r, cur: i, world: a, local: o } = t;
|
|
594
851
|
for (let t = 0; t < 3; t++) a[t] = r[t] + (i[t] - r[t]) * e;
|
|
595
|
-
if (
|
|
596
|
-
if (o[0] =
|
|
597
|
-
let e = this.parts[
|
|
598
|
-
|
|
852
|
+
if (g(r, 3, i, 3, e, a, 3), !t.limp) continue;
|
|
853
|
+
if (o[0] = t.animated[0], o[1] = t.animated[1], o[2] = t.animated[2], t.parent !== -1) {
|
|
854
|
+
let e = this.parts[t.parent].world;
|
|
855
|
+
n(e.subarray(3, 7), this.q), m(this.q, a.subarray(3, 7), this.r), m(this.r, t.bind, o, 3);
|
|
599
856
|
continue;
|
|
600
857
|
}
|
|
601
|
-
let s = this.engine.store.parents[
|
|
858
|
+
let s = this.engine.store.parents[t.node.id];
|
|
602
859
|
if (s === -1) {
|
|
603
|
-
o.set(a),
|
|
860
|
+
o.set(a), m(a.subarray(3, 7), t.bind, o, 3);
|
|
604
861
|
continue;
|
|
605
862
|
}
|
|
606
863
|
let c = this.engine.meshes.get(s).matrix;
|
|
607
|
-
|
|
608
|
-
let l = a[0] - c[12], u = a[1] - c[13],
|
|
609
|
-
o[0] = (l * (c[5] * c[10] - c[6] * c[9]) + u * (c[8] * c[6] - c[4] * c[10]) +
|
|
864
|
+
n(p(c, this.q), this.q), m(this.q, a.subarray(3, 7), this.r), m(this.r, t.bind, o, 3);
|
|
865
|
+
let l = a[0] - c[12], u = a[1] - c[13], d = a[2] - c[14], h = f(c);
|
|
866
|
+
o[0] = (l * (c[5] * c[10] - c[6] * c[9]) + u * (c[8] * c[6] - c[4] * c[10]) + d * (c[4] * c[9] - c[8] * c[5])) / h, o[1] = (l * (c[9] * c[2] - c[1] * c[10]) + u * (c[0] * c[10] - c[8] * c[2]) + d * (c[8] * c[1] - c[0] * c[9])) / h, o[2] = (l * (c[1] * c[6] - c[5] * c[2]) + u * (c[4] * c[2] - c[0] * c[6]) + d * (c[0] * c[5] - c[4] * c[1])) / h;
|
|
610
867
|
}
|
|
611
868
|
}
|
|
612
869
|
write() {
|
|
613
870
|
for (let e of this.parts) {
|
|
614
871
|
let { animated: t, local: n, written: r, node: i, weight: a } = e;
|
|
615
872
|
if (!a && !e.landed) continue;
|
|
616
|
-
if (e.landed = !1,
|
|
873
|
+
if (e.landed = !1, g(t, 3, n, 3, a, r, 3), e.parent === -1) for (let e = 0; e < 3; e++) r[e] = t[e] + (n[e] - t[e]) * a;
|
|
617
874
|
else for (let e = 0; e < 3; e++) r[e] = t[e];
|
|
618
875
|
i.setPosition([
|
|
619
876
|
r[0],
|
|
@@ -649,16 +906,16 @@ var b = (e, t, n) => {
|
|
|
649
906
|
}
|
|
650
907
|
}
|
|
651
908
|
limits() {
|
|
652
|
-
let e = this.parts,
|
|
653
|
-
for (let
|
|
654
|
-
let r = e[
|
|
655
|
-
n
|
|
909
|
+
let e = this.parts, t = this.omega, r = this.stiffness, { q: a, r: o, s, v: c, w: l } = this, u = !1;
|
|
910
|
+
for (let n = 0; n < e.length; n++) {
|
|
911
|
+
let r = e[n].body.angularVelocity;
|
|
912
|
+
t[n * 3] = r[0], t[n * 3 + 1] = r[1], t[n * 3 + 2] = r[2];
|
|
656
913
|
}
|
|
657
|
-
for (let
|
|
658
|
-
let d = e[
|
|
914
|
+
for (let t = 1; t < e.length; t++) {
|
|
915
|
+
let d = e[t], f = d.cone;
|
|
659
916
|
if (!f || !d.limp) continue;
|
|
660
|
-
let
|
|
661
|
-
if (
|
|
917
|
+
let p = e[d.parent], h = p.body.rotation, g = d.body.rotation;
|
|
918
|
+
if (m(h, d.rest, s), n(s, a), m(a, g, o), o[3] < 0) for (let e = 0; e < 4; e++) o[e] = -o[e];
|
|
662
919
|
let _ = d.axis, v = o[0] * _[0] + o[1] * _[1] + o[2] * _[2], y = 2 * Math.atan2(v, o[3]);
|
|
663
920
|
y > Math.PI && (y -= 2 * Math.PI);
|
|
664
921
|
let b = Math.hypot(v, o[3]);
|
|
@@ -667,28 +924,28 @@ var b = (e, t, n) => {
|
|
|
667
924
|
0,
|
|
668
925
|
0,
|
|
669
926
|
1
|
|
670
|
-
]),
|
|
671
|
-
let x = Math.hypot(o[0], o[1], o[2]), S = 2 * Math.atan2(x, Math.abs(o[3])), C =
|
|
927
|
+
]), m(o, a, o);
|
|
928
|
+
let x = Math.hypot(o[0], o[1], o[2]), S = 2 * Math.atan2(x, Math.abs(o[3])), C = p.limp;
|
|
672
929
|
if (S > f.swing && x > 1e-9) {
|
|
673
930
|
let e = o[3] < 0 ? -1 : 1;
|
|
674
|
-
c[0] = o[0] / x * e, c[1] = o[1] / x * e, c[2] = o[2] / x * e,
|
|
931
|
+
c[0] = o[0] / x * e, c[1] = o[1] / x * e, c[2] = o[2] / x * e, i(c, s, c), u = this.push(t, d.parent, c, C) || u, this.turn(d, c, -(S - f.swing) * r);
|
|
675
932
|
}
|
|
676
933
|
if (y > f.max || y < f.min) {
|
|
677
|
-
|
|
934
|
+
i(_, g, l);
|
|
678
935
|
let e = y - f.max;
|
|
679
|
-
y < f.min && (e = f.min - y, l[0] = -l[0], l[1] = -l[1], l[2] = -l[2]), u = this.push(
|
|
936
|
+
y < f.min && (e = f.min - y, l[0] = -l[0], l[1] = -l[1], l[2] = -l[2]), u = this.push(t, d.parent, l, C) || u, this.turn(d, l, -e * r);
|
|
680
937
|
}
|
|
681
938
|
}
|
|
682
|
-
if (u) for (let
|
|
683
|
-
n
|
|
684
|
-
n
|
|
685
|
-
n
|
|
939
|
+
if (u) for (let n = 0; n < e.length; n++) e[n].limp && e[n].body.setAngularVelocity([
|
|
940
|
+
t[n * 3],
|
|
941
|
+
t[n * 3 + 1],
|
|
942
|
+
t[n * 3 + 2]
|
|
686
943
|
]);
|
|
687
944
|
}
|
|
688
945
|
turn(e, t, n) {
|
|
689
946
|
let r = n / 2, i = Math.sin(r), a = this.turnBy;
|
|
690
947
|
a[0] = t[0] * i, a[1] = t[1] * i, a[2] = t[2] * i, a[3] = Math.cos(r);
|
|
691
|
-
let o =
|
|
948
|
+
let o = m(a, e.body.rotation, this.turned);
|
|
692
949
|
e.body.setRotation([
|
|
693
950
|
o[0],
|
|
694
951
|
o[1],
|
|
@@ -706,8 +963,8 @@ var b = (e, t, n) => {
|
|
|
706
963
|
return !0;
|
|
707
964
|
}
|
|
708
965
|
inverseInertia(e, t) {
|
|
709
|
-
let n = this.c,
|
|
710
|
-
|
|
966
|
+
let n = this.c, r = e.cur.subarray(3, 7);
|
|
967
|
+
i(e.axis, r, n);
|
|
711
968
|
let a = n[0] * t[0] + n[1] * t[1] + n[2] * t[2], o = a * a;
|
|
712
969
|
return 1 / (o / e.inertia[0] + (1 - o) / e.inertia[1]);
|
|
713
970
|
}
|
|
@@ -722,7 +979,7 @@ var b = (e, t, n) => {
|
|
|
722
979
|
}
|
|
723
980
|
}
|
|
724
981
|
live() {
|
|
725
|
-
if (this.disposed) throw new
|
|
982
|
+
if (this.disposed) throw new t("disposed", "Ragdoll is disposed; create a new Ragdoll.");
|
|
726
983
|
return this;
|
|
727
984
|
}
|
|
728
985
|
dispose() {
|
|
@@ -738,4 +995,4 @@ var b = (e, t, n) => {
|
|
|
738
995
|
}
|
|
739
996
|
};
|
|
740
997
|
//#endregion
|
|
741
|
-
export {
|
|
998
|
+
export { S as Ragdoll };
|