Skip to main content

Prismatic Joint (Slider)

game.ts
const renderLayers = {
foreground: 1 << 0,
};

const gravity = new Vector2(0, -600);

export const createPrismaticJointGame = async (): Promise<Game> => {
const { game, world, renderContext, time } = createGame('demo-game');

createCamera(world, {
isStatic: true,
cullingMask: renderLayers.foreground,
verticalWorldUnits: DEMO_VERTICAL_WORLD_UNITS,
});

const physicsWorld = new PhysicsWorld({ gravity });

await createSliders(world, renderContext, renderLayers.foreground);

// `createPrismaticJointEcsSystem` and `createPumpEcsSystem` must run
// before `createPhysicsSyncEcsSystem`, which is what steps `physicsWorld`:
// newly-added joints need to be registered, and this tick's pump impulses
// applied, before that step happens (see the Prismatic Joints guide's
// registration-order caution).
world.addSystem(createPrismaticJointEcsSystem(physicsWorld));
world.addSystem(createPumpEcsSystem(time));
world.addSystem(createCameraEcsSystem(time));
world.addSystem(createRenderEcsSystem(renderContext));
world.addSystem(createPhysicsSyncEcsSystem(physicsWorld, time));

return game;
};

This demo showcases PrismaticJoint in three different setups. On the left, a piston pumps back and forth along a level rail between two limits. In the middle, an elevator platform is pumped upward and falls back down under gravity to its lower limit. On the right, a ball is pumped back up a diagonal incline and rolls back down it under gravity. In every case the joint keeps the slider locked to its rail: no rotation, no drifting sideways off the line, only limited to translate along the axis. PrismaticJoint has no built-in motor, so each slider is driven by a small demo-only system that periodically applies an impulse (see pump.component.ts and pump.system.ts).