# Dominoes

> A chain reaction nobody scripts: it falls out of contact and gravity. Click to push the first domino.

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

A chain reaction: tall thin boxes spaced closer than their own height, so each topples the next. Nothing scripts the wave; it falls out of contact and gravity. **Click** to push the first one.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { installPhysics3d } from 'nacatamalon/physics3d';
import { WIDTH, HEIGHT } from './settings';
import { Scene } from './scenes/demo';

installPhysics3d();

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

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

`components/domino.ts`:

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

import { DOMINO } from '../settings';

export const Domino = ({ x, tint }: { x: number; tint: string }) => {
    useTransform({
        x,
        y: DOMINO.height / 2,
        scaleX: DOMINO.width,
        scaleY: DOMINO.height,
        scaleZ: DOMINO.depth
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: getColor(tint)
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        },
        density: 30
    });
};
```

`components/ground.ts`:

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

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

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, usePhysicsWorld3d, usePointer, useSpawn } from 'nacatamalon';
import { getPhysicsBody3d } from 'nacatamalon/physics3d';
import { PALETTE, COUNT, SPACING } from '../settings';
import { Ground } from '../components/ground';
import { Domino } from '../components/domino';

export const Scene = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 55,
        near: 0.1,
        far: 100,
        transform: {
            y: 6,
            z: 14,
            rotationX: -0.42
        }
    });
    useDirectionalLight({
        color: '#ffffff',
        intensity: 1.1,
        ambient: 0.26
    });

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

    useSpawn(Ground)();

    const spawnDomino = useSpawn(Domino);
    const dominoes: ReturnType<typeof spawnDomino>[] = [];

    for (let i = 0; i < COUNT; i++) {
        const x = -((COUNT - 1) * SPACING) / 2 + i * SPACING;

        dominoes.push(spawnDomino({
            x,
            tint: PALETTE[i % PALETTE.length]
        }));
    }

    // Click to tip the first one into the line.
    const pointer = usePointer();

    pointer.onDown(() => {
        getPhysicsBody3d(dominoes[0])?.applyImpulse(120, 0, 0);
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const PALETTE = ['#4cff91', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45', '#ff6b6b'];
export const COUNT = 12;
export const SPACING = 1.1;
export const DOMINO = {
    width: 0.3,
    height: 2.4,
    depth: 1.2
};
```
