# Bouncing balls

> Spheres that keep their energy across many bounces. Restitution belongs to the pair, so the floor asks for it too.

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

The `bounce` field in action: spheres that keep their energy across many bounces. It belongs to the pair and not to one body, so the floor has to ask for it too, and the scene writes it down whether or not anything is installed to simulate it. **Click** to launch them back up.

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

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

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

export const Ball = ({ x, y, z, tint }: { x: number; y: number; z: number; tint: string }) => {
    useTransform({
        x,
        y,
        z
    });
    createMesh({
        geometry: useSphereGeometry({
            radius: RADIUS
        }),
        tint: getColor(tint)
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'sphere',
            radius: RADIUS
        },
        density: 30,
        bounce: 0.92,
    });
};
```

`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]
        },
        bounce: 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 { Ball } from '../components/ball';

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

    const rng = useRandom();

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

    useSpawn(Ground)();

    const spawnBall = useSpawn(Ball);
    const balls: ReturnType<typeof spawnBall>[] = [];

    for (let i = 0; i < 6; i++) {
        balls.push(spawnBall({
            x: -4 + i * 1.6,
            y: 3 + i * 0.7,
            z: rng.rand(-1.5, 1.5),
            tint: rng.choose(PALETTE)
        }));
    }

    // Click to launch them back up. The push goes to the body the simulation made of each ball:
    // what the engine hands back is what a ball is made of.
    const pointer = usePointer();

    pointer.onDown(() => {
        for (const ball of balls) {
            getPhysicsBody3d(ball)?.applyImpulse(rng.rand(-30, 30), 160, 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'];
export const RADIUS = 0.5;
```
