FERRETGame engine over Babylon.js
Ferret

The world

World owns the Babylon scene, the simulation clock and everything that lives in it. A game has one.

The fixed step#

Simulation runs at a fixed rate (60 Hz by default, fixedStep), decoupled from rendering. Each rendered frame calls world.advance(realDt): it runs as many fixed steps as fit in the elapsed time, then the per-frame updates. A stall is never simulated as one long jump: frame time is capped at maxFrame (0.1 s by default).

Because the step is fixed, a run with the same inputs repeats exactly. That is what makes replays, networking and tests possible: tests drive a world with world.step() or world.run(seconds) and no renderer.

MemberUse
timeScaleglobal slow motion (below 1) or fast forward
pausedstops the simulation; rendering goes on
time, tickCountsimulated seconds and number of fixed steps so far
alphahow far the rendered frame is between the last two steps, for interpolating what the camera sees
entity.timeDilationper-entity dilation: a game may slow its NPCs without slowing the player

Entities and components#

An Entity wraps a Babylon TransformNode and carries components. A Component is a plain class with a few optional hooks:

class Bob extends Component<{ amplitude: number }> {
  onAttach()           { /* attached to an entity */ }
  onStart()            { /* before the first update */ }
  onFixedUpdate(dt)    { /* simulation: at the fixed rate */ }
  onUpdate(dt)         { /* presentation: once per rendered frame */ }
  onDetach()           { /* removed */ }
}

const e = world.spawn('crate', { position: new BABYLON.Vector3(0, 1, 0), tags: ['prop'] });
e.add(Bob, { amplitude: 0.1 });

Put game logic in onFixedUpdate and things that only need to look right in onUpdate. Components are found with entity.get(Cls) or require(Cls); the world finds entities by id, name, tag or component (entity, findByName, findByTag, entitiesWith).

params are plain data. That is deliberate: a level manifest names a component and gives its params, and Registry maps the name to the class (world.components, world.behaviours). A game registers its classes once; levels then wire them without code.

Services#

A Service is a world-wide singleton: input, levels, zones, physics, UI. It has an optional init(world), fixedUpdate(dt) (before the components' fixed update), update(dt) and dispose(). Add one with world.addService(s), find it with world.service('name'). createGame adds input and physics; you add what the game uses.

Events and commands#

EventBus is a small synchronous bus (on, once, off, emit) on world.events. The engine reports what happens through it: trigger:enter, trigger:exit, character:jump, character:land, physics:contact, world:step and many more, each documented where it is emitted.

CommandChannel (world.commands) is the other side: named commands with a description that a tool, the inspector or an AI agent can list and call. Every package registers its own; see Commands.

Logging#

Logger writes by scope and level to sinks; on OS Beroun a sink posts to the shell's log, in a browser to the console.

Simulation off the render thread (SimHost, JobPool, snapshots)#

Shipped in @ferret/core; use it when a game's fixed step is heavy and the render thread should not wait for it.

  • SimProgram: { spec, init(opts, out), command(name, args), step(dt), source(), time }. It owns the simulation, and nothing else may touch it. out.emit(name, data) sends an event with the step it belongs to.
  • SimHost({ mode, spawn | program, spec, dt, shared }): mode: 'worker' runs the program in a worker (spawn gives it: () => new Worker('sim-worker.js'); the worker's entry is runSimWorker(() => new MySim(), webPort())); mode: 'thread' runs it in the calling thread. The results are the same in both.
    • host.command(name, args) returns a promise; the program applies it at the next step boundary, in the order sent. An exception in the program rejects it.
    • host.pump(nowSeconds) once per frame: thread mode steps by the time that passed (at most five steps), worker mode takes the snapshots that came; then host.replica holds the objects, interpolated by alpha, and events are delivered to host.on(name, fn).
    • host.start(), pause(), step(n), dispose(); host.transport says ring (a SharedArrayBuffer ring of three slots, when the page is cross-origin isolated), transfer (transferable buffers by postMessage) or direct (thread mode).
  • Snapshot spec: what the render side may read, as flat typed arrays. A field is a path with a suffix: x number, pos:3 a vector, pos:3~ interpolated, q:4~q a quaternion, gone? a boolean, state@a|b|c a word, name$ a string, dmg{} JSON. A collection has a capacity and a key per object; a record is a single object. new Replica(spec, { audit: true }) records every read of an undeclared property (replica.undeclared, command snapshot.audit, and edit.validate reports it).
  • JobPool({ jobs, spawn, size }): pool.shared(name, Float64Array, n) makes an array every worker sees; pool.map(phase, n, args) runs jobs[phase](from, to, { shared, args }) over contiguous ranges and returns when all are done. A job writes only the indices of its own items and never reads another item's output: then the result is bit-identical for 0, 1, 4 or 8 workers. It blocks with Atomics.wait, so call it from the sim worker (or Node), never from a page's main thread (it is serial there). The worker's entry is runJobWorker(jobs, webPort()).
  • Pages that want the ring need Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp (tools/serve.mjs --isolate; the nginx block of tools/server/nginx-ferret.conf). Without them the transfer path is used and nothing else changes.
  • A sample that has src/sim-worker.ts or src/job-worker.ts gets each bundled next to its main.js by tools/build.mjs.