# Restitution ladder

> Six identical balls dropped from one height, differing only in bounce from 0 to 0.95.

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

Six identical balls dropped from the same height, differing only in `bounce`, from 0 to 0.95. The height each one returns to is the bounce, made visible.

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

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

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

export const Ball = ({ x, bounce, tint }: { x: number; bounce: number; tint: string }) => {
    createSprite({
        tint: getColor(tint),
        width: R * 2,
        height: R * 2,
        transform: {
            x: x,
            y: 24 + R
        }
    });
    usePhysicsBody2d({
        body: 'dynamic',
        collider: {
            shape: 'circle',
            radius: R
        },
        bounce,
        friction: 0.2,
        density: 1
    });
};
```

`components/floor.ts`:

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

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

export const Floor = () => {
    createSprite({
        tint: '#2a2a3a',
        width: WIDTH,
        height: WALL,
        transform: {
            x: WIDTH / 2,
            y: HEIGHT - WALL / 2
        }
    });
    // High friction so the balls bounce straight up instead of skidding away, and the 1.0 above.
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: WIDTH,
            height: WALL
        },
        friction: 0.9,
        bounce: 1
    });
};
```

`scenes/demo.ts`:

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

import { WIDTH, BALLS } from '../settings';
import { Floor } from '../components/floor';
import { Ball } from '../components/ball';

export const Scene = () => {
    // On the scene and not on any object in it: gravity is the room's.
    usePhysicsWorld2d({
        gravity: {
            x: 0,
            y: 420
        }
    });

    useSpawn(Floor)();

    const gap = WIDTH / (BALLS.length + 1);

    BALLS.forEach((ball, i) => {
        useSpawn(Ball)({
            x: gap * (i + 1),
            bounce: ball.bounce,
            tint: ball.tint
        });
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 380;
export const WALL = 12;
export const R = 14;
export const BALLS = [
    {
        bounce: 0.0,
        tint: '#5a5a6e'
    },
    {
        bounce: 0.2,
        tint: '#5db6ff'
    },
    {
        bounce: 0.4,
        tint: '#4cff91'
    },
    {
        bounce: 0.6,
        tint: '#ffd93d'
    },
    {
        bounce: 0.8,
        tint: '#ff9f45'
    },
    {
        bounce: 0.95,
        tint: '#ff6b6b'
    },
];
```
