nape-js API
    Preparing search index...

    Class SpringJoint

    A spring/damper constraint between two anchor points on two bodies.

    Applies a spring force that pulls or pushes the anchors toward a target restLength. The spring behavior is controlled by frequency (Hz) and damping (ratio), inherited from Constraint.

    Unlike DistanceJoint, a SpringJoint:

    • Is always soft — there is no rigid/stiff mode.
    • Has a single restLength instead of a [jointMin, jointMax] range.
    • Applies force in both directions (compression and extension).
    • Never goes slack — the spring always exerts a restorative force.

    Ideal for: vehicle suspension, soft-body connections, ragdoll hair/cloth, bouncy UI animations, bridge/rope segments, trampolines.

    const spring = new SpringJoint(
    body1, body2,
    Vec2.weak(0, 0), // anchor on body1 (local)
    Vec2.weak(0, 0), // anchor on body2 (local)
    100, // rest length (pixels)
    );
    spring.frequency = 5; // 5 Hz oscillation
    spring.damping = 0.5; // underdamped (bouncy)
    spring.space = space;

    Hierarchy (View Summary)

    Index

    Constructors

    • Parameters

      • body1: Body | null

        First body, or null for a static world anchor.

      • body2: Body | null

        Second body, or null for a static world anchor.

      • anchor1: Vec2

        Anchor point in body1's local space (disposed if weak).

      • anchor2: Vec2

        Anchor point in body2's local space (disposed if weak).

      • restLength: number

        Equilibrium distance between anchors (must be >= 0).

      Returns SpringJoint

    Properties

    debugDraw: boolean = true

    Accessors

    • get isSleeping(): boolean

      Whether the constraint's simulation component is currently sleeping.

      Only valid when the constraint is active and in a space — throws otherwise.

      Returns boolean

    • get maxForce(): number

      Maximum force (in Newtons) the constraint may apply per step.

      When the required force exceeds this value the constraint becomes slack. If breakUnderForce is true the constraint breaks instead. Must be >= 0. Infinity disables the limit.

      Returns number

      Infinity

    • set maxForce(value: number): void

      Parameters

      • value: number

      Returns void

    • get userData(): Record<string, unknown>

      Arbitrary user data attached to this constraint. Lazily initialized to {} on first access.

      Returns Record<string, unknown>

    Methods