@margoengine/ragdoll 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ import { conjugateQuaternion as e, multiplyQuaternions as t } from "@margoengine/math/quat";
2
+ //#region src/limits.ts
3
+ var n = Math.PI * 2;
4
+ function r(r, i) {
5
+ let a = r[0] * i[0] + r[1] * i[1] + r[2] * i[2], o = Math.hypot(a, r[3]);
6
+ if (o < 1e-9) return {
7
+ swing: [
8
+ r[0],
9
+ r[1],
10
+ r[2],
11
+ r[3]
12
+ ],
13
+ twist: 0
14
+ };
15
+ let s = [
16
+ i[0] * a / o,
17
+ i[1] * a / o,
18
+ i[2] * a / o,
19
+ r[3] / o
20
+ ], c = 2 * Math.atan2(a, r[3]);
21
+ return c > Math.PI ? c -= n : c < -Math.PI && (c += n), {
22
+ swing: t([
23
+ r[0],
24
+ r[1],
25
+ r[2],
26
+ r[3]
27
+ ], e(s)),
28
+ twist: c
29
+ };
30
+ }
31
+ var i = (e, t) => {
32
+ let n = Math.sin(t / 2);
33
+ return [
34
+ e[0] * n,
35
+ e[1] * n,
36
+ e[2] * n,
37
+ Math.cos(t / 2)
38
+ ];
39
+ };
40
+ function a(n, a) {
41
+ let o = n.spec;
42
+ if (o.type === "free") return a;
43
+ let s = n.rest, c = t(e(s), a);
44
+ if (c[3] < 0) for (let e = 0; e < 4; e++) c[e] = -c[e];
45
+ let { swing: l, twist: u } = r(c, n.axis), [d, f] = o.twist, p = o.swing, m = Math.hypot(l[0], l[1], l[2]), h = 2 * Math.atan2(m, Math.abs(l[3])), g = Math.min(f, Math.max(d, u));
46
+ if (h <= p && g === u) return a;
47
+ let _ = l[3] < 0 ? -1 : 1, v = h > p && m > 1e-9 ? i([
48
+ l[0] / m * _,
49
+ l[1] / m * _,
50
+ l[2] / m * _
51
+ ], p) : l;
52
+ return t(s, t(v, i(n.axis, g)));
53
+ }
54
+ //#endregion
55
+ export { a as n, r, i as t };
@@ -0,0 +1,12 @@
1
+ import type { RagdollPart } from './ragdoll.js';
2
+ /** The swing and twist of the unit quaternion `d` about the unit `axis`: `d` is swing * twist. */
3
+ export declare function swingTwist(d: ArrayLike<number>, axis: ArrayLike<number>): {
4
+ swing: number[];
5
+ twist: number;
6
+ };
7
+ export declare const aboutAxis: (axis: ArrayLike<number>, angle: number) => number[];
8
+ /**
9
+ * The rotation a joint can reach nearest `rotation` (a bone relative to its parent), inside its limits, so a motor
10
+ * never pulls against the limit it would have to pass. `part.spec` must be a cone or free joint.
11
+ */
12
+ export declare function insideLimits(part: RagdollPart, rotation: number[]): number[];
@@ -0,0 +1,145 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Mesh } from '@margoengine/core/extension';
3
+ import type { Ragdoll, RagdollPart } from './ragdoll.js';
4
+ /** Options of {@link addRagdollMotors}. Every field has a default. */
5
+ export interface RagdollMotorOptions {
6
+ /** How fast a motor closes on its target, as a natural frequency in hertz, above 0. Default 6. */
7
+ frequency?: number;
8
+ /** Damping ratio, above 0: 1 settles without overshoot, less rings. Default 1. */
9
+ damping?: number;
10
+ /** Share of the motors' pull, 0 (limp) to 1. Default 1. */
11
+ strength?: number;
12
+ /**
13
+ * The largest angular acceleration a motor may give the bones it carries, in rad/s², above 0. Each joint's torque cap
14
+ * is this times the inertia of its limb about the joint at the pose motors were added in. Default 400, what the hands
15
+ * and feet of a real walk clip need; less gives floppier limbs, and a hit stronger than the cap bends the joint.
16
+ */
17
+ maxAcceleration?: number;
18
+ /**
19
+ * Share of a spring from the world to the root bone, toward where the animation puts it, 0 to 1. Default 0: joint
20
+ * motors alone, so a character with no support falls while keeping its pose. Above 0 it stays up, and a hit knocks it
21
+ * away and it comes back. This is an outside force, not a muscle.
22
+ */
23
+ root?: number;
24
+ }
25
+ /** Options of {@link RagdollMotors.hit}. */
26
+ export interface RagdollHitOptions {
27
+ /** World point of the hit. Default the body's centre of mass. */
28
+ point?: readonly [number, number, number];
29
+ /** Share of motor strength the struck limb loses at once, 0 to 1. Default 0.8. */
30
+ weaken?: number;
31
+ /** Seconds the limb takes to get its strength back, 0 or more. Default 0.6. */
32
+ recover?: number;
33
+ }
34
+ /** The motors of a ragdoll ({@link addRagdollMotors}). */
35
+ export declare class RagdollMotors {
36
+ private readonly ragdoll;
37
+ /** Largest angle between a powered bone and the pose its motor drives it to at the last physics step, in radians. */
38
+ error: number;
39
+ private global;
40
+ private goal;
41
+ private rate;
42
+ private rootShare;
43
+ private readonly omega;
44
+ private readonly zeta;
45
+ private readonly parts;
46
+ /** Inertia of each limb about its joint, kg m²: from where its mass lies, and as a rod per body. */
47
+ private readonly limbs;
48
+ private readonly rods;
49
+ /** The inertia each motor's gains come from: the limb's, or the rod figure's for a foot on the floor, see {@link standing}. */
50
+ private readonly gains;
51
+ private readonly acceleration;
52
+ /** The limb's own share of the strength, set by {@link setStrength}. */
53
+ private readonly limb;
54
+ /** Share lost to a hit, fading at `heal` per second. */
55
+ private readonly lost;
56
+ private readonly heal;
57
+ private readonly running;
58
+ /** Each bone's share of the pull at the last step. */
59
+ private readonly shares;
60
+ /** Each bone's target when it last moved (xyzw, or the hinge angle first), and the spin that move gave it. */
61
+ private readonly last;
62
+ private readonly spin;
63
+ private readonly waited;
64
+ private readonly gap;
65
+ private readonly seen;
66
+ /** Targets set by {@link setTarget}, by bone index, in place of the animation's pose. */
67
+ private readonly targets;
68
+ private readonly off;
69
+ private disposed;
70
+ /** @internal use {@link addRagdollMotors}. */
71
+ constructor(ragdoll: Ragdoll, options: RagdollMotorOptions);
72
+ /**
73
+ * Skeletons put small bodies between big limbs (a chest node between two arms, a foot under a shin), and an iterative
74
+ * solver cannot hold a strong motor against a body hundreds of times lighter: on RiggedFigure the character flew apart.
75
+ * Each body gets the extra mass and inertia that keep it within MASS_RATIO and INERTIA_RATIO of what it carries, and
76
+ * `inertia` on top. This is what suits the solver, not a limb's true inertia, so it reckons with rods: the same ratio on the
77
+ * true figures shook RiggedFigure at 0.009 m/s at 20 passes, on 85 of 96 hull roundings.
78
+ * @internal for `@margoengine/ragdoll/balance`.
79
+ */
80
+ addSteadying(part: RagdollPart, inertia?: number): void;
81
+ /** True when bone `i` is `top` or hangs under it. Parents come first in the list. */
82
+ private within;
83
+ private live;
84
+ private index;
85
+ /** Inertia of `bone` and all that hangs from it about its joint, kg m², from where their mass lay when the motors were added. */
86
+ limbInertia(bone: Mesh | RagdollPart): number;
87
+ /** Share of the motors' pull, 0 (limp) to 1. Setting it stops a running {@link fade}. */
88
+ get strength(): number;
89
+ set strength(value: number);
90
+ /** Share of the world spring on the root bone, 0 to 1 (see {@link RagdollMotorOptions.root}). */
91
+ get root(): number;
92
+ set root(value: number);
93
+ /** Moves {@link strength} to `to` over `seconds`: to 0 to go limp gradually, to 1 to get up to speed. */
94
+ fade(to: number, seconds: number): this;
95
+ /**
96
+ * Drives `bone` to `rotation`, relative to its parent bone, in place of the pose the animation gives it: a controller
97
+ * sets a leg from where its foot must go. Pass `null` to go back to the animation.
98
+ * @throws RangeError for a node that is not a bone of this ragdoll.
99
+ */
100
+ setTarget(bone: Mesh | RagdollPart, rotation: readonly number[] | null): this;
101
+ /** Sets the strength share of `bone` and every bone under it, 0 to 1: a limp arm on a powered body. */
102
+ setStrength(bone: Mesh | RagdollPart, value: number): this;
103
+ /**
104
+ * Tells the motor of `bone` whether the floor carries that bone. Inertia sets a motor's gains so that it can hold a limb
105
+ * against its weight, and a foot on the floor hangs from nothing: the floor holds it, and what its ankle carries is the
106
+ * balance's. The limb's own inertia there slid Knight's feet 0.003 m, and a share of it growing with the foot's tilt shook
107
+ * Mage at 0.041 m/s at 8 passes, so a standing foot keeps the rod figure's gains.
108
+ * @internal for `@margoengine/ragdoll/balance`.
109
+ */
110
+ standing(bone: Mesh | RagdollPart, on: boolean): void;
111
+ /**
112
+ * A hit: applies `impulse` (N s, world) to `bone` and weakens it, the bones under it and the bones above it up to
113
+ * the root, so the blow travels through the body before the motors take the pose back.
114
+ */
115
+ hit(bone: Mesh | RagdollPart, impulse: readonly [number, number, number], options?: RagdollHitOptions): this;
116
+ private step;
117
+ /**
118
+ * The spin of the target itself, so a motor tracks a moving pose without lagging it by its damping. The animation moves
119
+ * once a frame and physics steps at its own rate, so the spin is what changed over the steps since it last moved.
120
+ */
121
+ private ballSpeed;
122
+ private moved;
123
+ /** A target that has stood still for longer than its last move took has stopped: its spin is 0. */
124
+ private settle;
125
+ private hingeSpeed;
126
+ /** Angle between bone `i` and `target` (relative to its parent). */
127
+ private apart;
128
+ /** The implicit spring from the world to the root bone. */
129
+ private pullRoot;
130
+ /** Turns the motors off and leaves the ragdoll as it was. Safe to call twice. */
131
+ dispose(): void;
132
+ [Symbol.dispose]: () => void;
133
+ }
134
+ /**
135
+ * Adds joint motors to a ragdoll: while a bone is physical (`activate()`), its joint pulls it toward the pose the
136
+ * animation gives it, inside the joint's limits, so the character keeps moving, takes hits and recovers instead of
137
+ * going limp. Add it to a ragdoll built in a standing pose (each joint's torque cap comes from the limb's inertia).
138
+ * @example
139
+ * const ragdoll = Ragdoll.attach(physics, { root: hips });
140
+ * const motors = addRagdollMotors(ragdoll, { root: 1 });
141
+ * ragdoll.activate();
142
+ * motors.hit(ragdoll.bones[1], [0, 0, -60]); // stumbles, then the animation has it back
143
+ * @throws TypeError without a ragdoll, RangeError for an option out of range.
144
+ */
145
+ export declare function addRagdollMotors(ragdoll: Ragdoll, options?: RagdollMotorOptions): RagdollMotors;
@@ -0,0 +1,246 @@
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";
5
+ //#region src/powered.ts
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([
13
+ e.animated[3],
14
+ e.animated[4],
15
+ e.animated[5],
16
+ e.animated[6]
17
+ ], a(e.bind)), g = class {
18
+ constructor(e, t) {
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");
20
+ let n = this.parts = e.bones, r = n.length, i = n.map((e) => {
21
+ let t = c(e.axis, e.body.rotation), n = e.body.position;
22
+ return [
23
+ 0,
24
+ 1,
25
+ 2
26
+ ].map((r) => n[r] + t[r] * e.length / 2);
27
+ }), a = n.map((e) => {
28
+ let t = c(e.body.centerOfMass, e.body.rotation), n = e.body.position;
29
+ return [
30
+ 0,
31
+ 1,
32
+ 2
33
+ ].map((e) => n[e] + t[e]);
34
+ });
35
+ this.limbs = new Float64Array(r), this.rods = new Float64Array(r), this.limb = new Float64Array(r).fill(1), this.lost = new Float64Array(r), this.heal = new Float64Array(r), this.running = new Uint8Array(r), this.shares = new Float64Array(r), this.last = new Float64Array(r * 4), this.spin = new Float64Array(r * 3), this.waited = new Float64Array(r), this.gap = new Float64Array(r), this.seen = new Uint8Array(r);
36
+ let o = (e, t, i) => {
37
+ let a = n[e].body.position, o = 0;
38
+ for (let s = e; s < r; s++) {
39
+ if (!this.within(s, e)) continue;
40
+ let r = n[s], c = Math.hypot(t[s][0] - a[0], t[s][1] - a[1], t[s][2] - a[2]);
41
+ o += r.body.mass * c * c + i(r);
42
+ }
43
+ return o;
44
+ }, s = (e) => {
45
+ let [t, n, r] = e.body.principalInertia;
46
+ return (t + n + r) / 3;
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);
49
+ this.gains = this.limbs.slice();
50
+ for (let e of n) this.addSteadying(e);
51
+ this.off = e.physics.on("beforestep", (e) => this.step(e));
52
+ }
53
+ addSteadying(e, t = 0) {
54
+ let n = this.parts, r = n.indexOf(e), i = e.parent === -1 ? 0 : n[e.parent].mass, a = 0;
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]));
56
+ let o = Math.max(0, i / d - e.mass), s = t + a / f;
57
+ e.body.setAdditionalMass(o, [
58
+ s,
59
+ s,
60
+ s
61
+ ]);
62
+ }
63
+ within(e, t) {
64
+ for (; e > t;) e = this.parts[e].parent;
65
+ return e === t;
66
+ }
67
+ live() {
68
+ if (this.disposed) throw new i("disposed", "Ragdoll motors are disposed; call addRagdollMotors again.");
69
+ return this;
70
+ }
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;
75
+ }
76
+ limbInertia(e) {
77
+ return this.limbs[this.index(e)];
78
+ }
79
+ get strength() {
80
+ return this.global;
81
+ }
82
+ set strength(e) {
83
+ this.global = this.goal = p(e, "Ragdoll motor strength"), this.rate = 0;
84
+ }
85
+ get root() {
86
+ return this.rootShare;
87
+ }
88
+ set root(e) {
89
+ this.rootShare = p(e, "Ragdoll motor root");
90
+ }
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;
94
+ }
95
+ setTarget(e, t) {
96
+ let n = this.index(e);
97
+ return t ? this.targets.set(n, t) : this.targets.delete(n), this;
98
+ }
99
+ setStrength(e, t) {
100
+ this.live(), p(t, "Ragdoll motor strength");
101
+ let n = this.index(e);
102
+ for (let e = n; e < this.parts.length; e++) this.within(e, n) && (this.limb[e] = t);
103
+ return this;
104
+ }
105
+ standing(e, t) {
106
+ let n = this.index(e);
107
+ this.gains[n] = (t ? this.rods : this.limbs)[n];
108
+ }
109
+ hit(t, n, r = {}) {
110
+ this.live();
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;
115
+ };
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;
119
+ }
120
+ step(e) {
121
+ if (this.disposed || !this.parts[0].body.valid) return;
122
+ if (this.rate > 0) {
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);
125
+ }
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));
130
+ let _ = this.shares[p] = this.global * this.limb[p] * (1 - l[p]);
131
+ if (m.body.type !== "dynamic" || _ <= 0) {
132
+ d[p] && g.clearMotor(), d[p] = 0;
133
+ continue;
134
+ }
135
+ d[p] = 1;
136
+ let v = this.gains[p], y = this.targets.get(p), b = y ? [
137
+ y[0],
138
+ y[1],
139
+ y[2],
140
+ y[3]
141
+ ] : h(m), x = {
142
+ model: "force",
143
+ stiffness: v * s * s * _,
144
+ damping: 2 * c * v * s * Math.sqrt(_),
145
+ maxForce: v * this.acceleration * _
146
+ }, S = m.spec;
147
+ if (S.type === "hinge") {
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));
149
+ g.setMotor({
150
+ targetPosition: l,
151
+ targetVelocity: this.hingeSpeed(p, l, e),
152
+ ...x
153
+ }), f = Math.max(f, this.apart(p, o(m.rest, r(S.axis, l))));
154
+ } else {
155
+ let n = t(m, b);
156
+ g.setRotationMotor(n, {
157
+ ...x,
158
+ targetVelocity: this.ballSpeed(p, n, e)
159
+ }), f = Math.max(f, this.apart(p, n));
160
+ }
161
+ }
162
+ this.error = f, this.rootShare > 0 && i[0].body.type === "dynamic" && this.pullRoot(e);
163
+ }
164
+ ballSpeed(e, t, n) {
165
+ let r = this.last.subarray(e * 4, e * 4 + 4), i = this.spin.subarray(e * 3, e * 3 + 3);
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);
167
+ else {
168
+ if (this.seen[e]) {
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];
171
+ }
172
+ r.set(t), this.moved(e);
173
+ }
174
+ return [
175
+ i[0],
176
+ i[1],
177
+ i[2]
178
+ ];
179
+ }
180
+ moved(e) {
181
+ this.seen[e] = 1, this.gap[e] = this.waited[e], this.waited[e] = 0;
182
+ }
183
+ settle(e, t) {
184
+ this.waited[e] > this.gap[e] + t / 2 && this.spin.fill(0, e * 3, e * 3 + 3);
185
+ }
186
+ hingeSpeed(e, t, n) {
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];
188
+ }
189
+ apart(e, t) {
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));
192
+ }
193
+ pullRoot(e) {
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);
199
+ if (r !== -1) {
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 = [
202
+ 0,
203
+ 1,
204
+ 2
205
+ ].map((t) => e[t] * i[0] + e[t + 4] * i[1] + e[t + 8] * i[2] + e[t + 12]);
206
+ }
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 = [
208
+ 0,
209
+ 1,
210
+ 2
211
+ ].map((e) => p * (d * d * (c[e] - m[e]) - f * g[e])), C = [
212
+ 0,
213
+ 1,
214
+ 2
215
+ ].map((e) => p * (d * d * v[e] * x - f * _[e]));
216
+ for (let e = 0; e < this.parts.length; e++) {
217
+ let t = this.parts[e], n = e ? this.shares[e] : 1;
218
+ if (n <= 0 || t.body.type !== "dynamic") continue;
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;
220
+ t.body.setLinearVelocity([
221
+ s[0] + n * (S[0] + C[1] * o - C[2] * a),
222
+ s[1] + n * (S[1] + C[2] * i - C[0] * o),
223
+ s[2] + n * (S[2] + C[0] * a - C[1] * i)
224
+ ]), t.body.setAngularVelocity([
225
+ c[0] + n * C[0],
226
+ c[1] + n * C[1],
227
+ c[2] + n * C[2]
228
+ ]);
229
+ }
230
+ }
231
+ dispose() {
232
+ if (!this.disposed && (this.disposed = !0, this.off(), this.parts[0].body.valid)) {
233
+ for (let e of this.parts) e.body.setAdditionalMass(0);
234
+ for (let e = 1; e < this.parts.length; e++) this.running[e] && this.parts[e].joint.clearMotor();
235
+ }
236
+ }
237
+ [Symbol.dispose ?? Symbol()]() {
238
+ this.dispose();
239
+ }
240
+ };
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 ?? {});
244
+ }
245
+ //#endregion
246
+ export { g as RagdollMotors, _ as addRagdollMotors };
@@ -0,0 +1,62 @@
1
+ import type { Mesh } from '@margoengine/core/extension';
2
+ // @ts-ignore optional peer: its types apply when it is installed
3
+ import { type HumanoidBone } from '@margoengine/rigging/retarget';
4
+ import type { Ragdoll, RagdollJoint, RagdollOptions, RagdollSettings, RagdollShape } from './ragdoll.js';
5
+ /** One bone of a {@link RagdollProfile}. Everything but `name` falls back to the ragdoll's defaults. */
6
+ export interface RagdollProfileBone {
7
+ /** The node's name in the skeleton. */
8
+ name: string;
9
+ /** Joint to the parent bone; left out on the root bone. */
10
+ joint?: RagdollJoint;
11
+ /** Bone length in world units. */
12
+ length?: number;
13
+ /** Capsule radius in world units. */
14
+ radius?: number;
15
+ /** Mass in kilograms. */
16
+ mass?: number;
17
+ /** The collider, in the bone's own frame. */
18
+ shape?: RagdollShape;
19
+ }
20
+ /** A ragdoll as data: `JSON.stringify` it, ship it, and bind it to a skeleton with {@link ragdollOptions}. */
21
+ export interface RagdollProfile {
22
+ /** Always `'margoengine-ragdoll'`. */
23
+ format: string;
24
+ /** Format version, 1. */
25
+ version: number;
26
+ /** Body settings shared by every bone. */
27
+ settings?: Partial<RagdollSettings>;
28
+ /** The bones. */
29
+ bones: RagdollProfileBone[];
30
+ }
31
+ /** Options of {@link humanoidProfile}. */
32
+ export interface HumanoidProfileOptions {
33
+ /** Whole body mass in kilograms. Default 70. */
34
+ mass?: number;
35
+ /** Which node is which bone, for a rig whose names no preset knows. Default: detected from the node names. */
36
+ bones?: Partial<Record<HumanoidBone, Mesh>>;
37
+ }
38
+ /**
39
+ * The ragdoll as a profile, every value as built: tune it, save it, and {@link ragdollOptions} builds the same one.
40
+ * @example
41
+ * localStorage.setItem('hero', JSON.stringify(saveRagdollProfile(ragdoll)));
42
+ * @throws TypeError without a ragdoll, RangeError for a bone with no name or two bones with one name.
43
+ */
44
+ export declare function saveRagdollProfile(ragdoll: Ragdoll): RagdollProfile;
45
+ /**
46
+ * The options that build a profile's ragdoll on a skeleton, finding each bone among `nodes` by name.
47
+ * @example
48
+ * const ragdoll = Ragdoll.attach(physics, ragdollOptions(JSON.parse(saved), model.nodes));
49
+ * @param profile A profile, parsed from JSON or made by {@link humanoidProfile}.
50
+ * @param nodes The skeleton's nodes, such as `model.nodes`.
51
+ * @throws TypeError without a profile or nodes, RangeError for another format, a newer version or a bone not in `nodes`.
52
+ */
53
+ export declare function ragdollOptions(profile: RagdollProfile, nodes: readonly Mesh[]): RagdollOptions;
54
+ /**
55
+ * A profile for a humanoid in a T or A pose, as a place to start: anatomical joint ranges, knees and elbows as hinges
56
+ * that bend the right way, and each bone's share of `mass` from published body segment tables. Build the ragdoll in the
57
+ * pose this reads.
58
+ * @example
59
+ * const ragdoll = Ragdoll.attach(physics, ragdollOptions(humanoidProfile(model.nodes), model.nodes));
60
+ * @throws TypeError without nodes, RangeError for a bad mass or a rig missing hips, a head, or an arm or leg bone.
61
+ */
62
+ export declare function humanoidProfile(nodes: readonly Mesh[], options?: HumanoidProfileOptions): RagdollProfile;