# Bumpers

> The same physics written by hand: usePhysicsWorld2d and usePhysicsBody2d say what each object is made of, and the adapter does what the format cannot say.

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

The same physics as *Falling crates*, written by hand instead of read from a file. Press **SPACE** to push the ball back up. The blue band is a **sensor**: it notices the ball and does not stop it, which is what a checkpoint, a pickup and a damage zone all are.

**The interesting part is where the two halves meet.** `usePhysicsWorld2d` and `usePhysicsBody2d` belong to the engine: they say how hard things fall and what each object is made of, write it into the scene so it can be saved, and hand it to whatever is installed to simulate. Everything past that is the adapter's, through `getPhysicsWorld2d()`: a push, a speed, a contact. The format can say what a thing is, never what happens to it.

**What you see is not what collides.** Every picture is a plain block, because that is all a tinted sprite is, while the ball and the three pegs collide as circles. Lining up the collider with the art is the game author's business, not the engine's.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { installPhysics2d } from 'nacatamalon/physics2d';
import * as Settings from './settings';
import { Table } from './scenes/demo';

installPhysics2d();

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

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

`components/ball.ts`:

```ts
import { createSprite, useKeyboard, usePhysicsBody2d, useSelf, useUpdate } from 'nacatamalon';
import { getPhysicsBody2d } from 'nacatamalon/physics2d';
import * as Settings from '../settings';

export const Ball = () => {
    const self = useSelf();
    const keys = useKeyboard();

    createSprite({
        tint: Settings.BALL,
        width: 18,
        height: 18,
        transform: {
            x: 134,
            y: 20
        }
    });
    usePhysicsBody2d({
        body: 'dynamic',
        collider: {
            shape: 'circle',
            radius: 9
        },
        bounce: 0.55,
        friction: 0.1
    });

    // Everything past here is the ADAPTER's and not the format's: a push, a speed, a question. The
    // format can say what a thing is made of and never what happens to it.
    useUpdate(() => {
        // The body, which is null in a tool that is not playing: the behaviour then simply does
        // nothing, which is what keeps a workspace from simulating.
        //
        // Why the number is so big: an impulse is weight times the change in speed, and weight
        // comes from density times area. At a density of 1 a ball nine pixels across weighs about
        // two hundred and fifty, so lifting it at three hundred pixels a second costs about
        // seventy thousand. Pixels are not metres, and this is where that shows.
        if (keys.justPressed(' ')) {
            getPhysicsBody2d(self)?.applyImpulse(0, -70000);
        }
    });
};
```

`components/band.ts`:

```ts
import { createSprite, usePhysicsBody2d, useSelf } from 'nacatamalon';
import { getPhysicsBody2d } from 'nacatamalon/physics2d';
import * as Settings from '../settings';

export const Band = (passes: { count: number; }) => {
    const self = useSelf();

    createSprite({
        tint: Settings.BAND,
        width: 300,
        height: 14,
        transform: {
            x: 180,
            y: 150
        }
    });
    // A SENSOR: it notices the overlap and declines to settle it, so things pass straight through.
    // A checkpoint, a pickup, a place that hurts.
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: 300,
            height: 14
        },
        sensor: true
    });

    // Listening with onEnter is what turns contact reporting on for this collider, so a body
    // nobody listens to costs nothing. No body in the editor while the scene is being built.
    getPhysicsBody2d(self)?.onEnter(() => {
        passes.count += 1;
    });
};
```

`components/peg.ts`:

```ts
import { createSprite, usePhysicsBody2d } from 'nacatamalon';

import * as Settings from '../settings';

export const Peg = (x: number, y: number) => {
    createSprite({
        tint: Settings.PEG,
        width: 26,
        height: 26,
        transform: {
            x: x,
            y: y
        }
    });
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'circle',
            radius: 13
        },
        bounce: 0.9
    });
};
```

`components/wall.ts`:

```ts
import { createSprite, usePhysicsBody2d } from 'nacatamalon';

import * as Settings from '../settings';

export const Wall = (x: number, y: number, w: number, h: number) => {
    createSprite({
        tint: Settings.STONE,
        width: w,
        height: h,
        transform: {
            x: x,
            y: y
        }
    });
    // What the object is MADE OF. Two things at once and on purpose: it is written down, so this
    // scene saves as a document with its colliders in it, and it is handed to whatever is
    // installed to simulate. One way in, so a scene typed by hand and the same scene reopened from
    // a file cannot behave differently.
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: w,
            height: h
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, createText, useLoadBitmapFont, usePhysicsWorld2d, useSpawn, useUpdate } from 'nacatamalon';

import * as Settings from '../settings';
import { Wall } from '../components/wall';
import { Peg } from '../components/peg';
import { Ball } from '../components/ball';
import { Band } from '../components/band';

export const Table = () => {
    const passes = {
        count: 0
    };

    const font = useLoadBitmapFont(Settings.FONT);

    usePhysicsWorld2d({
        gravity: {
            x: 0,
            y: 700
        }
    });

    useSpawn(Wall)(180, 190, 320, 12);
    useSpawn(Wall)(24, 100, 12, 200);
    useSpawn(Wall)(336, 100, 12, 200);
    useSpawn(Peg)(120, 80);
    useSpawn(Peg)(190, 110);
    useSpawn(Peg)(250, 70);
    useSpawn(Band)(passes);
    useSpawn(Ball)();

    const label = createText({
        text: '',
        font,
        style: {
            fontSize: 8
        },
        tint: Settings.PAPER,
        transform: {
            x: 30,
            y: 16
        }
    });

    createText({
        text: 'SPACE PUSHES IT UP',
        font,
        style: {
            fontSize: 8
        },
        tint: '#8fa4c4',
        transform: {
            x: 30,
            y: 32
        },
    });
    useUpdate(() => {
        label.text = 'PASSES ' + passes.count;
    });

    return createScene();
};
```

`settings.ts`:

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

export const WIDTH = 360;
export const HEIGHT = 200;
export const FONT = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
export const BALL = getColor('#e5c93b');
export const PEG = getColor('#a96fd8');
export const STONE = getColor('#3a3f52');
export const BAND = getColor('#2a4a6a');
export const PAPER = getColor('#f2f6fa');
```
