Physics
Marble3 uses Bullet compiled to WASM, through a copy of bullet-ts that ships inside the package. The engine hides the ceremony but not the world: fps.world is a real btDiscreteDynamicsWorld you can reach for anything the wrappers don't cover.
The world
new FPS() builds the world (dispatcher, broadphase, solver, ghost-pair callback, gravity 10 down) and disposes all of it in dispose(). WASM memory is manual — every Bullet object the engine creates, it also frees; if you construct raw bullet.* objects yourself, freeing them is on you (bullet.destroy).
The raw Bullet API is the marble3/bullet subpath — the same instance the engine runs, so its objects can go straight into fps.world:
import * as bullet from 'marble3/bullet';
const gravity = new bullet.btVector3(0, -20, 0);
fps.world.setGravity(gravity);
bullet.destroy(gravity); // the world copied itDo not install or import bullet-ts next to Marble3: it would load a second WASM runtime whose objects the engine's world cannot use.
Bodies enter the world through entities: fps.add(entity) calls the entity's registerPhysics(world). Anything implementing that pair of methods participates — including voxel terrain, which registers bodies lazily.
Raycasts
import { computeClosestCollision, raycastFromCamera } from 'marble3/physics';
const hit = computeClosestCollision(fps.world, from, to);
// → { position, normal, distance, entityId } | null
const { collision, end } = raycastFromCamera(camera, fps.world, range);entityId resolves the hit back to your entity: fps.entities[hit.entityId]. This is the mechanism behind the look-at cursor and any hitscan weapon.
Raycasts see every rigid body in the world, characters included. A body no entity put there, such as streamed terrain, comes back without an entityId.
Collision callbacks
fps.registerCollisionCallback(a, b, (a, b) => { /* touched */ });Checked once per frame against Bullet's contact manifolds: the callback runs on every frame the two entities touch, not only the first. With no callbacks registered the scan is skipped entirely.
Character controller
CharacterController drives a dynamic body by writing its full velocity every frame:
- a short downward raycast finds the ground: how far, how steep;
- walk input is projected parallel to walkable slopes;
- vertical velocity is owned by the controller — hug the floor when grounded, integrate gravity when airborne, take off on
jump().
Ground steeper than the walkable threshold (up-normal ≥ 0.8, ≈ 37°) is not "ground": the character slides back down instead of climbing. Owning the vertical axis is what makes that stick — the solver may shove a frictionless capsule up a face, but the shove is overwritten next frame.
controller.grounded, walkSpeed, jumpSpeed are public; setWalkDirection(v) takes a unit-ish vector (shorter = throttle).
Stepping
FPS.start steps the world at a fixed 60 Hz with at most 2 substeps per frame and a capped frame delta — overload degrades to slow motion, never to a spiral. See Games and the loop.