Makes this object a platformer's character: the game says how fast it goes, and the world stops it at the map's walls, holds it on ledges and slopes, and lets it push what the simulation moves.
useCharacterBody2d(options: TCharacterOptions2d) => TCharacterBody2d
Exampleimport { createSprite, useKeyboard, useUpdate } from 'nacatamalon';
import { useCharacterBody2d } from 'nacatamalon/physics2d';
const Hero = () => {
createSprite({ key: 'hero', transform: { x: 40, y: 100 } });
const body = useCharacterBody2d({ width: 12, height: 24 });
const keys = useKeyboard();
useUpdate(() => {
const walk = (keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0);
body.velocity.x = walk * 90;
if (keys.justPressed('x') && body.isOnGround) body.velocity.y = -300;
if (keys.justPressed('ArrowDown') && body.isOnOneWay && !body.isOnLadder) body.dropThrough();
// Onto a ladder with up in front of it, or with down standing on its top.
const up = keys.isDown('ArrowUp') && !body.isOnOneWay;
const down = keys.isDown('ArrowDown') && body.isOnOneWay;
if (body.isOnLadder && (up || down)) body.climbing = true;
if (body.climbing) body.velocity.y = keys.isDown('ArrowUp') ? -60 : keys.isDown('ArrowDown') ? 60 : 0;
});
};
Full entry in the referenceMakes this object a walking character: a capsule moved by asking the world what is in the way, and the thing that makes a room walkable.
useCharacterBody3d(options: TCharacterOptions3d) => TCharacterBody3d
Full entry in the referenceuseJoint2dnacatamalon/physics2d
Joins two objects' bodies, or one to a point of the world when b is null: the planks of a bridge, a pendulum on its nail, a platform on a spring, a door on its hinge.
useJoint2d(a: TGameObject, b: TGameObject | null, joint: TJoint2d) => TJointHandle2d
Exampleimport { createScene, createSprite, usePhysicsBody2d, usePhysicsWorld2d, useSpawn } from 'nacatamalon';
import { useJoint2d } from 'nacatamalon/physics2d';
import type { TGameObject, TSceneFn } from 'nacatamalon';
// A bridge of eight planks between two ledges, pinned to the world at both ends.
const Plank = (x: number) => {
createSprite({ key: 'plank', transform: { x, y: 120 } });
usePhysicsBody2d({ body: 'dynamic', collider: { shape: 'rect', width: 16, height: 4 } });
};
export const River: TSceneFn = () => {
usePhysicsWorld2d();
const spawnPlank = useSpawn(Plank);
const planks: TGameObject[] = [];
for (let i = 0; i < 8; i++) planks.push(spawnPlank(72 + i * 16));
useJoint2d(planks[0], null, { type: 'hinge', at: { x: 64, y: 120 } });
for (let i = 1; i < planks.length; i++) {
useJoint2d(planks[i - 1], planks[i], { type: 'hinge', at: { x: 64 + i * 16, y: 120 } });
}
useJoint2d(planks[7], null, { type: 'hinge', at: { x: 64 + 8 * 16, y: 120 } });
return createScene();
};
Full entry in the referenceGives this object a flat shape to collide with.
usePhysicsBody2d(options: TPhysicsBody2dOptions) => TPhysicsBody2d
Exampleconst BROWN = getColor('#8b5a2b');
const Crate = (x: number, y: number) => {
createSprite({ tint: BROWN, width: 24, height: 24, transform: { x, y } });
usePhysicsBody2d({ body: 'dynamic', collider: { shape: 'rect', width: 24, height: 24 }, bounce: 0.2 });
};
Full entry in the referenceGives this object a shape in three dimensions to collide with.
usePhysicsBody3d(options: TPhysicsBody3dOptions) => TPhysicsBody3d
Full entry in the referenceOpens the flat simulation for this scene, and says how hard things fall.
usePhysicsWorld2d(options: { id, gravity }) => TPhysicsWorld2d
Exampleexport const Level: TSceneFn = () => {
usePhysicsWorld2d({ gravity: { x: 0, y: 900 } });
// ...then objects with usePhysicsBody2d, which fall in it.
return createScene();
};
Full entry in the referenceOpens the simulation in three dimensions for this scene.
usePhysicsWorld3d(options: { id, gravity }) => TPhysicsWorld3d
Full entry in the referenceGives this object a retro body: one that never turns, moved in pixels a fixed step at a time and stopped against the others axis by axis, the way the old games did.
useRetroBody2d(options: TRetroBody2dOptions) => TRetroBody2d
Exampleconst Ball = (x: number, y: number) => {
createSprite({ key: 'ball', transform: { x, y } });
useRetroBody2d({ body: 'dynamic', collider: { shape: 'circle', radius: 4 }, bounce: 1, gravityScale: 0 });
};
Full entry in the referenceOpens the retro simulation for this scene, and says how hard things fall: in pixels per second squared with +y down, like the full flat simulation's.
useRetroWorld2d(options: Partial<Omit<TRetroWorld2d, '_type'>>) => TRetroWorld2d
Exampleexport const Level: TSceneFn = () => {
useRetroWorld2d({ gravity: { x: 0, y: 900 } });
// ...then objects with useRetroBody2d, which fall in it.
return createScene();
};
Full entry in the referenceMakes the object being built a soft body in the scene's 2D world: something that squashes, wobbles and rolls rather than keeping its shape.
useSoftBody2d(options: TSoftBodyOptions2d) => TSoftBody2d
Exampleimport { createGame, createPolygon, createScene, useKeyboard, usePhysicsWorld2d, useSpawn, useTransform, useUpdate } from 'nacatamalon';
import { installPhysics2d, useSoftBody2d } from 'nacatamalon/physics2d';
installPhysics2d();
// A green blob that hops with space.
const Blob = () => {
useTransform({ x: 160, y: 40 });
const ring = Array.from({ length: 16 }, (_, i) => ({ x: Math.cos(i / 16 * Math.PI * 2) * 20, y: Math.sin(i / 16 * Math.PI * 2) * 20 }));
const polygon = createPolygon({ points: ring, tint: '#5bd75b' });
const blob = useSoftBody2d({ shape: 'disk', radius: 20, polygon });
const keys = useKeyboard();
useUpdate(() => {
if (keys.justPressed(' ')) blob.kick(0, -300);
});
};
const Level = () => {
usePhysicsWorld2d();
useSpawn(Blob)();
return createScene();
};
createGame('#app', { width: 320, height: 180 })({ Level });
Full entry in the referenceMakes this object a platformer's character that walks on the scene's maps, with no physics engine: the game says how fast it goes, and the map stops it at walls, holds it on ledges and slopes, lets it climb ladders and carries it on belts.
useTileBody2d(options: TCharacterShape2d) => TCharacterBody2d
Exampleconst Hero = () => {
createSprite({ key: 'hero', transform: { x: 40, y: 100 } });
const body = useTileBody2d({ width: 12, height: 24 });
const keys = useKeyboard();
useUpdate(() => {
const walk = (keys.isDown('ArrowRight') ? 1 : 0) - (keys.isDown('ArrowLeft') ? 1 : 0);
body.velocity.x = walk * 90;
if (keys.justPressed('x') && body.isOnGround) body.velocity.y = -300;
if (keys.justPressed('ArrowDown') && body.isOnOneWay && !body.isOnLadder) body.dropThrough();
// Onto a ladder with up in front of it, or with down standing on its top.
const up = keys.isDown('ArrowUp') && !body.isOnOneWay;
const down = keys.isDown('ArrowDown') && body.isOnOneWay;
if (body.isOnLadder && (up || down)) body.climbing = true;
if (body.climbing) body.velocity.y = keys.isDown('ArrowUp') ? -60 : keys.isDown('ArrowDown') ? 60 : 0;
});
};
Full entry in the reference