# Box stack

> Eight boxes stacked into a tower that holds still. Click to topple it with a sideways impulse.

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

Stable stacking: eight boxes resting in a tower without drift, the quiet but essential half of a solver. **Click** to knock the whole pile over with a sideways impulse.

## 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/box.ts`:

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

export const Box = ({ x, y, z, tint }: { x: number; y: number; z: number; tint: string }) => {
    useTransform({
        x,
        y,
        z
    });
    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: 16,
        scaleZ: 16
    });
    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 { useRandom } from 'nacatamalon/math';
import { getPhysicsBody3d } from 'nacatamalon/physics3d';
import { PALETTE } from '../settings';
import { Ground } from '../components/ground';
import { Box } from '../components/box';

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

    const rng = useRandom();

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

    useSpawn(Ground)();

    // A hair of jitter per box, so it stacks like a real pile rather than a perfect column.
    const spawnBox = useSpawn(Box);
    const boxes: ReturnType<typeof spawnBox>[] = [];

    for (let i = 0; i < 8; i++) {
        boxes.push(spawnBox({
            x: rng.rand(-0.04, 0.04),
            y: 0.5 + i,
            z: rng.rand(-0.04, 0.04),
            tint: rng.choose(PALETTE)
        }));
    }

    // Click to topple it: a sideways shove and a little lift.
    const pointer = usePointer();

    pointer.onDown(() => {
        for (const box of boxes) {
            getPhysicsBody3d(box)?.applyImpulse(rng.rand(40, 90), rng.rand(10, 40), rng.rand(-30, 30));
        }
    });

    return createScene();
};
```

`settings.ts`:

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