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 worldspace position).
The stiffness of constraints can be modified to create springs or elastic.
See the included usage examples.
Methods
Matter.Constraint.create
Creates a new constraint.
All properties have default values, and many are precalculated 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

options
Object
Returns
constraint
Matter.Constraint.pointAWorld
Returns the worldspace position of constraint.pointA
, accounting for constraint.bodyA
.
Parameters

constraint
Constraint
Returns
the worldspace position
Matter.Constraint.pointBWorld
Returns the worldspace position of constraint.pointB
, accounting for constraint.bodyB
.
Parameters

constraint
Constraint
Returns
the worldspace position
Matter.Constraint.postSolveAll
Performs body updates required after solving constraints.
Parameters

bodies
Body[]
Matter.Constraint.preSolveAll
Prepares for solving by constraint warming.
Parameters

bodies
Body[]
Matter.Constraint.solve
Solves a distance constraint with GaussSiedel method.
Parameters

constraint
Constraint 
timeScale
Number
Matter.Constraint.solveAll
Solves all constraints in a list of collisions.
Parameters

constraints
Constraint[] 
timeScale
Number
Properties / Options
The following properties are specified for objects created by Matter.Constraint.create
and may be passed to it as options
.
The first possible Body
that this constraint is attached to.
Default: null
The second possible Body
that this constraint is attached to.
Default: null
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
An integer Number
uniquely identifying number generated in Composite.create
by Common.nextId
.
An arbitrary String
name to help the user identify and manage bodies.
Default: "Constraint"
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 pluginspecific properties.
A Vector
that specifies the offset of the constraint from center of the constraint.bodyA
if defined, otherwise a worldspace position.
Default: { x: 0, y: 0 }
A Vector
that specifies the offset of the constraint from center of the constraint.bodyB
if defined, otherwise a worldspace position.
Default: { x: 0, y: 0 }
An Object
that defines the rendering properties to be consumed by the module Matter.Render
.
Constraint.render.anchors
BooleanA Boolean
that defines if the constraint's anchor points should be rendered.
Default: true
Constraint.render.lineWidth
NumberA 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
Constraint.render.strokeStyle
StringA 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
StringA 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
BooleanA flag that indicates if the constraint should be rendered.
Default: true
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
A String
denoting the type of object.
Default: "constraint"