melonJS
    Preparing search index...

    Class BuiltinAdapter

    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 Application construction; user code only touches this directly when explicitly wiring it, for example with another gravity: new Application(w, h, { physic: await BuiltinAdapter.create({ gravity }) }).

    Implements

    Index
    bodies: Set<Body> = ...

    Active physics bodies in this simulation.

    capabilities: AdapterCapabilities = ...

    Advertised capabilities; user code may branch on these.

    detector: Detector

    Collision detector instance, created in init.

    gravity: Vector2d

    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.

    <0, 0.98>
    
    physicLabel: "builtin"

    Short 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.

      Parameters

      Returns Body

    • 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.

      Parameters

      Returns Bounds | undefined

    • 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.

      Parameters

      Returns readonly BodyShape[]

    • 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 form
      • querySphere(sphere: Sphere) — packaged form, the idiomatic surface once a query shape needs to be passed around or cached

      Active only under a 3D broadphase (Octree, set when world.sortOn === "depth"). Adapters that don't expose a 3D spatial index omit the method.

      Parameters

      Returns Renderable[]

      // 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);
      }
      }
    • 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.

      Parameters

      Returns void

    • 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.

      Parameters

      Returns void

    • Resolves 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.

      Returns Promise<void>

      a resolved promise