# Bowling

> A dense sphere and one applyImpulse into ten light pins: the strike is momentum transfer. Click to bowl.

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

A dense sphere and a single `applyImpulse` down a lane of ten light pins: the strike is pure momentum transfer, nothing scripts the scatter. Mass comes from `density` times volume, so the heavy ball ploughs through the rack. **Click** to bowl.

## 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 = ({ tint }: { tint: string }) => {
    useTransform({
        y: RADIUS,
        z: 6
    });
    createMesh({
        geometry: useSphereGeometry({
            radius: RADIUS
        }),
        tint: getColor(tint)
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'sphere',
            radius: RADIUS
        },
        density: 80,
        friction: 0.2,
    });
};
```

`components/lane.ts`:

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

export const Lane = () => {
    useTransform({
        y: -0.5,
        z: -3,
        scaleX: 6,
        scaleZ: 20
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#2a2a3a'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        },
        friction: 0.2
    });
};
```

`components/pin.ts`:

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

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

export const Pin = ({ x, z }: { x: number; z: number }) => {
    useTransform({
        x,
        y: PIN.height / 2,
        z,
        scaleX: PIN.width,
        scaleY: PIN.height,
        scaleZ: PIN.width
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#f4f4f8'
    });
    usePhysicsBody3d({
        body: 'dynamic',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        },
        density: 14
    });
};
```

`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, PIN } from '../settings';
import { Lane } from '../components/lane';
import { Pin } from '../components/pin';
import { Ball } from '../components/ball';

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

    const rng = useRandom();

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

    useSpawn(Lane)();

    // Ten pins in the classic triangle at the far end: rows of one, two, three and four.
    const spawnPin = useSpawn(Pin);

    for (let row = 0; row < 4; row++) {
        for (let i = 0; i <= row; i++) {
            spawnPin({
                x: (i - row / 2) * PIN.spacing,
                z: -7 - row * PIN.spacing
            });
        }
    }

    const ball = useSpawn(Ball)({
        tint: rng.choose(PALETTE)
    });

    // Click to bowl: a hard shove straight down the lane, with a hair of wobble in the aim.
    const pointer = usePointer();

    pointer.onDown(() => {
        getPhysicsBody3d(ball)?.applyImpulse(rng.rand(-30, 30), 0, -900);
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const PALETTE = ['#4cff91', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45', '#ff6b6b'];
export const PIN = {
    spacing: 0.75,
    width: 0.32,
    height: 1.4
};
export const RADIUS = 0.55;
```
