# Ramp cascade

> Marbles roll down a zigzag of tilted ramps. The turn orients the collider, not just the mesh.

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

Marbles roll down a zigzag of tilted static ramps. Each ramp's angle comes from `quatFromEuler`, and the turn orients the **collider**, not just the mesh, so the balls truly roll along the slope and hand off to the next. **Click** to send them back to the top.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';

import { installPhysics3d } from 'nacatamalon/physics3d';
import * as Settings from './settings';
import { Scene } from './scenes/demo';

installPhysics3d();

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

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

`components/basin.ts`:

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

export const Basin = () => {
    useTransform({
        y: -8,
        scaleX: 14,
        scaleZ: 4
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: '#2a2a3a'
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        }
    });
};
```

`components/marble.ts`:

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

import * as Settings from '../settings';

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

`components/ramp.ts`:

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

import * as Settings from '../settings';

// The tilt sign is negated because a turn in the plane runs the opposite way to the intuitive one.
export const Ramp = (dir: number, y: number, tint: string) => {
    useTransform({
        x: -1.6 * dir,
        y,
        scaleX: Settings.RW,
        scaleY: Settings.RH,
        scaleZ: Settings.RD,
        quaternion: quatFromEuler(0, 0, -Settings.TILT * dir),
    });
    createMesh({
        geometry: useCubeGeometry(),
        tint: getColor(tint)
    });
    usePhysicsBody3d({
        body: 'static',
        collider: {
            shape: 'box',
            size: [1, 1, 1]
        },
        friction: 0.4
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, usePhysicsWorld3d, usePointer, useSpawn } from 'nacatamalon';
import { useRandom } from 'nacatamalon/math';
import { getPhysicsBody3d } from 'nacatamalon/physics3d';
import * as Settings from '../settings';
import { Ramp } from '../components/ramp';
import { Basin } from '../components/basin';
import { Marble } from '../components/marble';

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

    const rng = useRandom();

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

    // Four ramps zigzagging down: the top one slopes right so a marble travels towards the next,
    // which slopes back left, each low end overhanging the next ramp's high end.
    const spawnRamp = useSpawn(Ramp);

    for (let i = 0;i < 4;i++) {
        spawnRamp(i % 2 === 0 ? 1 : -1, 4.5 - i * 3, i % 2 === 0 ? '#2f2f45' : '#26263a');
    }

    useSpawn(Basin)();

    // Released at the top, a little apart in time by being a little apart in height.
    const spawnMarble = useSpawn(Marble);
    const marbles: ReturnType<typeof spawnMarble>[] = [];

    for (let i = 0;i < 6;i++) {
        marbles.push(spawnMarble(-4 + rng.rand(-0.3, 0.3), 7 + i * 0.8, rng.rand(-0.6, 0.6), rng.choose(Settings.PALETTE)));
    }

    // Click to lift them back to the top of the run.
    const pointer = usePointer();

    pointer.onDown(() => {
        for (const marble of marbles) {
            getPhysicsBody3d(marble)?.applyImpulse(-40, 150, 0);
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
// A zigzag of tilted STATIC ramps. Each one is a box given a fixed turn, and that is the point:
// the turn orients the COLLIDER and not only what is drawn, so the marbles really do roll along
// the slope, fall off the low end and land on the next ramp tilted the other way.
//
// `quatFromEuler` takes the three angles and gives back the turn as a whole, in the same order the
// renderer reads them. Written either way the ramp leans identically; written as a whole it is the
// form the solver is told about, with nothing to convert in between.

export const PALETTE = ['#4cff91', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45', '#ff6b6b'];
export const TILT = 0.32;
export const RW = 7;
export const RH = 0.4;
export const RD = 3;
export const RADIUS = 0.42;
```
