# Kinematic platform

> The third body type: a platform that carries and shoves crates, but that nothing can push back.

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

The third body type: a platform that carries and shoves crates, but that nothing can push back. It is steered with `setLinearVelocity`, never by writing its position, because the simulation writes that on every step.

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

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

export const Crate = (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: 'rect',
            width: size,
            height: size
        },
        friction: 0.8,
        bounce: 0.05,
        density: 1,
    });
};
```

`components/platform.ts`:

```ts
import { createSprite, usePhysicsBody2d, useSelf, useUpdate } from 'nacatamalon';
import { getPhysicsBody2d } from 'nacatamalon/physics2d';
import * as Settings from '../settings';

// The platform. High friction so crates ride along with it instead of sliding off.
export const Platform = () => {
    const self = useSelf();

    const art = createSprite({
        tint: '#5db6ff',
        width: Settings.PLAT_W,
        height: Settings.PLAT_H,
        transform: {
            x: Settings.WALL + Settings.PLAT_W / 2,
            y: 200 + Settings.PLAT_H / 2
        }
    });

    usePhysicsBody2d({
        body: 'kinematic',
        collider: {
            shape: 'rect',
            width: Settings.PLAT_W,
            height: Settings.PLAT_H
        },
        friction: 1
    });

    // Steering is the adapter's: the engine says what a thing is made of and the simulation is
    // what moves it, through the body it made of this one.
    let dir = 1;

    // Patrol between the two walls. The platform's placement is written back from the
    // simulation every frame like any non-static body, so reading it here is reading the
    // simulation's own truth rather than a value we are guessing at.
    useUpdate(() => {
        const body = getPhysicsBody2d(self);

        if (!body) {
            return;
        }

        const left = Settings.WALL + Settings.PLAT_W / 2;
        const right = Settings.WIDTH - Settings.WALL - Settings.PLAT_W / 2;

        if (dir > 0 && art.transform.x >= right) {
            dir = -1;
        } else if (dir < 0 && art.transform.x <= left) {
            dir = 1;
        }

        // Re-asserted every frame: a kinematic body keeps whatever velocity it was last
        // given, and contacts never change it, so this is a constant-speed patrol.
        body.setLinearVelocity(Settings.SPEED * dir, 0);
    });
};
```

`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.9
    });
};
```

`scenes/demo.ts`:

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

import * as Settings from '../settings';
import { Wall } from '../components/wall';
import { Crate } from '../components/crate';
import { Platform } from '../components/platform';

export const Scene = () => {
    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(Platform)();

    // Crates dropped onto it, staggered so they land while it is already moving.
    const size = 22;

    for (let i = 0;i < 5;i++) {
        useSpawn(Crate)(80 + i * 60 + size / 2, -size / 2 - i * 70, size, Settings.PALETTE[i % Settings.PALETTE.length]);
    }

    return createScene();
};
```

`settings.ts`:

```ts
// The third body type, and the one that needs the most explaining.
//
//   static     - never moves, infinite mass. Walls and floors.
//   dynamic    - moved by gravity and contacts. Everything that falls.
//   kinematic  - moved only by YOU, but still pushes dynamic bodies out of its way.
//
// A kinematic body is unstoppable: a falling crate cannot slow the platform down, but the
// platform can carry, lift and crush the crate. That asymmetry is precisely what you want
// for a moving platform, a lift, or a Battle City-style bulldozing tank.
//
// You steer it with setLinearVelocity, NOT by writing its placement, which the simulation
// would overwrite on the next step.

export const WIDTH = 480;
export const HEIGHT = 320;
export const WALL = 12;
export const PLAT_W = 120;
export const PLAT_H = 16;
export const SPEED = 130;
export const PALETTE = ['#4cff91', '#ff6b6b', '#5db6ff', '#ffd93d', '#c77dff'];
```
