@margoengine/ragdoll 0.0.0-stage → 0.1.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 -0
- package/NOTICE +4 -0
- package/README.md +381 -3
- package/dist/balance.d.ts +195 -0
- package/dist/balance.js +468 -0
- package/dist/getup.d.ts +86 -0
- package/dist/getup.js +135 -0
- package/dist/hulls.d.ts +71 -0
- package/dist/hulls.js +173 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +741 -0
- package/dist/limits-CCe6Pdej.js +55 -0
- package/dist/limits.d.ts +12 -0
- package/dist/powered.d.ts +145 -0
- package/dist/powered.js +245 -0
- package/dist/profile.d.ts +62 -0
- package/dist/profile.js +267 -0
- package/dist/ragdoll.d.ts +276 -0
- package/package.json +82 -6
package/dist/profile.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { conjugateQuaternion as e, normalizeQuaternion as t, rotateByQuaternion as n, rotationToQuaternion as r } from "@margoengine/math/quat";
|
|
2
|
+
import { detectSkeleton as i, humanoidBones as a } from "@margoengine/rigging/retarget";
|
|
3
|
+
//#region src/profile.ts
|
|
4
|
+
var o = "margoengine-ragdoll", s = 1, c = (e, t) => ({
|
|
5
|
+
type: "cone",
|
|
6
|
+
swing: e,
|
|
7
|
+
twist: [-t, t]
|
|
8
|
+
}), l = {
|
|
9
|
+
trunk: {
|
|
10
|
+
share: .4346,
|
|
11
|
+
joint: c(.35, .35)
|
|
12
|
+
},
|
|
13
|
+
head: {
|
|
14
|
+
share: .0694,
|
|
15
|
+
joint: c(.7, .9),
|
|
16
|
+
length: .13
|
|
17
|
+
},
|
|
18
|
+
upperArm: {
|
|
19
|
+
share: .0271,
|
|
20
|
+
joint: c(1.75, 1.2)
|
|
21
|
+
},
|
|
22
|
+
lowerArm: {
|
|
23
|
+
share: .0162,
|
|
24
|
+
joint: "elbow"
|
|
25
|
+
},
|
|
26
|
+
hand: {
|
|
27
|
+
share: .0061,
|
|
28
|
+
joint: c(.9, .3),
|
|
29
|
+
length: .108
|
|
30
|
+
},
|
|
31
|
+
upperLeg: {
|
|
32
|
+
share: .1416,
|
|
33
|
+
joint: c(1.3, .5)
|
|
34
|
+
},
|
|
35
|
+
lowerLeg: {
|
|
36
|
+
share: .0433,
|
|
37
|
+
joint: "knee"
|
|
38
|
+
},
|
|
39
|
+
foot: {
|
|
40
|
+
share: .0137,
|
|
41
|
+
joint: c(.6, .2),
|
|
42
|
+
length: .152
|
|
43
|
+
}
|
|
44
|
+
}, u = 2.4, d = 2.5, f = .53, p = [
|
|
45
|
+
"hips",
|
|
46
|
+
"head",
|
|
47
|
+
"leftUpperArm",
|
|
48
|
+
"rightUpperArm",
|
|
49
|
+
"leftLowerArm",
|
|
50
|
+
"rightLowerArm",
|
|
51
|
+
"leftUpperLeg",
|
|
52
|
+
"rightUpperLeg",
|
|
53
|
+
"leftLowerLeg",
|
|
54
|
+
"rightLowerLeg"
|
|
55
|
+
], m = [
|
|
56
|
+
[
|
|
57
|
+
"hips",
|
|
58
|
+
"trunk",
|
|
59
|
+
"trunk"
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
"spine",
|
|
63
|
+
"trunk",
|
|
64
|
+
"trunk"
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
"chest",
|
|
68
|
+
"trunk",
|
|
69
|
+
"trunk"
|
|
70
|
+
],
|
|
71
|
+
[
|
|
72
|
+
"upperChest",
|
|
73
|
+
"trunk",
|
|
74
|
+
"trunk"
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
"neck",
|
|
78
|
+
"head",
|
|
79
|
+
"head"
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
"head",
|
|
83
|
+
"head",
|
|
84
|
+
"head"
|
|
85
|
+
],
|
|
86
|
+
...["left", "right"].flatMap((e) => [
|
|
87
|
+
[
|
|
88
|
+
`${e}Shoulder`,
|
|
89
|
+
"trunk",
|
|
90
|
+
"trunk"
|
|
91
|
+
],
|
|
92
|
+
[
|
|
93
|
+
`${e}UpperArm`,
|
|
94
|
+
"upperArm",
|
|
95
|
+
`${e}UpperArm`
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
`${e}LowerArm`,
|
|
99
|
+
"lowerArm",
|
|
100
|
+
`${e}LowerArm`
|
|
101
|
+
],
|
|
102
|
+
[
|
|
103
|
+
`${e}Hand`,
|
|
104
|
+
"hand",
|
|
105
|
+
`${e}Hand`
|
|
106
|
+
],
|
|
107
|
+
[
|
|
108
|
+
`${e}UpperLeg`,
|
|
109
|
+
"upperLeg",
|
|
110
|
+
`${e}UpperLeg`
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
`${e}LowerLeg`,
|
|
114
|
+
"lowerLeg",
|
|
115
|
+
`${e}LowerLeg`
|
|
116
|
+
],
|
|
117
|
+
[
|
|
118
|
+
`${e}Foot`,
|
|
119
|
+
"foot",
|
|
120
|
+
`${e}Foot`
|
|
121
|
+
]
|
|
122
|
+
])
|
|
123
|
+
], h = (e) => [
|
|
124
|
+
e.matrix[12],
|
|
125
|
+
e.matrix[13],
|
|
126
|
+
e.matrix[14]
|
|
127
|
+
], g = (e, t) => [
|
|
128
|
+
e[0] - t[0],
|
|
129
|
+
e[1] - t[1],
|
|
130
|
+
e[2] - t[2]
|
|
131
|
+
], _ = (e, t) => [
|
|
132
|
+
e[1] * t[2] - e[2] * t[1],
|
|
133
|
+
e[2] * t[0] - e[0] * t[2],
|
|
134
|
+
e[0] * t[1] - e[1] * t[0]
|
|
135
|
+
], v = (e) => {
|
|
136
|
+
let t = Math.hypot(e[0], e[1], e[2]);
|
|
137
|
+
return [
|
|
138
|
+
e[0] / t,
|
|
139
|
+
e[1] / t,
|
|
140
|
+
e[2] / t
|
|
141
|
+
];
|
|
142
|
+
};
|
|
143
|
+
function y(e) {
|
|
144
|
+
let n = e.matrix;
|
|
145
|
+
return t(r(n, [
|
|
146
|
+
0,
|
|
147
|
+
0,
|
|
148
|
+
0,
|
|
149
|
+
1
|
|
150
|
+
], Math.hypot(n[0], n[1], n[2]), Math.hypot(n[4], n[5], n[6]), Math.hypot(n[8], n[9], n[10])));
|
|
151
|
+
}
|
|
152
|
+
var b = (e) => e.type === "hull" ? {
|
|
153
|
+
type: "hull",
|
|
154
|
+
points: Array.from(e.points)
|
|
155
|
+
} : e;
|
|
156
|
+
function x(e) {
|
|
157
|
+
if (!e?.bones) throw TypeError("saveRagdollProfile needs a Ragdoll: saveRagdollProfile(ragdoll).");
|
|
158
|
+
let t = /* @__PURE__ */ new Set();
|
|
159
|
+
return {
|
|
160
|
+
format: o,
|
|
161
|
+
version: s,
|
|
162
|
+
settings: { ...e.settings },
|
|
163
|
+
bones: e.bones.map((e) => {
|
|
164
|
+
let { name: n } = e.node;
|
|
165
|
+
if (!n) throw RangeError("A ragdoll profile finds bones by name; name every bone node before saving.");
|
|
166
|
+
if (t.has(n)) throw RangeError(`Two ragdoll bones are named "${n}"; rename one, a profile finds bones by name.`);
|
|
167
|
+
return t.add(n), {
|
|
168
|
+
name: n,
|
|
169
|
+
...e.spec ? { joint: e.spec } : {},
|
|
170
|
+
length: e.length,
|
|
171
|
+
radius: e.radius,
|
|
172
|
+
mass: e.mass,
|
|
173
|
+
shape: b(e.shape)
|
|
174
|
+
};
|
|
175
|
+
})
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function S(e, t) {
|
|
179
|
+
if (!e || !Array.isArray(t)) throw TypeError("ragdollOptions needs a profile and the nodes: ragdollOptions(profile, model.nodes).");
|
|
180
|
+
if (e.format !== o) throw RangeError(`That is not a ragdoll profile: its format is not "${o}"; pass what saveRagdollProfile made.`);
|
|
181
|
+
if (!(e.version <= s)) throw RangeError(`This profile is version ${e.version}; this package reads up to ${s}. Update it.`);
|
|
182
|
+
if (!Array.isArray(e.bones) || !e.bones.length) throw RangeError("A ragdoll profile needs a non-empty bones list; pass what saveRagdollProfile made.");
|
|
183
|
+
let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Set();
|
|
184
|
+
for (let e of t) n.has(e.name) && r.add(e.name), n.set(e.name, e);
|
|
185
|
+
let i = e.bones.map(({ name: e, ...t }) => {
|
|
186
|
+
let i = n.get(e);
|
|
187
|
+
if (!i) throw RangeError(`The profile's bone "${e}" is not in this skeleton; pass its nodes.`);
|
|
188
|
+
if (r.has(e)) throw RangeError(`Two nodes are named "${e}", so the profile cannot tell them apart; rename one.`);
|
|
189
|
+
return {
|
|
190
|
+
node: i,
|
|
191
|
+
...t
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
...e.settings,
|
|
196
|
+
bones: i
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function C(t, r = {}) {
|
|
200
|
+
if (!Array.isArray(t)) throw TypeError("humanoidProfile needs the skeleton nodes: humanoidProfile(model.nodes).");
|
|
201
|
+
let i = r.mass ?? 70;
|
|
202
|
+
if (!(i > 0) || i === Infinity) throw RangeError("Humanoid mass must be positive kilograms, e.g. 70.");
|
|
203
|
+
let a = r.bones ?? w(t), b = p.filter((e) => !a[e]);
|
|
204
|
+
if (b.length) throw RangeError(`This rig has no ${b.join(", ")}; pass them as options.bones: {hips: node, ...}.`);
|
|
205
|
+
let x = a.hips, { parents: S } = x.engine.store;
|
|
206
|
+
x.engine.store.tick(!1);
|
|
207
|
+
let C = new Map(t.map((e) => [e.id, e])), T = /* @__PURE__ */ new Map();
|
|
208
|
+
for (let [e, t, n] of m) {
|
|
209
|
+
let r = a[e];
|
|
210
|
+
r && T.set(r, {
|
|
211
|
+
segment: t,
|
|
212
|
+
group: n
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
for (let [e, t, n] of m) {
|
|
216
|
+
let r = a[e];
|
|
217
|
+
for (; r && r !== x;) {
|
|
218
|
+
let i = C.get(S[r.id]);
|
|
219
|
+
if (!i) throw RangeError(`Humanoid bone ${e} is not under the hips; check the bones passed as options.bones.`);
|
|
220
|
+
T.has(i) || T.set(i, {
|
|
221
|
+
segment: t,
|
|
222
|
+
group: n
|
|
223
|
+
}), r = i;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
let E = v(g(h(a.head), h(x))), D = v(_(v(g(h(a.leftUpperLeg), h(a.rightUpperLeg))), E)), O = a.leftFoot ?? a.leftLowerLeg, k = g(h(x), h(O)).reduce((e, t, n) => e + t * E[n], 0) / f, A = (t, r, i, a) => {
|
|
227
|
+
let o = v(g(h(r ?? t), h(t))), s = n(v(_(o, i)), e(y(t)));
|
|
228
|
+
return {
|
|
229
|
+
type: "hinge",
|
|
230
|
+
axis: [
|
|
231
|
+
s[0],
|
|
232
|
+
s[1],
|
|
233
|
+
s[2]
|
|
234
|
+
],
|
|
235
|
+
limits: [0, a]
|
|
236
|
+
};
|
|
237
|
+
}, j = /* @__PURE__ */ new Map();
|
|
238
|
+
for (let { group: e } of T.values()) j.set(e, (j.get(e) ?? 0) + 1);
|
|
239
|
+
let M = /* @__PURE__ */ new Map();
|
|
240
|
+
for (let e of ["left", "right"]) M.set(a[`${e}LowerLeg`], a[`${e}Foot`]), M.set(a[`${e}LowerArm`], a[`${e}Hand`]);
|
|
241
|
+
let N = [...T].map(([e, { segment: t, group: n }]) => {
|
|
242
|
+
let r = l[t], i = m.some(([t]) => a[t] === e), o = i ? r.joint : c(.35, .35);
|
|
243
|
+
return o === "knee" ? o = A(e, M.get(e), [
|
|
244
|
+
-D[0],
|
|
245
|
+
-D[1],
|
|
246
|
+
-D[2]
|
|
247
|
+
], u) : o === "elbow" && (o = A(e, M.get(e), D, d)), {
|
|
248
|
+
name: e.name,
|
|
249
|
+
...e === x ? {} : { joint: o },
|
|
250
|
+
...i && r.length !== void 0 && e !== a.neck ? { length: r.length * k } : {},
|
|
251
|
+
mass: r.share / j.get(n)
|
|
252
|
+
};
|
|
253
|
+
}), P = N.reduce((e, t) => e + t.mass, 0);
|
|
254
|
+
for (let e of N) e.mass = e.mass / P * i;
|
|
255
|
+
return {
|
|
256
|
+
format: o,
|
|
257
|
+
version: s,
|
|
258
|
+
bones: N
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function w(e) {
|
|
262
|
+
let t = e.map((e) => e.name ?? ""), n = i(t), r = {};
|
|
263
|
+
if (n) for (let [i, o] of a(t, n.preset)) r[i] = e[o];
|
|
264
|
+
return r;
|
|
265
|
+
}
|
|
266
|
+
//#endregion
|
|
267
|
+
export { C as humanoidProfile, S as ragdollOptions, x as saveRagdollProfile };
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Engine, EnginePlugin, Mesh } from '@margoengine/core/extension';
|
|
3
|
+
import type { Physics, PhysicsBody, PhysicsJoint } from '@margoengine/physics';
|
|
4
|
+
type Vec3 = readonly [number, number, number];
|
|
5
|
+
/**
|
|
6
|
+
* How a bone turns against its parent bone, measured from the rest pose the ragdoll was built in.
|
|
7
|
+
* - `cone`: a ball joint whose bone axis stays inside a cone of half angle `swing` radians around its rest direction
|
|
8
|
+
* and twists about that axis between `twist[0]` and `twist[1]` radians (shoulders, hips, spine, neck).
|
|
9
|
+
* - `hinge`: one axis of rotation, `axis` in the bone's own local frame, turning between `limits` radians (knees,
|
|
10
|
+
* elbows, fingers).
|
|
11
|
+
* - `free`: a ball joint with no limits.
|
|
12
|
+
*/
|
|
13
|
+
export type RagdollJoint = {
|
|
14
|
+
type: 'cone';
|
|
15
|
+
swing?: number;
|
|
16
|
+
twist?: readonly [number, number];
|
|
17
|
+
} | {
|
|
18
|
+
type: 'hinge';
|
|
19
|
+
axis: Vec3;
|
|
20
|
+
limits?: readonly [number, number];
|
|
21
|
+
} | {
|
|
22
|
+
type: 'free';
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The collider of a bone, in the bone's own frame (the body's frame), in world units.
|
|
26
|
+
* - `capsule`: the capsule from the bone's `length` and `radius`.
|
|
27
|
+
* - `hull`: the convex hull of `points`, xyz each.
|
|
28
|
+
* - `box`: a box of `halfExtents` around `center`, turned by the unit quaternion `rotation`.
|
|
29
|
+
*/
|
|
30
|
+
export type RagdollShape = {
|
|
31
|
+
type: 'capsule';
|
|
32
|
+
} | {
|
|
33
|
+
type: 'hull';
|
|
34
|
+
points: ArrayLike<number>;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'box';
|
|
37
|
+
center: Vec3;
|
|
38
|
+
halfExtents: Vec3;
|
|
39
|
+
rotation: readonly [number, number, number, number];
|
|
40
|
+
};
|
|
41
|
+
/** One bone of a ragdoll built from an explicit list ({@link RagdollOptions.bones}). Every field but `node` has a default. */
|
|
42
|
+
export interface RagdollBone {
|
|
43
|
+
/** The skeleton node (a glTF joint) this body moves. */
|
|
44
|
+
node: Mesh;
|
|
45
|
+
/** Joint to the parent bone. Default {@link RagdollOptions.joint}. The root bone has no joint. */
|
|
46
|
+
joint?: RagdollJoint;
|
|
47
|
+
/**
|
|
48
|
+
* Length of the bone's capsule in metres, from the node's origin toward its children. Default: the distance to its
|
|
49
|
+
* children's centre (or to its farthest child when that centre is nearly at the origin). Required for a node with
|
|
50
|
+
* no children.
|
|
51
|
+
*/
|
|
52
|
+
length?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Capsule radius in metres. Default {@link RagdollOptions.thickness} times the length, or half the distance of the
|
|
55
|
+
* farthest child from the bone's line when that is more (a pelvis between the legs, a chest between the arms).
|
|
56
|
+
*/
|
|
57
|
+
radius?: number;
|
|
58
|
+
/** Mass in kilograms. Default: the capsule volume times {@link RagdollOptions.density}. */
|
|
59
|
+
mass?: number;
|
|
60
|
+
/** The collider. Default `{type: 'capsule'}`. */
|
|
61
|
+
shape?: RagdollShape;
|
|
62
|
+
}
|
|
63
|
+
/** Options of {@link Ragdoll}. Pass `root` (every bone under it) or `bones` (an explicit list); the rest have defaults. */
|
|
64
|
+
export interface RagdollOptions {
|
|
65
|
+
/** The skeleton's root bone (usually the hips): it and every descendant at least `minLength` long become bones. */
|
|
66
|
+
root?: Mesh;
|
|
67
|
+
/** An explicit list of bones instead of `root`. One is the root; every other bone's parent node is in the list. */
|
|
68
|
+
bones?: readonly (Mesh | RagdollBone)[];
|
|
69
|
+
/** Joint for every bone that does not set its own. Default `{type: 'cone', swing: π/4, twist: [-π/6, π/6]}`. */
|
|
70
|
+
joint?: RagdollJoint;
|
|
71
|
+
/** With `root`, bones whose farthest child is nearer than this many metres are left out with it (fingers, toes). Default 0.05. */
|
|
72
|
+
minLength?: number;
|
|
73
|
+
/** Capsule radius as a fraction of the bone length, for bones without a `radius`. Default 0.2. */
|
|
74
|
+
thickness?: number;
|
|
75
|
+
/** Body density in kg per cubic metre. Default 1000 (water, about a human body's). */
|
|
76
|
+
density?: number;
|
|
77
|
+
/** Start limp (`true`) or following the animation (`false`). Default false. */
|
|
78
|
+
active?: boolean;
|
|
79
|
+
/** Packed collision groups (`collisionGroups` from physics) for every capsule. Default: group 15, colliding with groups 0 to 14, so the ragdoll's parts do not collide with each other. */
|
|
80
|
+
collisionGroups?: number;
|
|
81
|
+
/** Friction of every capsule. Default 0.6. */
|
|
82
|
+
friction?: number;
|
|
83
|
+
/** Linear damping per body, so a limp ragdoll settles. Default 0.05. */
|
|
84
|
+
linearDamping?: number;
|
|
85
|
+
/** Angular damping per body. Default 0.8. */
|
|
86
|
+
angularDamping?: number;
|
|
87
|
+
/** How hard swing and twist limits push back, as the share of the error removed per physics step, 0 to 1. Default 0.2. */
|
|
88
|
+
limitStiffness?: number;
|
|
89
|
+
}
|
|
90
|
+
/** What every body of a ragdoll shares, as built ({@link Ragdoll.settings}). */
|
|
91
|
+
export interface RagdollSettings {
|
|
92
|
+
/** Packed collision groups. */
|
|
93
|
+
readonly collisionGroups: number;
|
|
94
|
+
/** Friction. */
|
|
95
|
+
readonly friction: number;
|
|
96
|
+
/** Linear damping. */
|
|
97
|
+
readonly linearDamping: number;
|
|
98
|
+
/** Angular damping. */
|
|
99
|
+
readonly angularDamping: number;
|
|
100
|
+
/** Share of a limit error removed per physics step. */
|
|
101
|
+
readonly limitStiffness: number;
|
|
102
|
+
}
|
|
103
|
+
/** A body of a ragdoll and the bone it moves ({@link Ragdoll.bones}). */
|
|
104
|
+
export interface RagdollPart {
|
|
105
|
+
/** The skeleton node. */
|
|
106
|
+
readonly node: Mesh;
|
|
107
|
+
/** Its capsule body; apply impulses to it for hits. `body.userData` is `{ragdoll, node}`. */
|
|
108
|
+
readonly body: PhysicsBody;
|
|
109
|
+
/** The joint to the parent bone; null for the root bone. */
|
|
110
|
+
readonly joint: PhysicsJoint | null;
|
|
111
|
+
/** Index of the parent bone in {@link Ragdoll.bones}; -1 for the root bone. */
|
|
112
|
+
readonly parent: number;
|
|
113
|
+
/** Capsule length in metres. */
|
|
114
|
+
readonly length: number;
|
|
115
|
+
/** Capsule radius in metres. */
|
|
116
|
+
readonly radius: number;
|
|
117
|
+
/** Mass in kilograms, as built. */
|
|
118
|
+
readonly mass: number;
|
|
119
|
+
/** The collider now, in the bone's own frame ({@link Ragdoll.setShape}). */
|
|
120
|
+
readonly shape: RagdollShape;
|
|
121
|
+
/** Unit direction of the bone in its own frame (the body's frame), toward its children. */
|
|
122
|
+
readonly axis: ArrayLike<number>;
|
|
123
|
+
/** Rest rotation relative to the parent bone, xyzw; what the joint limits measure from. */
|
|
124
|
+
readonly rest: ArrayLike<number>;
|
|
125
|
+
/** The joint as built, every default filled in; null for the root bone. */
|
|
126
|
+
readonly spec: RagdollJoint | null;
|
|
127
|
+
/**
|
|
128
|
+
* The node's local rotation is the turn from the parent body to this one times this: identity unless an uneven scale
|
|
129
|
+
* above the bone skews it.
|
|
130
|
+
*/
|
|
131
|
+
readonly bind: ArrayLike<number>;
|
|
132
|
+
/** The local pose the animation last gave the bone, xyz then xyzw: what motors drive toward. */
|
|
133
|
+
readonly animated: ArrayLike<number>;
|
|
134
|
+
/** True while physics drives this bone. */
|
|
135
|
+
readonly limp: boolean;
|
|
136
|
+
/** How much of this bone's drawn pose is the ragdoll's, 0 (the animation) to 1 (physics). */
|
|
137
|
+
readonly weight: number;
|
|
138
|
+
}
|
|
139
|
+
/** One bone or several, as nodes or parts: each stands for itself and every bone under it. */
|
|
140
|
+
export type RagdollLimb = Mesh | RagdollPart | readonly (Mesh | RagdollPart)[];
|
|
141
|
+
/**
|
|
142
|
+
* A ragdoll on a skeleton: one capsule body per bone, joined at the bone origins with swing and twist limits. Built
|
|
143
|
+
* kinematic, it follows the animation (hit boxes that push the world); `activate()` makes it limp, so physics drives
|
|
144
|
+
* the bones; `deactivate(seconds)` hands the bones back to the animation with a blend. Create it after the Animator so
|
|
145
|
+
* it runs after the animation each frame. Removed with `dispose()` (or `using`) or with the physics world.
|
|
146
|
+
* @example
|
|
147
|
+
* const ragdoll = Ragdoll.attach(physics, { root: model.nodes[hips] });
|
|
148
|
+
* ragdoll.activate(); // limp: physics moves the bones
|
|
149
|
+
* ragdoll.bones[0].body.applyImpulse([0, 0, -40]);
|
|
150
|
+
* ragdoll.deactivate(0.5); // blend back to the animation over half a second
|
|
151
|
+
*/
|
|
152
|
+
export declare class Ragdoll implements EnginePlugin {
|
|
153
|
+
readonly physics: Physics;
|
|
154
|
+
/** Plugin name, `ragdoll:<root node id>`. */
|
|
155
|
+
readonly name: string;
|
|
156
|
+
/** The bodies, parents before children; `bones[0]` is the root bone. */
|
|
157
|
+
readonly bones: readonly RagdollPart[];
|
|
158
|
+
/** What every body shares, as built. */
|
|
159
|
+
readonly settings: RagdollSettings;
|
|
160
|
+
private readonly parts;
|
|
161
|
+
private readonly engine;
|
|
162
|
+
private readonly stiffness;
|
|
163
|
+
private readonly unsubscribe;
|
|
164
|
+
/** How many bones are limp. */
|
|
165
|
+
private loose;
|
|
166
|
+
private stepped;
|
|
167
|
+
private attached?;
|
|
168
|
+
private disposed;
|
|
169
|
+
private readonly q;
|
|
170
|
+
private readonly r;
|
|
171
|
+
private readonly s;
|
|
172
|
+
private readonly v;
|
|
173
|
+
private readonly w;
|
|
174
|
+
private readonly c;
|
|
175
|
+
private readonly pose;
|
|
176
|
+
private readonly turnBy;
|
|
177
|
+
private readonly turned;
|
|
178
|
+
/** Angular velocities per bone during the limit pass, xyz each. */
|
|
179
|
+
private omega;
|
|
180
|
+
/**
|
|
181
|
+
* Builds the bodies and joints at the skeleton's current pose, which becomes the rest pose the limits measure from.
|
|
182
|
+
* @param physics The world to add the bodies to.
|
|
183
|
+
* @param options `root` or `bones`, plus optional joint, size and mass settings.
|
|
184
|
+
*/
|
|
185
|
+
constructor(physics: Physics, options: RagdollOptions);
|
|
186
|
+
/** Builds a ragdoll on `physics` and adds it to its bones' engine, in the world's scene, after the plugins already there (the Animator). */
|
|
187
|
+
static attach(physics: Physics, options: RagdollOptions): Ragdoll;
|
|
188
|
+
private collider;
|
|
189
|
+
/**
|
|
190
|
+
* Replaces a bone's collider. Its mass, friction, collision groups and joint stay; its inertia follows the new shape.
|
|
191
|
+
* Set shapes before `addRagdollMotors`, which reads each limb's inertia once.
|
|
192
|
+
* @throws RangeError for a node that is not a bone of this ragdoll, or a shape out of range; the old shape stays.
|
|
193
|
+
*/
|
|
194
|
+
setShape(bone: Mesh | RagdollPart, shape: RagdollShape): this;
|
|
195
|
+
private boneList;
|
|
196
|
+
private join;
|
|
197
|
+
/** True while physics drives any bone (after `activate()`, until `deactivate()`). */
|
|
198
|
+
get active(): boolean;
|
|
199
|
+
/**
|
|
200
|
+
* How much of the drawn pose is the ragdoll's, 0 (the animation) to 1 (physics): the largest of any bone's.
|
|
201
|
+
* `activate()` sets 1 and `deactivate(seconds)` fades it to 0; set it yourself for a partial blend of the limp bones.
|
|
202
|
+
*/
|
|
203
|
+
get weight(): number;
|
|
204
|
+
set weight(value: number);
|
|
205
|
+
/** World position of the root bone's body (the hips) at the last physics step: where to put a character that gets up. */
|
|
206
|
+
get rootPosition(): [number, number, number];
|
|
207
|
+
/** The bones `limb` names and every bone under them; every bone without a `limb`. */
|
|
208
|
+
private limbBones;
|
|
209
|
+
/**
|
|
210
|
+
* Goes limp: each body becomes dynamic, keeps the velocity the animation gave it, and drives its bone. Bones that are
|
|
211
|
+
* limp already stay as they are.
|
|
212
|
+
* @param limb With a bone or a list of bones, only they and the bones under them go limp, hanging from the bones that
|
|
213
|
+
* keep animating: one arm for a hit, the spine for the whole upper body. Without it, every bone.
|
|
214
|
+
* @throws RangeError for a node that is not a bone of this ragdoll.
|
|
215
|
+
*/
|
|
216
|
+
activate(limb?: RagdollLimb): this;
|
|
217
|
+
/**
|
|
218
|
+
* Hands bones back to the animation: the bodies become kinematic again (following the animation), and the drawn
|
|
219
|
+
* pose blends from the ragdoll's last pose to the animation over `duration` seconds (default 0.3; 0 snaps). Bones that
|
|
220
|
+
* are not limp stay as they are.
|
|
221
|
+
* @param limb With a bone or a list of bones, only they and the bones under them. Without it, every bone.
|
|
222
|
+
* @throws RangeError for a node that is not a bone of this ragdoll.
|
|
223
|
+
*/
|
|
224
|
+
deactivate(duration?: number, limb?: RagdollLimb): this;
|
|
225
|
+
/** The part whose bone is `node`, or undefined. */
|
|
226
|
+
part(node: Mesh): RagdollPart | undefined;
|
|
227
|
+
setup(engine: Engine): void;
|
|
228
|
+
/**
|
|
229
|
+
* Runs each frame after the animation: reads the animated pose, sets the kinematic targets, and writes the blended
|
|
230
|
+
* ragdoll pose into the bones. The engine calls it; call it yourself only for a ragdoll that is not attached.
|
|
231
|
+
*/
|
|
232
|
+
onPreUpdate(delta: number): void;
|
|
233
|
+
/**
|
|
234
|
+
* Measures `bone.bind` from the pose the nodes hold now. Under an uneven scale a child's world rotation is not its
|
|
235
|
+
* parent's times its own, so the turn from one body to the next differs from the node's local rotation.
|
|
236
|
+
*/
|
|
237
|
+
private fit;
|
|
238
|
+
/** Reads `node`'s local transform into `out`, xyz then xyzw. */
|
|
239
|
+
private readLocal;
|
|
240
|
+
/**
|
|
241
|
+
* Takes the bone's local transform as its animated base pose, unless it is exactly what the ragdoll wrote last
|
|
242
|
+
* frame: then nothing animated it since, and the base it had stays.
|
|
243
|
+
*/
|
|
244
|
+
private track;
|
|
245
|
+
/**
|
|
246
|
+
* The physics pose of every bone, interpolated to the frame, as world poses, and of every limp bone as a local
|
|
247
|
+
* transform. A bone blending back keeps the local transform it left physics with.
|
|
248
|
+
*/
|
|
249
|
+
private physicsLocals;
|
|
250
|
+
/** Writes the animated pose blended toward the physics pose by its `weight` into every bone that has one. */
|
|
251
|
+
private write;
|
|
252
|
+
private beforeStep;
|
|
253
|
+
/**
|
|
254
|
+
* Swing and twist limits. Each cone joint's swing and twist are measured from its rest pose (swing-twist
|
|
255
|
+
* decomposition about the bone axis); where one is past its limit, an impulse between the two bodies stops the
|
|
256
|
+
* relative spin that drives it further out (an inelastic velocity constraint, E. Catto, "Iterative Dynamics with
|
|
257
|
+
* Temporal Coherence", GDC 2005), and the child turns back about its own origin, which is the joint, by a share of the
|
|
258
|
+
* error (constraint projection, T. Jakobsen, "Advanced Character Physics", GDC 2001). Turning about the joint keeps
|
|
259
|
+
* the joint together, and projecting the position adds no velocity, so a limit does not bounce.
|
|
260
|
+
*/
|
|
261
|
+
private limits;
|
|
262
|
+
/** Turns `bone`'s body by `angle` radians about the world `axis` through its origin (its joint). */
|
|
263
|
+
private turn;
|
|
264
|
+
/**
|
|
265
|
+
* One limit impulse about world `axis` (the direction that grows the error) between bone `i` and its parent `p`:
|
|
266
|
+
* it removes their relative spin along the axis when that spin drives the error up. Returns whether it acted.
|
|
267
|
+
*/
|
|
268
|
+
private push;
|
|
269
|
+
private inverseInertia;
|
|
270
|
+
private afterStep;
|
|
271
|
+
private live;
|
|
272
|
+
/** Removes every body and joint and leaves the bones in their last pose. Safe to call twice. */
|
|
273
|
+
dispose(): void;
|
|
274
|
+
[Symbol.dispose]: () => void;
|
|
275
|
+
}
|
|
276
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@margoengine/ragdoll",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@margoengine/ragdoll",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Ragdolls on Margo physics: capsule bodies from a skeleton, cone and hinge joint limits, kinematic hit boxes, going limp and blending back to the animation.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"margoengine",
|
|
7
|
+
"webgpu",
|
|
8
|
+
"3d",
|
|
9
|
+
"ragdoll",
|
|
10
|
+
"physics",
|
|
11
|
+
"animation"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://margoengine.com",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/scottcampbelldata/margoengine/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/scottcampbelldata/margoengine.git",
|
|
20
|
+
"directory": "packages/ragdoll"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"author": "Scott Campbell",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./powered": {
|
|
32
|
+
"types": "./dist/powered.d.ts",
|
|
33
|
+
"import": "./dist/powered.js"
|
|
34
|
+
},
|
|
35
|
+
"./getup": {
|
|
36
|
+
"types": "./dist/getup.d.ts",
|
|
37
|
+
"import": "./dist/getup.js"
|
|
38
|
+
},
|
|
39
|
+
"./hulls": {
|
|
40
|
+
"types": "./dist/hulls.d.ts",
|
|
41
|
+
"import": "./dist/hulls.js"
|
|
42
|
+
},
|
|
43
|
+
"./profile": {
|
|
44
|
+
"types": "./dist/profile.d.ts",
|
|
45
|
+
"import": "./dist/profile.js"
|
|
46
|
+
},
|
|
47
|
+
"./balance": {
|
|
48
|
+
"types": "./dist/balance.d.ts",
|
|
49
|
+
"import": "./dist/balance.js"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist",
|
|
54
|
+
"LICENSE",
|
|
55
|
+
"NOTICE"
|
|
56
|
+
],
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@margoengine/math": "^0.1.0"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"@margoengine/animator": "^0.1.0",
|
|
65
|
+
"@margoengine/core": "^0.1.0",
|
|
66
|
+
"@margoengine/physics": "^0.1.0",
|
|
67
|
+
"@margoengine/rigging": "^0.1.0"
|
|
68
|
+
},
|
|
69
|
+
"peerDependenciesMeta": {
|
|
70
|
+
"@margoengine/animator": {
|
|
71
|
+
"optional": true
|
|
72
|
+
},
|
|
73
|
+
"@margoengine/rigging": {
|
|
74
|
+
"optional": true
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"scripts": {
|
|
78
|
+
"build": "node ../../scripts/build-optional.js ragdoll",
|
|
79
|
+
"test": "npm run build && node --test tests/*.test.js",
|
|
80
|
+
"prepack": "npm run build"
|
|
81
|
+
}
|
|
82
|
+
}
|