@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,252 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Mesh } from '@margoengine/core/extension';
3
+ import type { RagdollMotors } from './powered.js';
4
+ import type { Ragdoll, RagdollPart } from './ragdoll.js';
5
+ type Vec3 = [number, number, number];
6
+ /** Options of {@link addRagdollBalance}. */
7
+ export interface RagdollBalanceOptions {
8
+ /** The two foot bones. Each foot's parent bone must be a knee hinge, as `humanoidProfile` builds. */
9
+ feet: readonly [Mesh | RagdollPart, Mesh | RagdollPart];
10
+ /** Seconds a step takes. Default 0.28. */
11
+ stepTime?: number;
12
+ /** How high a foot lifts, as a share of the leg's length. Default 0.15. */
13
+ stepHeight?: number;
14
+ /** How far the capture point may leave the feet before a step, as a share of the leg's length. Default 0.2. */
15
+ stepTrigger?: number;
16
+ /** Hip height held, as a share of the height the character was added at. Default 0.97: knees a little bent. */
17
+ stance?: number;
18
+ /** Share of the upper body's weight an outside force carries, 0 to 1. Default 0: it stands on its own legs. */
19
+ carry?: number;
20
+ /** Frequency in rad/s of the turn that holds the hips upright, answered through the legs by the ground. Default 10. */
21
+ upright?: number;
22
+ /** Largest turn that gives, in rad/s². A harder blow than this can hold knocks the character over. Default 40. */
23
+ maxUpright?: number;
24
+ /** Tilt of the hips in radians past which the character has fallen and every assist stops. Default 0.8. */
25
+ fallTilt?: number;
26
+ /** World gravity along up, in m/s². Default 9.81. */
27
+ gravity?: number;
28
+ }
29
+ /** What a balancing character is doing. */
30
+ export type RagdollBalanceState = 'standing' | 'stepping' | 'fallen';
31
+ /** A character keeping its feet ({@link addRagdollBalance}). */
32
+ export declare class RagdollBalance {
33
+ private readonly ragdoll;
34
+ private readonly motors;
35
+ /** What the character is doing. */
36
+ state: RagdollBalanceState;
37
+ /** Steps started so far. */
38
+ steps: number;
39
+ /** Where a step now would stop the fall, in the world, on the ground. */
40
+ capture: Vec3;
41
+ /** Share of the whole body's weight the `carry` force gave at the last physics step: 0 unless `carry` is set. */
42
+ carried: number;
43
+ private readonly legs;
44
+ private readonly upper;
45
+ private readonly standing;
46
+ private readonly up;
47
+ private readonly forward;
48
+ private readonly total;
49
+ /** Height of a planted ankle. */
50
+ private floor;
51
+ /** From the right ankle to the left at rest. */
52
+ private readonly stanceLine;
53
+ private readonly upperMass;
54
+ /** Each bone's centre of mass in its own frame. */
55
+ private readonly massPoints;
56
+ private readonly upperShare;
57
+ private readonly height;
58
+ private readonly natural;
59
+ private readonly stepTime;
60
+ private readonly lift;
61
+ private readonly trigger;
62
+ private readonly stance;
63
+ private readonly carry;
64
+ private readonly upright;
65
+ private readonly maxUpright;
66
+ private readonly fallTilt;
67
+ private readonly gravity;
68
+ private readonly hipsOverFeet;
69
+ private readonly off;
70
+ private readonly offForces;
71
+ private disposed;
72
+ /** How far through {@link SETTLE_TIME}, 0 to 1. */
73
+ private settled;
74
+ private stanceNow;
75
+ /** The height the hips are held to over the ankles at this step: `stance`, or lower while a step needs it, see {@link control}. */
76
+ private held;
77
+ /** False until the first step, when added inertia takes effect: see {@link control}. */
78
+ private primed;
79
+ /** Each bone's force and torque for this step, xyz each, and what the bodies hold from the last. */
80
+ private readonly wanted;
81
+ private readonly given;
82
+ /** The feet's colliders as they came, for {@link dispose}. */
83
+ private readonly soles;
84
+ /** Where the hips stood at the last step, and how they moved to get there: see {@link readHips}. */
85
+ private hipsWere;
86
+ private hipsSpeed;
87
+ private hipsSpin;
88
+ /** @internal use {@link addRagdollBalance}. */
89
+ constructor(ragdoll: Ragdoll, motors: RagdollMotors, options: RagdollBalanceOptions);
90
+ /** Radians the hips lean from how they stood. */
91
+ get tilt(): number;
92
+ private step;
93
+ /**
94
+ * How the hips moved over the last step, from where they went. The velocity a body reports at the end of a step holds what
95
+ * the solver's last pass left in it: Seed-san's hips reported 0.02 m/s one way while they moved 0.008 m/s the other, and the
96
+ * height's spring rang on that at half the frame rate.
97
+ */
98
+ private readHips;
99
+ /**
100
+ * Hands the bodies what this step wants as forces. A force goes through every solver pass; an impulse would land before the
101
+ * first, where one pass's share of friction and of the sole's weight must take a whole step's kick, so more passes meant
102
+ * less hold and the feet crept at the same speed on every figure. Only the change is added: a force stays until taken
103
+ * off, and anyone else's stays on the body. A bone that is not dynamic holds its force out of reach, so it is cleared.
104
+ */
105
+ private settle;
106
+ /** Adds to what bone `part` is given this step, N and N m. */
107
+ private give;
108
+ private control;
109
+ /** The hip joint to ankle length at its longest as {@link driveLeg} drives it: 0.999 of thigh and shin, or as the knee's limit allows. */
110
+ private longest;
111
+ /** The hips' height over the ankles at which `leg`, its hip joint over `hips` across the ground, reaches `foot` at its {@link longest}. */
112
+ private reachHeight;
113
+ /** How far a step may reach from the stance foot `leg`: {@link LONGEST_STEP} of the stature its hip joint over its sole gives. */
114
+ private stepLength;
115
+ /** Where over a planted foot the body is held: the ankle, or forward of it until {@link STANCE_HEEL} of the sole is behind. */
116
+ private aim;
117
+ /** The centre of mass of the whole body, and its velocity. */
118
+ private massCentre;
119
+ /**
120
+ * A stance knee and ankle have their torques from {@link carryWeight}. A solver motor on a joint the body's weight goes through
121
+ * slides the feet along the floor, a body at rest included, in proportion to its damping and stiffness: RiggedFigure 0.045 m
122
+ * in five seconds through its knees, Knight 0.003 m through its ankles. So a stance knee has only a spring, with no damper
123
+ * and growing with how far it is off its bend, and a stance ankle keeps the rod figure's gains, see
124
+ * {@link RagdollMotors.standing}. Without the spring a thrown leg lands loose and the character stumbles on. A swinging foot
125
+ * gets a motor that keeps it level. The listeners run in the order they were added, so these come after the rod motors'
126
+ * own step and replace it.
127
+ */
128
+ private holdJoints;
129
+ /**
130
+ * The swing foot's motor target, relative to its shin: the sole level, as SIMBICON keeps a swing foot parallel to the ground
131
+ * (K. Yin, K. Loken, M. van de Panne, SIGGRAPH 2007), heading where the hips head now, and inside the ankle's limits as every
132
+ * motor target is. The foot's rest pose in the world was level but kept the heading the character stood with, so once the
133
+ * body turned about the vertical it asked the ankle for more twist than it has: on a joint whose limits hold, the motor and
134
+ * the limit pulled against each other through the shin and turned the leg and the body with them.
135
+ */
136
+ private swingFoot;
137
+ /**
138
+ * A foot level on the ground, heading as `turned` heads now against `from` (the foot's own rest by default, so a planted foot
139
+ * keeps the heading it has), relative to its shin and inside the ankle's limits.
140
+ */
141
+ private levelFoot;
142
+ /** The hinge angle the knee stands at. */
143
+ private kneeAngle;
144
+ /** True for a bone of a stance leg, which {@link carryWeight} holds. */
145
+ private planted;
146
+ /** How far the sole of a foot reaches from the ankle: the points of its collider near its lowest point. */
147
+ private soleOf;
148
+ /**
149
+ * Gives a hull foot a sole of four corners on one plane, the box of what {@link soleOf} found, and keeps the rest of the
150
+ * hull. A hull fitted to a boot has a sole of many points a few millimetres apart in height, and a contact keeps four of
151
+ * them for the solver: which four changes with the slightest turn, so the foot rocked and dropped on a point left out,
152
+ * and the body shook. Four corners are all kept, and they are the sole the ankle torques reckon with.
153
+ */
154
+ private flatten;
155
+ /**
156
+ * Carries the body through the legs: the force and turn the stance legs must give at the hips to hold them at their
157
+ * height, the centre of mass over the feet and the upper body upright, as joint torques by the transpose of the leg's
158
+ * Jacobian (J. Pratt et al., "Virtual Model Control", 1997), each an equal and opposite pair on the two bones of its joint.
159
+ */
160
+ private carryWeight;
161
+ /** The turn this step gives the upper body toward how it stood, about its own centre of mass. */
162
+ private uprightTurn;
163
+ /**
164
+ * How far the sole reaches from the ankle across the ground as the foot lies now: its four corners turned with the foot. A foot
165
+ * tipped onto its heel has the heel nearer under the ankle than flat (back cos a - h sin a, h the ankle over the sole), so the
166
+ * turn its heel can answer is that much less. Reckoned flat, the bound let a tipped foot be turned further onto its heel than
167
+ * the heel could hold, and a stance foot through a step had no bound at all: Seed-san's stance foot rolled to 0.9 rad on its
168
+ * heel through one step (KB2208, fault 3).
169
+ */
170
+ private soleNow;
171
+ /** The capture point: the centre of mass plus its velocity over the pendulum's natural frequency, on the ground. */
172
+ private capturePoint;
173
+ /** How far the capture point lies from the line between two planted feet, across the ground. */
174
+ private outside;
175
+ /** True when the capture point lies inside the sole of one planted foot, across the ground. */
176
+ private over;
177
+ /**
178
+ * Brings the feet back beside each other once a step has caught the body. With no step called for and the capture point
179
+ * inside one planted sole, that foot alone stops the fall (zero-step capturable: T. Koolen et al., "Capturability-based
180
+ * analysis and control of legged locomotion, Part 1", IJRR 2012), so the other foot is free: if it stands farther than
181
+ * `stepTrigger` from its rest place beside that one, it steps there, and the stance is two feet wide again for the next push.
182
+ * A foot the capture point is also over, carrying more of the weight, is the one that stays. With the step-time bound of
183
+ * {@link stepDuration}, the KB2208 speed bands fall fewer and no band falls more (docs/known-bugs.md).
184
+ */
185
+ private gather;
186
+ /**
187
+ * The foot to carry the weight over before the feet are gathered, or null. {@link gather} frees a foot once the other alone
188
+ * stops the fall, the capture point inside its sole (zero-step capturable: T. Koolen et al., IJRR 2012). A body held at the
189
+ * midpoint of two feet whose soles do not reach it never meets that: no step is called for, the point being near the line
190
+ * between the feet, and none is gathered. Holding it over the stood-on point ({@link aim}) of the foot nearer the capture point
191
+ * is the least that does. It acts only with both feet down, the capture point over neither sole and the feet farther than
192
+ * `stepTrigger` from their rest stance, the measure `gather` uses.
193
+ *
194
+ * It is gated on a foot whose heel is shorter than {@link STANCE_HEEL} of its sole, the test `aim` already makes, so it acts
195
+ * for Seed-san alone: a short heel cannot hold the capture point fore and aft. On every other figure the same carry costs
196
+ * steps and falls, since a landed foot that bears no load still skates. The gate is a special case until the follow-through
197
+ * for every figure lifts it (KB-RAGDOLL-FOLLOWTHROUGH1, docs/known-bugs.md).
198
+ */
199
+ private carryOver;
200
+ /**
201
+ * True when the capture point lies outside the base of support, the convex hull of both planted soles across the ground.
202
+ * With both feet down the body is zero-step capturable only while the capture point lies in the base of support (J. Rebula,
203
+ * T. Koolen, T. de Boer, J. Pratt, draft of "Capturability-based analysis and control of legged locomotion", Part 1, March
204
+ * 2011, Corollaries 2 and 3, in T. Koolen, MSc thesis, TU Delft, 2011), and the ankle strategy holds only that long (B.
205
+ * Stephens, "Push recovery control for force-controlled humanoid robots", PhD thesis, CMU-RI-TR-11-15, 2011, eq. 3.15); past
206
+ * it a step is the one way back. Without this the step waited on `stepTrigger` from the line between the feet: Seed-san
207
+ * pushed backward at 0.4 m/s had its capture point behind both heels from frame 1 and stepped at frame 35, its trunk let go.
208
+ */
209
+ private offSupport;
210
+ private startStep;
211
+ /**
212
+ * How long this step may take. The capture point runs away from the stance foot as e^(omega t) (J. Pratt et al., "Capture
213
+ * Point: A Step toward Humanoid Push Recovery", Humanoids 2006; J. Englsberger et al., "Bipedal walking control based on
214
+ * Capture Point dynamics", IROS 2011), so a step that takes `stepTime` lands where the capture point has gone, and after a
215
+ * hard push that is past where the leg reaches: the stance foot is then dragged off the ground and the body has nothing
216
+ * under it. The step takes no longer than the capture point needs to run from d, its distance from the stance foot now, to the
217
+ * edge of the step's reach, ln(reach / d) / omega, as step timing adaptation chooses it (M. Khadiv, A. Herzog, S. A. A. Moosavian,
218
+ * L. Righetti, "Walking Control Based on Step Timing Adaptation", IEEE T-RO 2020), and no less than half a natural period.
219
+ * Nor longer than the capture point takes to run past the sole put down for it: the step aims where the point is now, and
220
+ * once the point lies e outside the stance sole it runs away from that sole's edge as e e^(omega t), so it has crossed the
221
+ * m the new sole reaches beyond its ankle that way after ln(1 + m / e) / omega.
222
+ */
223
+ private stepDuration;
224
+ /** True when a planted foot's ankle stands well above the ground: a push lifted it, and it carries nothing. */
225
+ private lifted;
226
+ /** Where a step of `leg` lands: beside the capture point by half the rest stance, on the foot's own side. */
227
+ private landing;
228
+ /** Sets a leg's motors so its foot reaches the plant or the swing from `hips`, where the hips should be. */
229
+ private driveLeg;
230
+ /** The outside help when `carry` is set: a capped force under the upper body, moved as one rigid body. */
231
+ private assist;
232
+ private fall;
233
+ /** Stands again from where the feet are, after a get-up or a teleport. */
234
+ reset(): this;
235
+ /** Stops balancing and hands the legs back to the animation's pose. Safe to call twice. */
236
+ dispose(): void;
237
+ [Symbol.dispose]: () => void;
238
+ }
239
+ /**
240
+ * Keeps a powered ragdoll on its feet: it stands, and steps to where its momentum says it must when pushed. Add it with the
241
+ * character standing still in the pose to hold, before `activate`. It stands on its own legs: every torque is a pair inside
242
+ * the body, the ground answers within the sole, and a hard enough blow knocks it over. It adds inertia to the feet and shins
243
+ * and solver passes to the character's island and gives a hull foot a flat sole of four corners, and `dispose` takes them away.
244
+ * @example
245
+ * const motors = addRagdollMotors(ragdoll, { frequency: 8 });
246
+ * const balance = addRagdollBalance(ragdoll, motors, { feet: [leftFoot, rightFoot] });
247
+ * ragdoll.activate();
248
+ * motors.hit(chest, [0, 0, -40]); // it stumbles and catches itself; balance.steps counts how
249
+ * @throws TypeError without a ragdoll, motors or two feet, RangeError for a foot with no knee hinge or an option out of range.
250
+ */
251
+ export declare function addRagdollBalance(ragdoll: Ragdoll, motors: RagdollMotors, options: RagdollBalanceOptions): RagdollBalance;
252
+ export {};