Skip to content

1. Build the arena ​

The game starts as a place: a floor, a grid of pillars and an evening sky, looked at from above. This chapter assumes you followed Getting started: package installed, the two Vite settings in place.

The shell ​

A game is a function returning an element, so the shell is three lines:

ts
// src/main.ts
import { createGame } from './game';

document.body.appendChild(createGame());

Give the page a full-viewport body (html, body { height: 100%; margin: 0 }): the engine sizes its canvas to the window.

The arena ​

A floor and a grid of pillars, and a sky that three.js draws with a shader, so there is no image to load:

ts
// src/arena.ts
import * as THREE from 'three';
import { Sky } from 'three/addons/objects/Sky.js';
import type { FPS } from 'marble3';
import { Box } from 'marble3/object';

export const sunset = () => {
  const sky = new Sky();
  sky.scale.setScalar(2000);
  const u = sky.material.uniforms;
  u.turbidity.value = 8;
  u.rayleigh.value = 2.5;
  u.sunPosition.value.setFromSphericalCoords(1, THREE.MathUtils.degToRad(87), THREE.MathUtils.degToRad(200));
  return sky;
};

export const buildArena = (fps: FPS) => {
  const material = new THREE.MeshStandardMaterial({ color: 0x9aa0a8, roughness: 0.6 });

  const floor = new Box({ width: 100, height: 20, depth: 100, mass: 0, material });
  floor.position = new THREE.Vector3(0, -12, 0); // its top is at y = -2
  fps.add(floor);

  for (let i = -3; i <= 3; i++) {
    for (let j = -3; j <= 3; j++) {
      if (i === 0 && j === 0) continue; // keep the middle clear: that's where you start
      const pillar = new Box({ width: 8, height: 10, depth: 8, mass: 0, material });
      pillar.position = new THREE.Vector3(i * 20, 1, j * 20);
      fps.add(pillar);
    }
  }
};

Box is a mesh and a Bullet body of the same size. A mass of 0 makes the body static: nothing moves it, and everything that has a body collides with it. That is all the collision the arena needs: the player of chapter 2 and the enemies of chapter 3 stand on the floor and walk around the pillars without another line.

The full example draws the same boxes with a tiled shader material (terrain.ts) and sinks a few more under the floor; the plain grey here keeps the chapter short.

A first look ​

ts
// src/game.ts
import * as THREE from 'three';
import { FPS } from 'marble3';
import { defaultLights } from 'marble3/lights';

import { buildArena, sunset } from './arena';

export const createGame = (): HTMLElement => {
  const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 4000);
  const fps = new FPS();

  fps.scene.add(defaultLights());
  fps.scene.add(sunset());
  fps.renderer.toneMapping = THREE.ACESFilmicToneMapping; // the sky is in physical units
  fps.renderer.toneMappingExposure = 0.6;
  buildArena(fps);

  camera.position.set(0, 30, 60);
  camera.lookAt(0, 0, 0);
  return fps.start({ camera });
};

start({ camera }) runs the world loop without a player, drawing through a camera you place yourself.

What you should see: run vite and open the page. The arena sits under a pale evening sky, seen from above one edge: a grey floor and rows of pillars, lit and shadowed. Nothing moves yet. Chapter 2 puts you in it.