# Physics: recipes and measured numbers

How to get the physics behaviour you want: a stack that stands, a belt that carries, a
hero that climbs stairs, a jump that forgives. Every number on this page is measured by
one of the engine's own tests, so it stays true. The calls and fields themselves are in
the [physics reference](../reference/physics.md).

## How stiff things are

Contacts and joints give a little under load, and `substeps` in `[physics]` stiffens
them. Nothing else does: there is no setting for it.

| `substeps` | A stack of 10 crates stands low by | A bridge of 8 planks under 20 heavy crates: its hinges come apart by, at worst |
|---|---|---|
| 4 (when left out) | 3.2 units | 8.5 units |
| 8 | 0.8 | 2.8 |
| 16 | 0.2 | 1.3 |

Under 4 a stack does not stand.

## How long things swing

One body on a hinge with no `damping` swings for ever: it has 99% of its energy after a
minute. A chain of 4 links let go from 30 degrees has 98% after a minute. Let go level,
the same chain folds and whips, and 20% is left, whatever `substeps` is: the links hit
each other, and that is where it goes.

## Belts

`Collider.surface` is the speed a collider's surface runs at, in units a second,
clockwise round it: its top goes to the right, its underside to the left. A negative
speed runs the other way.

```toml
[belt]
Transform = { x = 200, y = 20 }
Collider = { w = 200, h = 8, surface = 60 }
```

A body on it is brought up to that speed by friction, so a `friction` of 0 on either
carries nothing. A `Mover` on it is taken along exactly, a unit a step at 60, on top of
its own walk: walking against it at 60 it stays where it is. It works on a collider
that moves too, so a belt on a lift carries both ways at once, and on a `one_way`
collider, which carries a mover from the step it lands on it and not while it passes
up through it.

## Ledges and stairs

A `Mover` stops at anything it walks into that is steeper than its `slope`, however
low. `Mover.step` is the highest ledge it walks up onto instead:

```toml
[hero]
Collider = { shape = "capsule", w = 16, h = 32 }
Mover = { step = 8 }
```

It steps only from the ground, only where it fits on top, and only onto what it could
stand on; a dynamic body that low is pushed, not climbed. Going up costs it no speed.
A ledge higher than the capsule's radius (half its `w`) is stepped too, all at once.
Coming down is the same: off a kerb no deeper than `step` it stays on the ground, where
without one it is in the air for a few steps and `grounded` is false. A mover stays on
ground that falls away no faster than its `slope` either way.

## A jump that forgives

Gravity and the jump are the script's: the engine does not know what a jump is, so it
cannot time one. What it does keep is `Mover.air`, the seconds since the mover last
stood (0 on the ground), and with that the two usual kindnesses are one call of the
kit library, `kit.jump`:

```luau
local kit = library("kit.luau")

function step(dt)
    local hero = find("hero")
    local pressed = kit.pressed(state.keys, "jump", input.held(0, "jump"))
    local air = get(hero, "Mover", "air")
    -- 0.1 s after a ledge (coyote time), 0.1 s before a landing (a buffered press)
    if kit.jump(state.keys, pressed, air, dt, 0.1, 0.1) then
        set(hero, "Mover", "vy", 330)
    end
end
```

It is true on the one step the jump should start, and once for each time off the
ground. What it remembers is in the table it is handed, which belongs in `state` so
that a rollback or a reload keeps it. `kit.luau` is a file of your game, yours to
change: the function is twelve lines.

## One-way platforms

`Collider.one_way` and `TileCollider.one_way` hold a `Mover` that comes down onto them
and let it through from below and from the side; `Mover.drop` lets it fall through.
That is for movers: a dynamic body meets a one-way collider as solid from every side.
