melonJS
    Preparing search index...

    Interface PhysicsBody

    Portable physics body handle returned by PhysicsAdapter.addBody and stored on renderable.body. Every adapter guarantees these methods so engine-portable code can read and mutate body state without knowing which adapter is active.

    Each adapter exports a concrete Body type (e.g. MatterAdapter.Body, BuiltinAdapter.Body) that extends this interface with its native fields — cast to the adapter-specific type only when reaching for engine-specific state (Matter's frictionAir, BuiltinAdapter's vel/force, etc.).

    Methods marked optional are not implemented by every adapter; check the adapter docs or fall back to the equivalent on the adapter itself (e.g. adapter.setVelocity(r, v) always works).

    interface PhysicsBody {
        collisionMask: number;
        collisionType: number;
        applyForce(x: number, y: number, pointX?: number, pointY?: number): void;
        applyImpulse(x: number, y: number): void;
        applyTorque?(torque: number): void;
        getAngle?(): number;
        getAngularVelocity?(): number;
        getVelocity(out?: Vector2d): Vector2d;
        setAngle?(rad: number): void;
        setAngularVelocity?(omega: number): void;
        setBounce?(r: number): void;
        setCollisionMask(mask: number): void;
        setCollisionType?(type: number): void;
        setGravityScale?(scale: number): void;
        setMass?(m: number): void;
        setSensor?(isSensor?: boolean): void;
        setStatic(isStatic?: boolean): void;
        setVelocity(x: number, y: number): void;
    }
    Index

    Properties

    collisionMask: number

    Live alias of the collision mask bits.

    collisionType: number

    Live alias of the collision category bit.

    Methods

    • Apply a continuous force (integrated over the next step). When the optional application point is provided and differs from the body's centroid, a corresponding torque is generated: τ = r × F, where r = point - centroid. BuiltinAdapter routes the torque into applyTorque; MatterAdapter passes the point directly to Matter.Body.applyForce.

      Parameters

      • x: number

        force X component

      • y: number

        force Y component

      • OptionalpointX: number

        world X of the application point (defaults to centroid)

      • OptionalpointY: number

        world Y of the application point (defaults to centroid)

      Returns void

      // pure linear thrust (existing 2-arg form, unchanged):
      ship.body.applyForce(0, -0.05);

      // off-centre kick: pushes the crate to the right AND tips it over,
      // because the contact point is at the top of the crate, away from
      // its centroid:
      const topX = crate.pos.x + crate.width / 2;
      const topY = crate.pos.y;
      crate.body.applyForce(1.5, 0, topX, topY);
    • Apply an angular impulse directly: Δω = τ / pseudoInertia. Bypasses the force/lever-arm computation in applyForce for the case where you want to spin something up without applying a linear force — a thruster, a power-up's intrinsic rotation, a knockback spin effect.

      Parameters

      • torque: number

      Returns void

      // give a powerup a one-shot 360° spin-up burst on collection:
      powerup.body.applyTorque?.(50);
    • Read angular velocity (rad / frame). Returns 0 if rotation isn't tracked.

      Returns number

      if ((spinner.body.getAngularVelocity?.() ?? 0) > 1) {
      spinner.body.setAngularVelocity?.(1); // cap spin
      }
    • Set absolute rotation angle (radians). Re-syncs the renderable's currentTransform immediately — no need to wait for the next physics step. Useful when you want to point a body at a target.

      Parameters

      • rad: number

      Returns void

      // turret tracks the player every frame:
      const dx = player.centerX - turret.centerX;
      const dy = player.centerY - turret.centerY;
      turret.body.setAngle?.(Math.atan2(dy, dx));
    • Set angular velocity (rad / frame). BuiltinAdapter integrates this each step into the body's angle and updates the renderable's currentTransform (visual rotation only — SAT collisions remain axis-aligned). MatterAdapter routes to Matter.Body.setAngularVelocity, which participates fully in matter's rotational dynamics.

      Parameters

      • omega: number

      Returns void

      // a coin sprite that spins continuously while sitting on the ground:
      coin.body.setAngularVelocity?.(0.05);
    • Toggle static (fixed-position, infinite-mass) state. Static bodies still participate in collisions; they just don't integrate.

      Parameters

      • OptionalisStatic: boolean

      Returns void