# Physics from a document

> The same crates, written as data: a scene document run through sceneFromDoc, with installPhysics2d() supplying the simulation.

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

The same falling crates, written as **data** instead of code: a scene document, the exact JSON an editor saves, run through `parseSceneDoc` and `sceneFromDoc`. The engine stores colliders and reads them back but cannot move them; `installPhysics2d()` is what makes them fall.

The document is verbose because it is a file format and not an authoring API. In an editor you would never type it: you would drag an object and tick "Collider".

## Code

`index.ts`:

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

installPhysics2d();

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

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

`data/room.ts`:

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

import * as Settings from '../settings';
import type { TColor } from 'nacatamalon';

const box = (id: string, name: string, x: number, y: number, w: number, h: number, tint: TColor, physics: Record<string, unknown>, scale = 1) => ({
    id,
    name,
    transform: {
        x,
        y,
        z: 0,
        rotation: 0,
        rotationX: 0,
        rotationY: 0,
        scaleX: scale,
        scaleY: scale,
        scaleZ: 1
    },
    components: [
        {
            type: 'sprite',
            id: id + '-art',
            texture: null,
            tint,
            width: w,
            height: h
        },
        physics,
    ],
    children: [],
});

// A collider component: the shape, how it is driven, and what it is made of. Everything it
// leaves out is filled in when the file is read, so a document written before a field existed
// still means exactly one thing.
const collider = (id: string, body: string, shape: string, extra: Record<string, unknown> = {
}) => ({
    type: 'physics-body-2d',
    id: id + '-body',
    name: 'Body',
    body,
    collider: {
        shape,
        ...extra
    },
    bounce: 0.3,
    friction: 0.5,
    density: 1,
});

export const doc = {
    format: 'nacatamalon-scene',
    version: 1,
    name: 'Level',
    assets: [],
    root: {
        id: 'root',
        name: 'Level',
        transform: null,
        // Gravity is the SCENE's, so it sits on the root.
        components: [
            {
                type: 'physics-world-2d',
                id: 'world',
                gravity: {
                    x: 0,
                    y: 620
                }
            }
        ],
        children: [
            box('ground', 'Ground', Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL, getColor('#2a2a3a'),
                collider('ground', 'static', 'rect', {
                    width: Settings.WIDTH,
                    height: Settings.WALL
                })),
            box('left', 'WallL', Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT, getColor('#2a2a3a'),
                collider('left', 'static', 'rect', {
                    width: Settings.WALL,
                    height: Settings.HEIGHT
                })),
            box('right', 'WallR', Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT, getColor('#2a2a3a'),
                collider('right', 'static', 'rect', {
                    width: Settings.WALL,
                    height: Settings.HEIGHT
                })),

            // Five crates and a ball, all declared, none of them scripted.
            ...[0, 1, 2, 3, 4].map((i) =>
                box('crate' + i, 'Crate' + i, 60 + i * 62 + 13, -40 - i * 70, 26, 26,
                    getColor(Settings.PALETTE[i]),
                    collider('crate' + i, 'dynamic', 'rect', {
                        width: 26,
                        height: 26
                    }))),

            box('ball', 'Ball', 210 + 15, -260, 30, 30, getColor('#ffffff'),
                collider('ball', 'dynamic', 'circle', {
                    radius: 15
                })),

            // Scaled x2. A collider is authored in the object's own units and its placement
            // scales it, exactly as it scales the picture. So this declares 20 and both the art
            // and the body come out 40. If the collider ignored the scale it would be half size
            // and this crate would sink halfway into the ground, which is what you would see.
            box('big', 'BigCrate', 300 + 10, -340, 20, 20, getColor('#ff9f45'),
                collider('big', 'dynamic', 'rect', {
                    width: 20,
                    height: 20
                }), 2),
        ],
    },
};
```

`scenes/demo.ts`:

```ts
import { parseSceneDoc, sceneFromDoc } from 'nacatamalon/files';
import { doc } from '../data/room';

export const Level = sceneFromDoc(parseSceneDoc(doc, 'scenes/room.scene'));
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 340;
export const WALL = 14;
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff'];
```
