Class DistanceConstraint

Constraint that tries to keep the distance between two bodies constant.


// If distance is not given as an option, then the current distance between the bodies is used.
// In this example, the bodies will be constrained to have a distance of 2 between their centers.
var bodyA = new Body({ mass: 1, position: [-1, 0] });
var bodyB = new Body({ mass: 1, position: [1, 0] });
var constraint = new DistanceConstraint(bodyA, bodyB);


// Manually set the distance and anchors
var constraint = new DistanceConstraint(bodyA, bodyB, {
distance: 1, // Distance to keep between the points
localAnchorA: [1, 0], // Point on bodyA
localAnchorB: [-1, 0] // Point on bodyB




bodyA: Body

First body participating in the constraint.

bodyB: Body

Second body participating in the constraint.

collideConnected: boolean

Set to true if you want the connected bodies to collide.


distance: number

The distance to keep between the anchor points. Defaults to the current distance between the bodies.

equations: Equation[]

Equations to be solved in this constraint

localAnchorA: Vec2

The anchor point for bodyA, defined locally in bodyA frame. Defaults to [0,0].

localAnchorB: Vec2

The anchor point for bodyB, defined locally in bodyB frame. Defaults to [0,0].

lowerLimit: number

The lower constraint limit.

lowerLimitEnabled: boolean

If the lower limit is enabled or not.

maxForce: number

Maximum force to apply.

position: number

Current constraint position. This is equal to the current distance between the world anchor points.

type: 2 | 1 | -1 | 3 | 4 | 5

The type of constraint. May be one of Constraint.DISTANCE, Constraint.GEAR, Constraint.LOCK, Constraint.PRISMATIC or Constraint.REVOLUTE.

upperLimit: number

The upper constraint limit.

upperLimitEnabled: boolean

If the upper limit is enabled or not.

DISTANCE: 1 = ...
GEAR: 2 = ...
LOCK: 3 = ...
OTHER: -1 = ...
PRISMATIC: 4 = ...
REVOLUTE: 5 = ...


  • Get the max force

    Returns number

  • Set max bias for this constraint.


    • maxBias: number

    Returns void

  • Set the max force to be used


    • maxForce: number

    Returns void

  • Set relaxation for this constraint.


    • relaxation: number

    Returns void

  • Set stiffness for this constraint.


    • stiffness: number

    Returns void

  • Update the constraint equations. Should be done if any of the bodies changed position, before solving.

    Returns void