Skip to content

Physics playground ​

Every primitive body falling onto a grid floor: a Sphere, a Box, an Icosahedron and a Torus (both convex hulls), plus two visual-only procedural fractals. Walk among them — click to grab the mouse, WASD + Space, and look at things with the cursor.

The whole game ​

ts
import * as THREE from 'three';

import { FPS } from '@maomi/sge';
import { LookAtCursor } from '@maomi/sge/cursor';
import { Player } from '@maomi/sge/player';
import { Box, Icosahedron, Sphere, Torus } from '@maomi/sge/object';
import { Grid } from '@maomi/sge/terrain/grid';
import { defaultLights } from '@maomi/sge/lights';

import * as Fractal from './fractals';

/* The kitchen-sink FPS demo: every primitive body falling onto a grid floor, a player to
   walk among them (click to grab the mouse, WASD + space), and a look-at cursor. */
export const createGame = (): HTMLElement => {
  const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 4000);
  const fps = new FPS();

  fps.scene.add(defaultLights());

  const player = new Player({ camera });
  player.position = new THREE.Vector3(0, 1, 0);
  fps.add(player);

  const sphere = new Sphere();
  sphere.position = new THREE.Vector3(3, 5, 0.1);
  fps.add(sphere);

  const box = new Box();
  box.position = new THREE.Vector3(2.5, 10, 0.25);
  fps.add(box);

  // Convex-hull bodies: exact for the icosahedron, a solid lens for the torus.
  const ico = new Icosahedron({ radius: 2 });
  ico.position = new THREE.Vector3(-3, 6, -8);
  fps.add(ico);

  const torus = new Torus({ radius: 2, tube: 0.6 });
  torus.position = new THREE.Vector3(0, 12, -10);
  fps.add(torus);

  // Visual-only procedural shapes: plain scene objects, no physics.
  const broccoli = Fractal.RomanescoBroccoli(0, 1.5, 3);
  broccoli.position.set(6, 3, -12);
  fps.scene.add(broccoli);

  const sponge = Fractal.MengerSponge(2, 4);
  sponge.position.set(-7, 3, -12);
  fps.scene.add(sponge);

  fps.add(new Grid(200));

  const el = fps.start(player);
  fps.add(new LookAtCursor(player));
  return el;
};

Things to notice:

  • fps.add(...) is the only registration step — a Sphere is in the scene and the physics world after one call.
  • The fractals are plain THREE.Mesh objects added to fps.scene directly: anything Three-only can bypass the entity system entirely.
  • LookAtCursor is added like any entity; it ticks itself in the entity loop.