# Convex polygons

> Triangles, diamonds, pentagons and hexagons tumbling down static ramps. Corners are written around the object's centre.

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

Triangles, diamonds, pentagons and hexagons tumbling down static ramps. A `polygon` collider takes the corners of a **convex** shape: the engine builds their hull, so the order does not matter and a dent disappears. For a concave shape, compose it from several convex ones.

Corners are written around the object's position, which is its centre, the same point a sprite is centred on. The picture is still a square, so a triangle shows as a square resting on a slanted edge: the gap between what is drawn and what collides stays in plain sight. **Click** to drop more.

## Code

`index.ts`:

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

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

installPhysics2d();

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

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

`components/piece.ts`:

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

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

// The picture is always a rectangle and only the collider is a polygon, so a triangle shows
// as a square resting on a slanted edge: exactly the gap between what is drawn and what
// collides that this engine keeps out in the open.
export const Piece = (kind: keyof typeof Settings.SHAPES, x: number, y: number, size: number, tint: string) => {
    createSprite({
        tint: getColor(tint),
        width: size,
        height: size,
        transform: {
            x: x,
            y: y
        }
    });
    usePhysicsBody2d({
        body: 'dynamic',
        collider: {
            shape: 'polygon',
            vertices: Settings.SHAPES[kind](size)
        },
        friction: 0.5,
        bounce: 0.25,
        density: 1,
    });
};
```

`components/ramp.ts`:

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

// Two static ramps to make the shapes tumble instead of landing flat: a hexagon and a
// triangle behave very differently on a slope, which is the point of the example.
//
// These are ROTATED RECTS, not polygons, and deliberately so: a sprite is a quad, so a
// rotated rect is the one case where the drawing and the collider are the same shape. The
// turn is carried into the body, and both turn about the same point, so the slope you see is
// exactly the slope things slide on.
export const Ramp = (x: number, y: number, w: number, h: number, rotation: number) => {
    createSprite({
        tint: '#3a3a4e',
        width: w,
        height: h,
        transform: {
            x: x,
            y: y,
            rotation: rotation
        }
    });
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: w,
            height: h
        },
        friction: 0.5
    });
};
```

`components/wall.ts`:

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

export const Wall = (x: number, y: number, w: number, h: number) => {
    createSprite({
        tint: '#2a2a3a',
        width: w,
        height: h,
        transform: {
            x: x,
            y: y
        }
    });
    usePhysicsBody2d({
        body: 'static',
        collider: {
            shape: 'rect',
            width: w,
            height: h
        },
        friction: 0.8
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, usePointer, usePhysicsWorld2d, useSpawn } from 'nacatamalon';
import { useRandom } from 'nacatamalon/math';

import * as Settings from '../settings';
import { Wall } from '../components/wall';
import { Ramp } from '../components/ramp';
import { Piece } from '../components/piece';

export const Scene = () => {
    const rng = useRandom();

    usePhysicsWorld2d({
        gravity: {
            x: 0,
            y: 900
        }
    });

    useSpawn(Wall)(Settings.WIDTH / 2, Settings.HEIGHT - Settings.WALL / 2, Settings.WIDTH, Settings.WALL);
    useSpawn(Wall)(Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);
    useSpawn(Wall)(Settings.WIDTH - Settings.WALL / 2, Settings.HEIGHT / 2, Settings.WALL, Settings.HEIGHT);

    useSpawn(Ramp)(Settings.WALL + 10 + 85, 130 + 7, 170, 14, 0.30);
    useSpawn(Ramp)(Settings.WIDTH - Settings.WALL - 190 + 85, 225 + 7, 170, 14, -0.26);

    // Kept, so the same spawner can be used again later: a piece dropped on a click is made
    // exactly like one dropped at the start, and is a real object of the scene either way.
    const spawnPiece = useSpawn(Piece);

    const drop = (i: number) => {
        const kind = Settings.KINDS[i % Settings.KINDS.length];
        const size = rng.rand(24, 38);

        spawnPiece(
            kind,
            rng.rand(Settings.WALL + 20 + size / 2, Settings.WIDTH - Settings.WALL - 60),
            -size - i * 55,
            size,
            rng.choose(Settings.PALETTE),
        );
    };

    for (let i = 0;i < 8;i++) {
        drop(i);
    }

    const pointer = usePointer();

    pointer.onDown(() => {
        for (let i = 0;i < 3;i++) {
            drop(rng.rand(0, 100) | 0);
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
// A polygon collider takes the corners of a CONVEX shape. The engine builds a convex hull from
// them, so the winding order does not matter and an outline with a dent in it silently
// becomes its hull: if you need a genuine dent, compose it from several convex pieces.
//
// Corners are authored AROUND THE OBJECT'S PLACEMENT, which in this engine is the middle of
// what it places, the same point a sprite is centred on. So a shape drawn from -s/2 to +s/2
// lands exactly where the picture is drawn, with no offset to work out.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
// Each entry maps a size to its corners, counter-clockwise from the top, around the centre.
export const SHAPES = {
    triangle: (s: number): [number, number][] => [[0, -s / 2], [s / 2, s / 2], [-s / 2, s / 2]],
    diamond: (s: number): [number, number][] => [[0, -s / 2], [s / 2, 0], [0, s / 2], [-s / 2, 0]],
    pentagon: (s: number): [number, number][] => [[0, -s / 2], [s / 2, -s * 0.12], [s * 0.31, s / 2], [-s * 0.31, s / 2], [-s / 2, -s * 0.12]],
    hexagon: (s: number): [number, number][] => [[-s * 0.25, -s / 2], [s * 0.25, -s / 2], [s / 2, 0], [s * 0.25, s / 2], [-s * 0.25, s / 2], [-s / 2, 0]],
};
export const KINDS = Object.keys(SHAPES) as Array<keyof typeof SHAPES>;
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff', '#ff9f45'];
```
