Construct an adapter. Rapier must be loaded first: use Rapier3dAdapter.create, or await Rapier3dAdapter.load.
Readonlycapabilitiesadvertised capabilities; user code may branch on these
World gravity, in pixels per second². Change it at any time; read at every step.
Back-reference to the owning melonJS world (set in init).
ReadonlynameOptional display name reported on the startup banner. Falls back to the
adapter's physicLabel, then its class name — avoid relying on the latter,
as it is mangled in minified builds. Third-party packages typically set
this to their npm package id (e.g. "@melonjs/matter-adapter").
ReadonlyphysicShort adapter identifier exposed as world.physic. User code uses
it to branch on which physics implementation is active without
importing the adapter class — e.g.
if (app.world.physic === "matter") {
// matter-only setup (constraints, etc.)
}
Convention: a single lowercase token. The first-party labels are
"builtin" (default — BuiltinAdapter) and "matter"
(@melonjs/matter-adapter). Third-party adapters should pick a
concise identifier that won't collide with future official ones.
The reserved value "none" is set on world.physic only when the
user passes physic: "none" to Application to disable physics
entirely; adapters should not use it.
Defaults to "builtin" if an adapter doesn't declare its own — keeps
legacy adapters wired in before this field was added still working.
ReadonlypixelsSpatial conversion factor between melonJS pixels and Rapier meters.
ReadonlyrapierThe Rapier module: an escape hatch for what the portable PhysicsAdapter interface does not cover (other joints, the character controller, native queries). See the README.
ReadonlyurlOptional URL (npm / homepage / repo) reported on the startup banner. Convention matches the debug-plugin's startup line.
ReadonlyversionOptional package version reported on the startup banner. Set this when shipping the adapter as a separate npm package so users can tell which version is wired in at a glance.
The Rapier world; exposed for advanced use. Made in init.
StaticloadedWhether Rapier3dAdapter.load has finished, so that
new Rapier3dAdapter() can be called.
true once Rapier is compiled
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.
Make a joint between two bodies: what world.addJoint() calls, which
game code uses instead. A "hinge" is a Rapier revolute joint about
axis (default Z), a "rope" a rope joint, a "weld" a fixed joint
and a "slider" a prismatic joint along axis (default X). Anchors
are in each renderable's own space, relative to its pos, in pixels,
and the axis in a's; a z left out is 0.
The bodies are joined as they stand, however each is turned: a weld and a slider keep the turn between them, and a hinge leaves them only the turn about its axis.
what to join, and how, in pixels and radians
the Rapier joint
Push the body for this frame, across every sub-step: it accelerates by
force / mass pixels per second². A force at point (in world pixels)
also turns it. A force or point given as a Vector2d acts in
the plane of the body.
the body's renderable
the force
Optionalpoint: Vector2d | Vector3d
where it pushes, in world space
Strike the body once: its velocity changes by impulse / mass pixels
per second, at once. An impulse at point (in world pixels) also turns
it. A Vector2d strikes in the plane of the body.
the body's renderable
the impulse
Optionalpoint: Vector2d | Vector3d
where it strikes, in world space
Twist the body for this frame: a number about Z, a Vector3d
about each axis. In Rapier's own unit (N·m, not scaled by
pixelsPerMeter), as on the 2D Rapier adapter.
the body's renderable
the torque
Called when the adapter is being torn down; release native resources.
The body's turn about Z: the angle +X is turned through, seen down Z.
the body's renderable
in radians; 0 for a renderable with no body here
Read angular velocity, in the same unit. Returns 0 if not tracked.
The body's spin about each axis, in radians per second.
the body's renderable
Optionalout: Vector3d
the vector to write into
the spin; zero for a renderable with no body here
The body's XY bounds in renderable-local pixels, relative to pos: the
footprint of its colliders as the body is turned now. Exact for a
box, a sphere and a capsule; for a hull or a triangle mesh, that of its
bounding box, turned.
the renderable whose body bounds to read
destination Bounds (filled in place, also returned)
The body's collision shapes, as given in its bodyDef, relative to
pos and before the body turns (the turn is
Rapier3dAdapter.getRotation). A shape with isActive: false
is left out.
the renderable whose body shapes to read
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, from the one it had when it was made.
the body's renderable
Optionalout: Quaternion
the quaternion to write into
the orientation; identity for a renderable with no body here
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: Vector2dThe body's velocity on all three axes, in pixels per second.
the body's renderable
Optionalout: Vector3d
the vector to write into
the velocity; zero for a renderable with no body here
Whether 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.
Whether Rapier has put the body to sleep: a dynamic body at rest, left
out of the step until a contact or a write to it (a velocity, a force,
an impulse, a position or a rotation, which all wake it) moves it
again. false for a fixed or kinematic body, and for a renderable this
adapter does not know, including every one once it is destroyed.
the renderable whose body to read
true while the body sleeps
The renderables whose colliders' boxes overlap a rectangle in XY, at any depth: the boxes Rapier3dAdapter.getBodyAABB reports.
the rectangle, in world pixels
the renderables, each once
The renderables whose colliders overlap a sphere: their exact shapes.
the renderables, each once
The first collider a segment meets, from from to to, in world
pixels: the exact shape, not a bounding volume. A shape the ray starts
inside is skipped, so a body casting from its own centre does not hit
itself. Sensors are hit like any other body.
the hit, or null
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).
Remove a joint Rapier3dAdapter.addJoint made: what
world.removeJoint() calls. A joint already gone, with one of its
bodies, is ignored.
the Rapier joint
Turn the body to angle about Z, the turn a 2D game knows, leaving out
any other.
the body's renderable
in radians
Set the body's spin, in radians per second: a Vector3d about each axis, a number about Z, keeping the spin about X and Y.
the body's renderable
the spin
Cap the body's speed on each axis, in pixels per second. A z caps
the depth axis too; left out, that axis has no cap.
the body's renderable
the cap
Move the body and its renderable, at once. A Vector3d sets the depth too; a Vector2d keeps it. A renderable with no body here is moved all the same, as on every adapter. A coordinate that is not a finite number leaves the body and its renderable where they are.
the body's renderable
the new position, in renderable.pos space
Turn the body, and its renderable with it, to an orientation, from the one it had when it was made.
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, in pixels per second. A Vector3d sets all three axes; a Vector2d sets x and y and keeps the depth velocity as it was.
the body's renderable
the velocity
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.
Give a body new shapes. The body itself stays: its position, turn and
velocities, its joints, what was changed on it at run time (static or
not, gravity scale, damping, sensor, collision type and mask, bounce,
speed cap, Rapier's own settings such as CCD) and the handle on
renderable.body. Its colliders are replaced, so the pairs touching
it end and start again, and its mass is that of the new shapes.
A shape this adapter cannot build throws before anything is changed.
the body's renderable
the new shapes, relative to pos
StaticcreateCreate an adapter, the same way for every adapter: wait for Rapier3dAdapter.load, then construct it.
Optionaloptions: Rapier3dAdapterOptions
the constructor's options
the new adapter, an instance of the class create is called on
StaticloadDownload and compile Rapier's WebAssembly. Every call returns the same promise, so it can be started early and awaited wherever it is needed; a failed load rejects, and a later call tries again.
Rapier is imported with import(), so bundlers put it in a chunk of its
own, downloaded only when this is called.
a promise that resolves once the adapter can be constructed
melonJS physics adapter for Rapier 3D (https://rapier.rs), compiled to WebAssembly: rigid bodies that move and turn in three dimensions, for a game drawn with
Camera3d.Bodies are declared as on every adapter, with a
bodyDef, from the 3D shapes: Box3d, Sphere, Capsule3d, ConvexHull3d and TriangleMesh3d. Shapes are in the renderable's space, relative to itspos(anddepth), which is where the body sits; each frame the body's position is written back topos/depth, and its turn ontocurrentTransform, so a Mesh, aSprite3dor aGLTFModeltumbles as it is simulated.The adapter takes the 3D form of the portable API:
setVelocity,applyForce,applyImpulseandsetPositiontake a Vector3d, andgetVelocity3d,getRotation/setRotationandgetAngularVelocity3dread the rest. The 2D calls still work, in the plane of the body: aVector2dleaves the depth as it was.Rapier's WebAssembly has to be compiled before an adapter can be constructed: Rapier3dAdapter.create does both.
Example