Stated Walk Module
How a body reads the walk its character's mover states, and which way that
walk turns it. Each frame it moves a character, the CharacterController
states two walks (`__poseProgram.stateWalk`): its own walk across what it
stands on (its motion less the carry of the ground under it) and the walk
it was asked for, whether or not the walls let it go there. `Locomotion`
and `LocomotionFull` both read the walk through here, and the engine running
Locomotion's graph reads it by the same rule, so a body standing still on a
conveyor or a moving deck stands idle, and one walked slowly or into a wall
turns toward where it is asked to go, whichever of them animates it. On a
frame nothing states a walk the body's motion is its mover's velocity, and
a body with no mover of its own (an NPC body under the root its brain and
navigation move) measures its motion in the world.
Consumers:
local StatedWalk = require("~.characterController.movement.statedWalk")
local measured = StatedWalk.measured()
local vx, vy, vz, ax, az = StatedWalk.motion(public.entity, MS, CC, measured, dt)
local tx, tz = StatedWalk.facing(vx, vz, ax, az, moveDeadzone)
StatedWalk.moversChanged(public.entity)
isPlacement(dx: number, dy: number, dz: number, dt: number) → boolean
Whether a step of `(dx, dy, dz)` over `dt` seconds is a placement (a
teleport, a respawn, a scripted move to somewhere else) rather than
motion: a step faster than `TELEPORT_SPEED`. A step over no time that
goes anywhere is a placement.
| arg | type | description |
|---|
| dx | number | Step along world X. |
| dy | number | Step along world Y. |
| dz | number | Step along world Z. |
| dt | number | The seconds the step spans. |
examples
if StatedWalk.isPlacement(dx, dy, dz, dt) then vel.y = 0 end
placesCharacter(dx: number, dy: number, dz: number, dt: number) → boolean
Whether a move of `(dx, dy, dz)` that something else gave a
character over a frame of `dt` seconds placed it (a teleport, a respawn,
a checkpoint) rather than carried it (a room-scale step in XR, a script
pushing it along): a move `isPlacement` names, or one longer than
`PLACEMENT_DISTANCE` over a frame of any length. The character
controller starts a placed character at rest, with no footing to jump
from until it stands again.
| arg | type | description |
|---|
| dx | number | Move along world X. |
| dy | number | Move along world Y. |
| dz | number | Move along world Z. |
| dt | number | The seconds of the frame the move lands in. |
examples
if StatedWalk.placesCharacter(dx, dy, dz, dt) then vel.y = 0 end
measured( ) → Measured
A fresh record of a body's measured motion, for `measure` and
`motion` to keep: nothing sampled yet, and no velocity measured.
examples
local measured = StatedWalk.measured()
measure(body: any, held: Measured, dt: number) →
The velocity of `body` measured in the world: its world position's
step since the frame before over the frame's gameplay seconds, across the
frame it stands in (`Frame.standing`). A step faster than
`TELEPORT_SPEED` is a placement and leaves the velocity as it was; a
body not sampled on the frame before measures none.
| arg | type | description |
|---|
| body | any | The entity whose motion is measured. |
| held | Measured | The record `measured` made for the body, updated in place. |
| dt | number | The frame's gameplay seconds. |
examples
local vx, vy, vz = StatedWalk.measure(public.entity, measured, dt)
read(body: any, hasOwnMover: boolean) →
The walk the mover of `body`'s character stated this frame, across
the frame `body` stands in (`Frame.standing`): the walk it took across what
it stands on and the walk it was asked for. A body with a mover of its own
(a MovementState or a CharacterController on its entity) reads its own
entity's statement; a body with none reads the statement of the nearest
of its ancestors that has one (a body parented under the root a
CharacterController walks). Nil on a frame nothing states a walk for it (a
replica the multiplayer sync moves, a body a script or a path moves), and
the body measures its motion then.
| arg | type | description |
|---|
| body | any | The entity the walk is read for. |
| hasOwnMover | boolean | Whether `body`'s entity carries a MovementState or a CharacterController. |
examples
local wx, wz, ax, az = StatedWalk.read(public.entity, MS ~= nil or CC ~= nil)
motion(body: any, ms: any, cc: any, held: Measured, dt: number) →
This frame's motion of `body` across the frame it stands in: its
walk across the ground and its velocity along up, and the walk it was
asked for. The walk is the one `read` answers whenever a mover states one;
on a frame none does it is the velocity of the body's MovementState, else
of its mover, else (a body with neither) its motion measured
in the world (`measure`). The velocity along up is always the mover's, or
the measured one for a body with neither.
| arg | type | description |
|---|
| body | any | The entity whose motion is read. |
| ms | any | The MovementState on `body`'s entity, or nil. |
| cc | any | The mover on `body`'s entity (`movers`), or nil. |
| held | Measured | The record `measured` made for the body, kept for a body with neither mover. |
| dt | number | The frame's gameplay seconds. |
examples
local vx, vy, vz, ax, az = StatedWalk.motion(public.entity, MS, CC, measured, dt)
facing(vx: number, vz: number, askedX: number?, askedZ: number?, moveDeadzone: number) →
The direction a body turns toward, across the frame it stands in: the
walk asked for, at whatever speed it is asked; with none asked, the
velocity once it is faster than `moveDeadzone`. Nil when neither holds,
and the body keeps its facing.
| arg | type | description |
|---|
| vx | number | The body's velocity across the ground, along the standing frame's X. |
| vz | number | The body's velocity across the ground, along the standing frame's Z. |
| askedX | number? | The walk asked for along X, or nil when nothing stated one. |
| askedZ | number? | The walk asked for along Z, or nil when nothing stated one. |
| moveDeadzone | number | The speed, in m/s, a velocity turns the body above. |
examples
local tx, tz = StatedWalk.facing(vx, vz, ax, az, public.moveDeadzone)
movers(body: any) →
The components `body` states its walk with: its MovementState, the one
whose name is `MovementState` whichever package it comes from, and its
mover, the component that moves it under the `bodyMover` contract
(`Physics.moverOf`). Either is nil where the body carries none.
| arg | type | description |
|---|
| body | any | The entity whose movers are read. |
examples
MS, CC = StatedWalk.movers(public.entity)
moversChanged(body: any) → void
Tell the components that animate `body` from its walk (`Locomotion`,
`LocomotionFull`, whichever package each comes from) that its movers
changed: a MovementState or a mover came to its entity or went from it.
Each reads its movers again on its next frame.
| arg | type | description |
|---|
| body | any | The entity whose movers changed. |
examples
StatedWalk.moversChanged(public.entity)