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
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
-
optionsObject
Returns
constraint
Matter.Constraint.currentLength
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
-
constraintConstraint
Returns
the current length
Matter.Constraint.pointAWorld
Returns the world-space position of constraint.pointA, accounting for constraint.bodyA.
Parameters
-
constraintConstraint
Returns
the world-space position
Matter.Constraint.pointBWorld
Returns the world-space position of constraint.pointB, accounting for constraint.bodyB.
Parameters
-
constraintConstraint
Returns
the world-space position
Matter.Constraint.postSolveAll
Performs body updates required after solving constraints.
Parameters
-
bodiesBody[]
Matter.Constraint.preSolveAll
Prepares for solving by constraint warming.
Parameters
-
bodiesBody[]
Matter.Constraint.solve
Solves a distance constraint with Gauss-Siedel method.
Parameters
-
constraintConstraint -
timeScaleNumber
Matter.Constraint.solveAll
Solves all constraints in a list of collisions.
Parameters
-
constraintsConstraint[] -
deltaNumber
Properties / Options
The following properties if specified below are 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 plugin-specific properties.
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 }
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 }
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
Constraint.render.lineWidth
Number
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
Constraint.render.strokeStyle
String
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
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"