Class DistanceConstraint

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

Example

// 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);
world.addConstraint(constraint);

Example

// 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
});
world.addConstraint(constraint);

Hierarchy

Constructors

Properties

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.

Default

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

Methods

  • Get the max force

    Returns number

  • Set max bias for this constraint.

    Parameters

    • maxBias: number

    Returns void

  • Set the max force to be used

    Parameters

    • maxForce: number

    Returns void

  • Set relaxation for this constraint.

    Parameters

    • relaxation: number

    Returns void

  • Set stiffness for this constraint.

    Parameters

    • stiffness: number

    Returns void

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

    Returns void