# Walkers

# Walkers

Part of `@ferret/physics` (`walkers.ts`). Pure numbers, no engine objects, so it runs in the browser, on a server and in the UE build.

## The rule

**Every Ferret scene rests on the engine's physics unless it says otherwise.** Nobody is put at a height by hand. A man in the air falls (into the water if he is over water); a man on a deck rests on the deck; nobody walks through a wall, off an edge by himself, or below a surface. A rigid body per soldier would be far too dear for a crowd of thousands, so the engine gives a cheaper body that keeps what matters: gravity, supports, walls and water. A sample that sets `y` from a table, or moves people along a path that can leave them in air or inside a hull, is wrong; the guarantee belongs here, not in each sample.

## The model

A `Walker` is a point with feet: position, velocity in the air, what he stands on (`support`) and where on it in its own frame (`local`), `air`, and `depth` (water over his feet).

A `Support` answers one question: for feet at height `feet` at (x, z), what ground can he step onto (the highest walkable top not above `feet + maxStep`) and is there a wall (something solid between a step and `reach` above his feet). Ready-made supports:

| Support | For |
|---|---|
| `FieldSupport(name, top, solidTop?)` | the sand, the sea bed, a quay: a function of (x, z) |
| `GridSupport(name, spacing, origin, frame?)` | the deck of a ship or any model: cells with the tops read off it, in its own frame; `frame` makes it move (a ship that heaves, rolls and steams carries whoever stands on it); `range` makes a far one free; one-cell holes with ground on both sides are bridged, an edge is not |
| `StripSupport(name, halfWidth, extendA, extendB)` | a gangway or ramp between two world points; it lies on over both ends and `clears` walls (a gangway passes through a rail) |

`Walkers` holds the supports and the options (`gravity`, `maxStep` 0.45, `maxDrop` 0.6, `reach` 2.2, `water(x, z)` the surface, `sink`) and steps a walker:

```ts
const walkers = new Walkers([new FieldSupport('ground', walkHeight), gangway, deckGrid], { water: () => sea.level });
const w = newWalker(); walkers.place(w, x, y, z);             // or walkers.mount(w, deckGrid, [lx, ly, lz]) on a moving deck
const r = walkers.step(w, dt, dx, dz, true);                   // r: { landed, splash, blocked }
```

`step` carries him with his support, moves him by (dx, dz) if the ground and the walls allow (it slides along a wall), keeps his feet on the ground at a step up or a drop down, and otherwise lets him fall. `careful: true` is for anyone who walks on his own will: he looks where he puts his foot and stops at an edge. Falling is then only for the involuntary: the ground taken from under him, a push, an order that sends him off a ship. A man who goes through the water surface reports `splash`, sinks to the float line (`swimDepth`, the chin) and floats: `w.swim` is true and the move he is asked for is a stroke. He swims to where he can stand and stands up; nobody is ever moved by hand (no teleports). Where he swims to is the game's decision.

## Reading a model into a support

Rays down onto the model's meshes on a grid, several layers per ray (the deck under an overhang as well as the roof over it), in the model's own frame: a top with a normal up is ground, a slope is solid. The Dunkirk sample's `decks.ts` does it for ships and boats; keep only the layers from a little under the main deck up, so the hull's inside is not ground to land on.

## Traps

- Update a moving support's world matrix and its `range` in the same place and the same frame (`computeWorldMatrix(true)`), or men are tested against a stale deck and fall through it.
- A support laid in the middle of a frame (a gangway) is not carried: lay it where the sim moves the men, in the same step, not in the render loop.
- A man who stands where two supports meet takes the one he is already on, then the one that carries him (so a gangway lying on a deck does not leave him behind when the ship sails).
- A deck that rises under a man (a ship on a swell) catches him: ground up to a step above his feet counts.
- A target that needs a path over ground the support does not have is unreachable: give the man a waypoint (a gangway foot to a point inboard) or let him give up (`blocked`).
