nape-js API
    Preparing search index...

    Class RadialGravityField

    A point-source gravity field — pulls bodies toward an anchor with a chosen falloff law.

    Replaces the manual for (body of space.bodies) body.force = ... loops commonly written for orbital / planet / multi-body gravity scenarios. Multiple fields compose naturally via RadialGravityFieldGroup or by calling apply() on each one in sequence — each call adds to the existing accumulated force, so userland body.force writes are preserved.

    // Mario-Galaxy-style planet pulling everything toward its center
    const planet = new Body(BodyType.STATIC, new Vec2(400, 300));
    planet.shapes.add(new Circle(40));
    planet.space = space;

    const field = new RadialGravityField({
    source: planet,
    strength: 800000,
    maxRadius: 250,
    softening: 100,
    });

    // Each frame, BEFORE space.step():
    field.apply(space);
    space.step(1 / 60);
    Index

    Constructors

    Properties

    bodyFilter: BodyFilter | null
    enabled: boolean
    maxRadius: number
    minRadius: number
    scaleByMass: boolean
    softening: number
    source: Vec2 | Body
    strength: number

    Methods

    • Add this field's force contribution to every eligible body in space.

      Adds to (does not replace) each body's existing accumulated force, so multiple fields and userland force writes all stack naturally. Call once per frame, before space.step().

      Parameters

      Returns void

    • Compute (but do not apply) the force this field would exert on body given its current position. Returns (0, 0) when the field is disabled, the body is static, the body is filtered out, or the body is outside maxRadius.

      The returned Vec2 is fresh and owned by the caller.

      Parameters

      Returns Vec2

    • Current world-space center of the field.

      Returns the anchor's (x, y) — for a Body source this reflects the body's current position each call, so the field automatically tracks a moving anchor.

      Returns { x: number; y: number }