# Box stack and wrecking ball

> An eight-box tower that has to stay standing. Click to fire a heavy ball through it.

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

An eight-box tower that has to stay standing: the classic test of whether a solver holds resting contacts steady. **Click** to fire a dense ball through it, with `setLinearVelocity`, which replaces motion instead of adding to it.

## Code

`index.ts`:

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

installPhysics2d();

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

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

`components/box.ts`:

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

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

export const Box = ({ x, y, tint }: { x: number; y: number; tint: string }) => {
    createSprite({
        tint: getColor(tint),
        width: BOX,
        height: BOX,
        transform: {
            x: x,
            y: y
        }
    });
    usePhysicsBody2d({
        body: 'dynamic',
        collider: {
            shape: 'rect',
            width: BOX,
            height: BOX
        },
        friction: 0.7,
        bounce: 0.02,
        density: 1,
    });
};
```

`components/floor.ts`:

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

import { WIDTH, HEIGHT, FLOOR } from '../settings';

export const Floor = () => {
    createSprite({
        tint: '#2a2a3a',
        width: WIDTH,
        height: FLOOR,
        transform: {
            x: WIDTH / 2,
            y: HEIGHT - FLOOR / 2
        }
    });
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: WIDTH,
            height: FLOOR
        },
        friction: 0.9
    });
};
```

`components/wrecker.ts`:

```ts
import { createSprite, usePointer, usePhysicsBody2d, useSelf } from 'nacatamalon';
import { getPhysicsBody2d } from 'nacatamalon/physics2d';
import { HEIGHT, FLOOR, BR } from '../settings';

export const Wrecker = () => {
    const self = useSelf();

    createSprite({
        tint: '#e8e8f0',
        width: BR * 2,
        height: BR * 2,
        transform: {
            x: 30 + BR,
            y: HEIGHT - FLOOR - BR - 40
        }
    });
    usePhysicsBody2d({
        body: 'dynamic',
        collider: {
            shape: 'circle',
            radius: BR
        },
        friction: 0.4,
        bounce: 0.2,
        density: 8,
    });

    // The engine says what a thing is MADE OF and never what happens to it, so a shove goes to the
    // body the simulation made of it.
    const pointer = usePointer();

    pointer.onDown(() => {
        // setLinearVelocity overwrites motion outright, unlike applyImpulse which adds to
        // it, so every click launches from the same speed no matter what came before.
        getPhysicsBody2d(self)?.setLinearVelocity(700, -260);
    });
};
```

`scenes/demo.ts`:

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

import { WIDTH, HEIGHT, FLOOR, BOX, ROWS, PALETTE } from '../settings';
import { Floor } from '../components/floor';
import { Box } from '../components/box';
import { Wrecker } from '../components/wrecker';

export const Scene = () => {
    usePhysicsWorld2d({
        gravity: {
            x: 0,
            y: 900
        }
    });

    useSpawn(Floor)();

    // Boxes are seated with a 1px gap rather than exactly touching. Spawning bodies in
    // contact means the solver's first job is pushing them apart, which shows up as the
    // tower settling with a small shudder before it is stable.
    const towerX = WIDTH * 0.62 + BOX / 2;

    for (let row = 0; row < ROWS; row++) {
        const y = HEIGHT - FLOOR - (row + 1) * (BOX + 1) + BOX / 2;

        useSpawn(Box)({
            x: towerX,
            y,
            tint: PALETTE[row % PALETTE.length]
        });
    }

    useSpawn(Wrecker)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const FLOOR = 14;
export const BOX = 26;
export const ROWS = 8;
export const BR = 16;
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45'];
```
