Show:
API

Matter.Constraint

The Matter.Constraint module contains methods for creating and manipulating constraints. Constraints are used for specifying that a fixed distance must be maintained between two bodies (or a body and a fixed world-space position). The stiffness of constraints can be modified to create springs or elastic.

See the included usage examples.

Methods

Matter.Constraint.create

(options)
Constraint

Creates a new constraint. All properties have default values, and many are pre-calculated automatically based on other properties. To simulate a revolute constraint (or pin joint) set length: 0 and a high stiffness value (e.g. 0.7 or above). If the constraint is unstable, try lowering the stiffness value and / or increasing engine.constraintIterations. For compound bodies, constraints must be applied to the parent body (not one of its parts). See the properties section below for detailed information on what you can pass via the options object.

Parameters

Returns

Constraint

constraint

Matter.Constraint.currentLength

(constraint)
Number

Returns the current length of the constraint. This is the distance between both of the constraint's end points. See constraint.length for the target rest length.

Parameters

Returns

Number

the current length

Matter.Constraint.pointAWorld

(constraint)
Vector

Returns the world-space position of constraint.pointA, accounting for constraint.bodyA.

Parameters

Returns

Vector

the world-space position

Matter.Constraint.pointBWorld

(constraint)
Vector

Returns the world-space position of constraint.pointB, accounting for constraint.bodyB.

Parameters

Returns

Vector

the world-space position

Matter.Constraint.postSolveAll

(bodies)
private

Performs body updates required after solving constraints.

Parameters

Matter.Constraint.preSolveAll

(bodies)
private

Prepares for solving by constraint warming.

Parameters

Matter.Constraint.solve

(constraint, timeScale)
private

Solves a distance constraint with Gauss-Siedel method.

Parameters

Matter.Constraint.solveAll

(constraints, delta)
private

Solves all constraints in a list of collisions.

Parameters

Properties / Options

The following properties if specified below are for objects created by Matter.Constraint.create and may be passed to it as options.

Constraint.bodyA

Body

The first possible Body that this constraint is attached to.

Default: null

Constraint.bodyB

Body

The second possible Body that this constraint is attached to.

Default: null

Constraint.damping

Number

A Number that specifies the damping of the constraint, i.e. the amount of resistance applied to each body based on their velocities to limit the amount of oscillation. Damping will only be apparent when the constraint also has a very low stiffness. A value of 0.1 means the constraint will apply heavy damping, resulting in little to no oscillation. A value of 0 means the constraint will apply no damping.

Default: 0

Constraint.id

Number

An integer Number uniquely identifying number generated in Composite.create by Common.nextId.

Constraint.label

String

An arbitrary String name to help the user identify and manage bodies.

Default: "Constraint"

Constraint.length

Number

A Number that specifies the target resting length of the constraint. It is calculated automatically in Constraint.create from initial positions of the constraint.bodyA and constraint.bodyB.

Constraint.plugin

An object reserved for storing plugin-specific properties.

Constraint.pointA

Vector

A Vector that specifies the offset of the constraint from center of the constraint.bodyA if defined, otherwise a world-space position.

Default: { x: 0, y: 0 }

Constraint.pointB

Vector

A Vector that specifies the offset of the constraint from center of the constraint.bodyB if defined, otherwise a world-space position.

Default: { x: 0, y: 0 }

Constraint.render

Object

An Object that defines the rendering properties to be consumed by the module Matter.Render.

Constraint.render.anchors

Boolean

A Boolean that defines if the constraint's anchor points should be rendered.

Default: true

A Number that defines the line width to use when rendering the constraint outline. A value of 0 means no outline will be rendered.

Default: 2

A String that defines the stroke style to use when rendering the constraint outline. It is the same as when using a canvas, so it accepts CSS style property values.

Default: a random colour

Constraint.render.type

String

A String that defines the constraint rendering type. The possible values are 'line', 'pin', 'spring'. An appropriate render type will be automatically chosen unless one is given in options.

Default: 'line'

Constraint.render.visible

Boolean

A flag that indicates if the constraint should be rendered.

Default: true

Constraint.stiffness

Number

A Number that specifies the stiffness of the constraint, i.e. the rate at which it returns to its resting constraint.length. A value of 1 means the constraint should be very stiff. A value of 0.2 means the constraint acts like a soft spring.

Default: 1

Constraint.type

String

A String denoting the type of object.

Default: "constraint"