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.
| Member | Use |
|---|---|
timeScale | global slow motion (below 1) or fast forward |
paused | stops the simulation; rendering goes on |
time, tickCount | simulated seconds and number of fixed steps so far |
alpha | how far the rendered frame is between the last two steps, for interpolating what the camera sees |
entity.timeDilation | per-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 (spawngives it:() => new Worker('sim-worker.js'); the worker's entry isrunSimWorker(() => 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; thenhost.replicaholds the objects, interpolated byalpha, and events are delivered tohost.on(name, fn).host.start(),pause(),step(n),dispose();host.transportsaysring(aSharedArrayBufferring of three slots, when the page is cross-origin isolated),transfer(transferable buffers bypostMessage) ordirect(thread mode).
- Snapshot spec: what the render side may read, as flat typed arrays. A field is a path with a suffix:
xnumber,pos:3a vector,pos:3~interpolated,q:4~qa quaternion,gone?a boolean,state@a|b|ca word,name$a string,dmg{}JSON. A collection has acapacityand a key per object; a record is a single object.new Replica(spec, { audit: true })records every read of an undeclared property (replica.undeclared, commandsnapshot.audit, andedit.validatereports it). JobPool({ jobs, spawn, size }):pool.shared(name, Float64Array, n)makes an array every worker sees;pool.map(phase, n, args)runsjobs[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 withAtomics.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 isrunJobWorker(jobs, webPort()).- Pages that want the ring need
Cross-Origin-Opener-Policy: same-originandCross-Origin-Embedder-Policy: require-corp(tools/serve.mjs --isolate; the nginx block oftools/server/nginx-ferret.conf). Without them the transfer path is used and nothing else changes. - A sample that has
src/sim-worker.tsorsrc/job-worker.tsgets each bundled next to itsmain.jsbytools/build.mjs.