ReadonlybodiesActive physics bodies in this simulation.
ReadonlycapabilitiesAdvertised capabilities; user code may branch on these.
Collision detector instance, created in init.
World gravity. Mutate to change at runtime. A Vector3d given
here (or as the gravity option) also pulls along the depth axis, its
z added to each body's forceZ as x and y are to its force.
ReadonlyphysicShort adapter identifier exposed as world.physic. Lets user code
branch on the active physics implementation without importing the
concrete adapter class.
Register a body with the simulation. Returns an opaque handle that
becomes renderable.body. Each adapter chooses its own concrete
body type — see the class doc for the convention. Like the builtin
Body constructor, it sets renderable.isKinematic to false, so the
renderable takes part in physics and receives pointer events.
Prefer the declarative path. Set renderable.bodyDef and call
Container.addChild(renderable) — the container auto-invokes
addBody AND inserts the renderable into the world's broadphase
(QuadTree) in one atomic step. Direct calls to addBody only
register the body with this adapter; they do NOT add the renderable
to the world's container hierarchy, so the broadphase won't return
it as a collision candidate. A body registered via direct addBody
without a matching addChild will integrate (velocity, forces) but
never collide.
The built-in physics has no joints (capabilities.constraints is
false): always returns null, which makes World#addJoint
throw.
null
Optionalpoint: Vector2d | Vector3dApply an angular impulse (Δω = τ / inertia).
Called when the adapter is being torn down; release native resources.
Read absolute rotation angle (radians). Returns 0 if not tracked.
Read angular velocity, in the same unit. Returns 0 if not tracked.
The body's angular velocity about each axis: about Z only, as the built-in bodies turn.
the body's renderable
Optionalout: Vector3d
the vector to write into
(0, 0, ω)
Return the body's axis-aligned bounding box in renderable-local
coordinates (relative to renderable.pos). Writes into the
supplied out Bounds so callers can poll allocation-free each
frame. Returns undefined if the renderable has no registered
body.
Used by ecosystem tooling — most notably @melonjs/debug-plugin
— to visualize the body's effective collision extent. The adapter
owns coordinate-system conversion: builtin Body already stores
bounds in local space, matter stores them in world space and the
adapter subtracts renderable.pos before writing.
Required by the adapter contract: any adapter that registers bodies MUST implement this. The output is the canonical, local- space representation tools rely on regardless of which engine the adapter wraps.
Return a snapshot of the body's collision shapes in renderable-local coordinates. The returned array is intended for read-only inspection (debug drawing, hit-region queries) — adapters may return a live reference for performance, so callers MUST NOT mutate it. Returns an empty array if no body.
Pairs with getBodyAABB as the adapter-side debug surface; lets each adapter own its coordinate convention without polluting the underlying physics-engine body objects. Required by the adapter contract.
Read the body's current velocity cap (mirror of
setMaxVelocity). Returns plain {x, y} so callers don't
need to import a vector type. A body with no cap reports Infinity
on both axes; a builtin Body always has one, 490 by default. Optional — adapters that don't implement velocity caps
omit this method.
The body's orientation: its turn about Z, the only one the built-in bodies have.
the body's renderable
Optionalout: Quaternion
the quaternion to write into
the turn about Z, as a quaternion
Portable velocity / force / position API. Every adapter implements
these by routing to its native engine. Use these instead of
mutating the body handle directly when writing adapter-agnostic
code. They act on the live body, which a renderable has once it is in
the world: call them from its onActivateEvent(), which runs after the
body is built, or later, never from its constructor.
Optionalout: Vector2dRead the body's velocity with its depth component, on an adapter whose bodies have depth (AdapterCapabilities.bodies3d). getVelocity stays 2D, so the code reading it is unchanged.
Optionalout: Vector3dWhether the body has at least one active contact with a surface below it (collision normal pointing up). Capability-gated by AdapterCapabilities.isGrounded. melonJS extension — Matter has no direct equivalent and the MatterAdapter implements it by scanning active pairs each call.
Always false: the builtin simulation steps every body each frame and
has no sleeping (capabilities.sleepingBodies is false).
the renderable whose body to read
false
Optional 3D sphere region query — returns renderables whose
getAbsolutePosition() lies within radius of center.
The narrow phase is a point-in-sphere test against each
candidate's centre; callers wanting bounding-sphere overlap
should intersect with the candidate's bounds afterwards.
Two call shapes:
querySphere(center: Vector3d, radius: number) — loose formquerySphere(sphere: Sphere) — packaged form, the
idiomatic surface once a query shape needs to be passed
around or cachedActive only under a 3D broadphase (Octree, set when
world.sortOn === "depth"). Adapters that don't expose a 3D
spatial index omit the method.
// Loose form — quick one-off query:
const nearby = world.adapter.querySphere?.(enemy.pos, HIT_RADIUS);
// Idiomatic form — give an entity its own bounding sphere and
// reuse it each frame:
import { Sphere } from "melonjs";
class Enemy extends Renderable {
collider = new Sphere(0, 0, 0, 30);
update(dt) {
// keep the sphere glued to this renderable's pose
this.collider.pos.set(this.pos.x, this.pos.y, this.pos.z);
// query — returns every renderable whose centre is within
// this enemy's collider
const nearby = world.adapter.querySphere?.(this.collider) ?? [];
for (const r of nearby) {
// narrow-phase / kind filter / etc.
}
return super.update(dt);
}
}
Spatial queries.
raycast is capability-gated by AdapterCapabilities.raycasts.
Adapters that don't support it may omit the method entirely
(typeof adapter.raycast === "function").
queryAABB is mandatory — every adapter must support a region query
(a broadphase walk is already needed for collision detection, so
exposing it costs nothing).
Optional 3D variant of PhysicsAdapter.raycast. Walks
the world's Octree (active only under cameraClass: Camera3d) and returns the nearest renderable whose bounding
sphere the ray intersects. 2D adapters omit this method;
call sites should check capabilities.raycasts3d first.
Unregister a body. Called automatically when Container.removeChild
detaches the renderable; direct calls are the inverse of a direct
addBody (rare — use removeChild for the normal lifecycle).
Does nothing: the built-in physics has no joints to remove.
Set angular velocity: radians per step, per second on planck and Rapier. A Vector3d is a turn about each axis, for an adapter that turns bodies in 3D; a number turns about Z, as in 2D.
Move a renderable and its body. A Vector3d also sets the depth
of a body that lives in 3D, one with a Box3d, Sphere or
other 3D shape; a flat body keeps its depth, which in a 2D game is its
draw order, whatever it is given. So setPosition(bullet, gun.pos)
(a renderable's pos is a 3D vector at run time) moves a flat bullet
without moving it in front of or behind anything.
the body's renderable
the new position, in renderable.pos space
Turn the body to an orientation. The built-in bodies turn about Z
only, so the part of q about Z is applied (the turn that takes +X
to where q takes it, seen down Z) and the rest is left out.
the body's renderable
the orientation
Toggle a body between solid and sensor mode. A sensor still fires
collision events (onCollisionStart / onCollisionActive /
onCollisionEnd) but the engine does not push the bodies apart on
contact — useful for one-way platforms, trigger zones, ground-snap
ground assists, etc.
Adapters without a native sensor flag emulate by toggling the
collision mask between its previous value and NO_OBJECT.
Runtime body-property mutators. Each maps to the corresponding
BodyDefinition field and lets game code change a body's
physical properties without re-creating it. Adapter implementations
route to their native engine (BuiltinAdapter writes to the Body
handle; MatterAdapter calls Matter's Body.set* helpers). They act on
the live body: what a body starts with goes in its
BodyDefinition, and what is set on it from the start goes in
the renderable's onActivateEvent(), which runs after the body is
built, each time it enters the world.
Set the body's velocity. A Vector3d also sets it along the
depth axis on an adapter whose bodies have depth
(AdapterCapabilities.bodies3d); a 2D adapter reads x and y
only. The same holds for applyForce, applyImpulse and
setPosition.
Advance the simulation by one frame. Called from World.update(dt).
Copy physics-engine body positions back to renderable.pos and
rotations to the renderable's transform. Called after step
each frame. Adapters that mutate the renderable directly during
step (e.g. BuiltinAdapter) may leave this a no-op.
Replace the body's collision geometry without re-creating the body.
StaticcreateCreate an adapter, the same way for every adapter: wait for
BuiltinAdapter.load, then construct it. Here that is the same as
new BuiltinAdapter(options), which stays supported.
Optionaloptions: AdapterOptions
the constructor's options
the new adapter, an instance of the class create is called on
StaticloadResolves once the adapter can be constructed. The built-in solver is plain JavaScript, loaded with melonJS, so this resolves
at once; it is here so every adapter has the same
PhysicsAdapterClass API, and code written as
await BuiltinAdapter.create() reads the same as for one that loads
WebAssembly first.
a resolved promise
Default PhysicsAdapter that wraps melonJS's native SAT-based physics. Owns the active body set, the Detector, gravity, and the simulation step. Returns the legacy Body class as its body handle so existing property-based game code (
body.vel.x = 5,body.isStatic = true) keeps working unchanged.Instantiated by default during
Applicationconstruction; user code only touches this directly when explicitly wiring it, for example with another gravity:new Application(w, h, { physic: await BuiltinAdapter.create({ gravity }) }).