Docs / Characters
Moving a figure
FigureMotion sits between "how is the body moving" and "pose the skeleton": each step it says which clips to play, how far into each, and how much of each. It eases one clip into the next, keeps every stride on one phase, plays a jump from take-off and lays one-shots such as a swing or a flinch over the lot. StrideAlignment measures, once per model, where each gait's stride lines up with the others', so those blends are blends of one stride.
Where it sits
Skeletons and clips covers the pieces underneath: LocomotionBlend.choose(airborne:rising:groundSpeed:heading:clips:) decides which clips a body plays, and StrideClock keeps the stride clips on one phase. FigureMotion is what a game ends up writing around them: the easing, the clocks for the jump and the loops, the one-shots. Use it for every figure, the player and everyone beside them, so nobody walks by a second rule. Turning a clip's name into a pose stays with your game.
Each step
Make one per figure, and give each a different clock so a crowd does not idle in step:
var motion = FigureMotion(clock: .random(in: 0..<2)) // easing: 0.1 by default
Each step, describe the body and ask what to play:
// your game let plays = motion.advance(by: TimeInterval(deltaTime), body: .init(airborne: !controller.isGrounded, rising: controller.verticalVelocity > 0, groundSpeed: speed, heading: heading), oneShots: shots, clips: clips, duration: { name in clipsByName[name].map { TimeInterval($0.duration) } }, strideOffsets: strideOffsets)
FigureMotion.Body is what the body is doing this frame: airborne, rising, groundSpeed, heading (the way it travels against the way it faces, radians, positive to the left, .pi backing away) and backpedaling. clips is a LocomotionBlend.Clips, and it may change from one frame to the next, standing to crouched. duration is each clip's length on this model, nil for a clip it lacks; a clip with no length is left out of the result.
The result is an array of FigureMotion.Play, each a name, a time in seconds and a weight. Keep one AnimationPlayhead per clip, made once, and mix them:
// your game var layers: [(playhead: AnimationPlayhead, weight: Float)] = [] for play in plays { guard var playhead = playheads[play.name] else { continue } playhead.time = Float(play.time) layers.append((playhead, play.weight)) } pose.set(mixing: layers)
set(mixing:) treats the weights as relative, which matters here: while clips are easing, the weights need not sum to one.
What it decides
- Which clips.
LocomotionBlendchooses, one gait at a time: idle, the carrying gait and its directions, or the rise or the fall. - The hand-over. Every clip's weight eases toward what the choice asked for, with a half-life of
easingseconds. A tenth of a second covers setting off, the top of a jump and a landing without making the states feel vague. A clip eased below a trace is let go.easing: 0cuts. - The stride.
walkPhase, 0 to 1, advances at the blend'spaceover the carrying gait's length, so the feet keep pace with the ground speed. A stride taken up again starts at 0, a foot on the ground, rather than wherever the last one stopped;StrideClockon its own holds instead. Withbackpedalingset, for a gait that has no backward clip, the forward stride runs in reverse. Every stride clip is sampled at the shared phase plus its offset, including the old clip set's strides while they ease out. - The jump and the fall. Both run from the moment the feet left the ground. The jump holds its last frame; the fall loops.
- Everything else loops on the figure's own
clock.
clock is animation time, not wall time: whatever you advance it by. A step scaled down for a hit-stop slows the stride, the jump and the idle together.
One-shots
A FigureMotion.OneShot is a clip, its age in seconds and a weight, 0 to 1. Each one takes its weight from everything under it, in order: a swing at 1 under a flinch at 0.75 leaves the swing a quarter. It plays one to one at its age and holds its last frame rather than looping. One-shots are not eased: they bring their own envelope, which you own. envelope(at:lasting:fadeIn:fadeOut:) is the usual one:
// your game let weight = FigureMotion.OneShot.envelope(at: age, lasting: 0.37, fadeIn: 0.03, fadeOut: 0.15) let shots = weight > 0 ? [FigureMotion.OneShot(clip: class="p">"Swing", age: age, weight: weight)] : []
A blow should start now and can hand back gently, so the fade-in is usually much shorter than the fade-out. A one-shot overrides the whole body; for an upper body swinging over running legs, lay the clip over the pose afterwards with overlay(_:mask:modelRotationAt:).
Lining up the strides
Two gaits blended at the same fraction of their cycles are not at the same moment of the stride: each clip starts wherever its animator started it, so one clip's left foot forward can average with another's right, and the legs cross. That happens whenever one gait eases into the next.
StrideAlignment.align(_:on:footJoint:samples:acceptBelow:) measures the fix. It traces footJoint's path through each clip, scales every path to the same size, tries every shift against the gait with the longest stride, and keeps the best:
// your game, once per model let alignment = StrideAlignment.align(rig.clips, on: rig.skeleton, footJoint: class="p">"foot_l") let strideOffsets = alignment.mapValues { Double($0.phaseOffset) }
The result is one StrideAlignment per clip that strides, by name: phaseOffset, match (0 is a perfect match, near 2 is an unrelated motion) and isAligned. An idle, whose foot barely moves, and a clip that does not drive the foot get no entry. A match worse than acceptBelow (0.8) is left unaligned with an offset of 0, because a confident wrong answer is worse than none; log match rather than finding it by watching the legs. footJoint may be a whole joint path or its last component, and a joint the skeleton lacks returns an empty dictionary.
It works best on what it was built for, forward gaits whose feet trace the same path at different sizes. A strafe or a backpedal moves the foot along another path and is likely to come back unaligned; give those clips a measured phase on their LocomotionBlend.Direction instead.
Measured offsets are added to the clips' own. strideOffsets goes on top of LocomotionBlend.Clips.phaseOffset(of:). Measure a clip one way or the other, never both, or its offset counts twice.
What it does not do
- No rig. It names clips and times; your game samples and mixes them.
- No state machine beyond the body: which one-shots run, and when, is your game's.
- No upper-body masks, no IK and no root motion.
A speed, heading or age that is not a finite number is treated as zero, so nothing that is not a number reaches a pose.