Skip to content

Physics ​

Wasmotion uses Bullet compiled to WASM, through the bullet-ts 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).

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 ​

ts
import { computeClosestCollision, raycastFromCamera } from '@maomi/sge/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 rigid bodies. Kinematic ghosts (characters) are not hit — pick characters with a THREE.Raycaster against their meshes instead.

Collision callbacks ​

ts
fps.registerCollisionCallback(a, b, (a, b) => { /* touched */ });

Checked once per frame against Bullet's contact manifolds; ghost overlaps (characters) count too. 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.