# Character walk

> A character, not a body: a capsule moved by queries against the world instead of by forces. WASD to walk, Space to jump.

Example `physics-character`, running at https://nacatamalon.com/en/examples/physics-character. The whole catalogue: https://nacatamalon.com/llms/examples.md.

A **character**, not a body. Everything else is pushed around by the solver; this capsule is moved by queries: it asks which surfaces are around it and settles where it touches none of them. That is why it stops dead when you release the key, holds a slope without sliding, and slides along a wall instead of sticking to it.

`useCharacterBody` comes from the adapter, not the format: it is the part of physics a scene cannot write down. **WASD** to walk, **Space** to jump.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';

import { installPhysics3d } from 'nacatamalon/physics3d';
import * as Settings from './settings';
import { Scene } from './scenes/demo';

installPhysics3d();

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#12121c',
});

// Declare Scenes
game({
    Scene
}, 'Scene');
```

`components/crate.ts`:

```ts
import { createMesh, useCubeGeometry, usePhysicsBody3d, useTransform } from 'nacatamalon';

// Loose boxes, to show what the character does NOT do: it walks through them rather than shoving
// them, because a character resolves against the world and never pushes on it.
export const Crate = (x: number) => {
    useTransform({
        x,
        y: 3,
        z: -6
    });
    createMesh({
        geometry: useCubeGeometry({
            width: 0.6,
            height: 0.6,
            depth: 0.6
        }),
        tint: '#ff9f45'
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'box',
            size: [0.6, 0.6, 0.6]
        }
    });
};
```

`components/ground.ts`:

```ts
import { createMesh, useCubeGeometry, usePhysicsBody3d, useTransform } from 'nacatamalon';

export const Ground = () => {
    useTransform({
        y: -0.5,
        scaleX: 30,
        scaleZ: 30
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#2b2b3d'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`components/player.ts`:

```ts
import { createMesh, useKeyboard, useTransform, useUpdate, useSphereGeometry } from 'nacatamalon';

import { useCharacterBody3d } from 'nacatamalon/physics3d';
import * as Settings from '../settings';

// `height` is the WHOLE height including the capsule's rounded ends, so this is a person of one
// metre eighty: the same way the format measures one, and the same way a person is measured.
export const Player = () => {
    useTransform({
        y: 3,
        z: 5
    });
    createMesh({
        geometry: useSphereGeometry({
            radius: 0.45
        }),
        tint: '#4cff91'
    });

    const player = useCharacterBody3d({
        radius: 0.35,
        height: 1.8
    });

    const keys = useKeyboard();

    useUpdate(() => {
        // Set outright rather than accumulated: that is what makes a character stop the instant
        // the key goes up, and what a body driven by forces cannot do.
        player.velocity.x = (keys.isDown('d') ? 1 : 0) - (keys.isDown('a') ? 1 : 0);
        player.velocity.z = (keys.isDown('s') ? 1 : 0) - (keys.isDown('w') ? 1 : 0);

        const length = Math.hypot(player.velocity.x, player.velocity.z);

        if (length > 0) {
            player.velocity.x = (player.velocity.x / length) * Settings.SPEED;
            player.velocity.z = (player.velocity.z / length) * Settings.SPEED;
        }

        // `isOnGround` comes from the surfaces the last step actually found, so it is false in
        // mid-air and false on a slope too steep to stand on: no double jumps, and no jumping off
        // a wall you are merely brushing against.
        if (keys.isDown(' ') && player.isOnGround) {
            player.velocity.y = Settings.JUMP;
        }
    });
};
```

`components/ramp.ts`:

```ts
import { createMesh, useCubeGeometry, usePhysicsBody3d, useTransform } from 'nacatamalon';
import { quatFromEuler } from 'nacatamalon/math';

// Tilted so its low edge meets the floor: walk at it from the left and you go up. The slope limit
// is what decides whether a surface counts as ground you can stand on or one you slide off, and
// 0.32 radians is well inside the default fifty degrees, so this one holds you.
export const Ramp = () => {
    useTransform({
        x: 5,
        y: 0.9,
        scaleX: 6,
        scaleY: 0.3,
        scaleZ: 5,
        quaternion: quatFromEuler(0, 0, 0.32)
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#39395a'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`components/step.ts`:

```ts
import { createMesh, useCubeGeometry, usePhysicsBody3d, useTransform } from 'nacatamalon';

// Something to walk into that is not a wall: low enough to climb without jumping.
export const Step = () => {
    useTransform({
        x: -5,
        y: 0.25,
        scaleX: 3,
        scaleY: 0.5,
        scaleZ: 3
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#39395a'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`components/wall.ts`:

```ts
import { createMesh, useCubeGeometry, usePhysicsBody3d, useTransform } from 'nacatamalon';

// A character that stops at a wall AND keeps sliding along it is the difference between
// colliding and controlling.
export const Wall = (x: number, z: number, sx: number, sz: number) => {
    useTransform({
        x,
        y: 1,
        z,
        scaleX: sx,
        scaleY: 2,
        scaleZ: sz
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#3a3a52'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, usePhysicsWorld3d, useSpawn } from 'nacatamalon';

import { Ground } from '../components/ground';
import { Wall } from '../components/wall';
import { Ramp } from '../components/ramp';
import { Step } from '../components/step';
import { Crate } from '../components/crate';
import { Player } from '../components/player';

export const Scene = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 58,
        near: 0.1,
        far: 140,
        transform: {
            y: 15,
            z: 17,
            rotationX: -0.72
        }
    });
    useDirectionalLight({
        color: '#ffffff',
        intensity: 1.15,
        ambient: 0.3
    });

    usePhysicsWorld3d({
        gravity: {
            x: 0,
            y: -9.81,
            z: 0
        }
    });

    useSpawn(Ground)();

    const spawnWall = useSpawn(Wall);

    spawnWall(0, -9, 24, 1);
    spawnWall(-11, 0, 1, 20);
    spawnWall(11, 0, 1, 20);
    useSpawn(Ramp)();
    useSpawn(Step)();

    const spawnCrate = useSpawn(Crate);

    for (let i = 0; i < 4; i++) {
        spawnCrate(-3 + i * 1.4);
    }

    useSpawn(Player)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 640;
export const HEIGHT = 420;
// A CHARACTER, not a body. Everything else in these examples is pushed around by the solver; this
// capsule is moved by QUERIES: it asks the world which surfaces are around it and settles on a
// place that touches none of them. WASD to walk, Space to jump.
//
// That difference is the whole point, and it is also why a character is the one part of physics a
// scene cannot write down. A crate is a shape with a weight and a file can say so. This is a
// decision taken sixty times a second about where a body is allowed to be: it climbs the step,
// walks up the ramp and not up the wall, and stops the instant you let go. None of that is a
// property of the object, so there is nothing to keep, and it comes from the adapter instead.
//
// A capsule steered with forces does none of it: it slides back down slopes, tips over, keeps
// skating after the key is up, and catches on the seam between two floor pieces.

export const SPEED = 5;
export const JUMP = 5.5;
```
