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 via new Application(w, h, { physic: { adapter: new BuiltinAdapter() } }).

    Implements

    Index

    Constructors

    Properties

    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.

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

    Methods

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

      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