Polygon

Polygon

Creates a new rigid body model with a regular polygon hull with the given number of sides and radius.

Constructor

# new Polygon(world, attributes, optionsopt)

Parameters:
Name Type Attributes Description
world world The Matter.js world object
attributes object Visual properties e.g. position, radius and color
options object <optional>
Defines the behaviour e.g. mass, bouncyness or whether it can move
Source:
Tutorials:
Example
let polygon = new Polygon(world, {x: 300, y: 200, s: 5, r: 100, color: 'white'})

Extends

Members

# constraints :Array.<Matter.Constraint>

Type:
  • Array.<Matter.Constraint>
Overrides:
Source:

Methods

# collideWith(block)

Adds a block to an internal collisions array, to check whether this block colides with another block
Parameters:
Name Type Description
block Block
Overrides:
Source:

# constrainTo(block, optionsopt) → {Matter.Constraint}

Constrains this block to another block. Constraints are used for specifying that a fixed distance must be maintained between two blocks (or a block and a fixed world-space position). The stiffness of constraints can be modified via the options to create springs or elastic.
Parameters:
Name Type Attributes Description
block Block
options Matter.IConstraintDefinition <optional>
Overrides:
Source:
Returns:
Type
Matter.Constraint

# draw()

Draws the matter body to the p5 canvas
Overrides:

# drawSprite()

Draw an image "sprite" instead of the shape of the block. Make sure to set attributes.image so that there is an image to draw.
Overrides:
Source:

# drawVertices(vertices)

Parameters:
Name Type Description
vertices Array.<Matter.Vector>
Overrides:

# offsetMassCentre(block, offset)

Sets the mass centre of the block to a specific offset the offset is relative to the current mass centre
Parameters:
Name Type Description
block Block
offset Matter.Vector
Source:

# removeConstraint(constraint)

Remove a constraint of this block to another block.
Parameters:
Name Type Description
constraint Matter.Constraint
Source:

# rotate(rotation, pointopt, updateVelocityopt)

Rotates the block to a specific angle (absolute) set the angle of the block to the given angle
Parameters:
Name Type Attributes Description
rotation number The angle in radians
point Matter.Vector <optional>
The point to rotate around
updateVelocity boolean <optional>
Whether to update the velocity of the block
Overrides:
Source:
Example
// Rotate the block to 45 degrees
block.rotate(PI / 4)

// Rotate the block to 45 degrees around a specific point
block.rotate(PI / 4, { x: 100, y: 100 })

// Rotate the block to 45 degrees around a specific point and update the velocity
block.rotate(PI / 4, { x: 100, y: 100 }, true)

// Rotate the block to 45 degrees around a specific point and update the velocity
block.rotate(PI / 4, { x: 100, y: 100 }, true)

# rotateBy(rotation, pointopt, updateVelocityopt)

Rotates the block by a specific angle (relative) adds the angle to the current angle of the block
Parameters:
Name Type Attributes Description
rotation number The angle in radians
point Matter.Vector <optional>
The point to rotate around
updateVelocity boolean <optional>
Whether to update the velocity of the block
Overrides:
Source:
Example
// Increments the rotation of the block by 1 degrees
block.rotateBy(PI / 180)

// Increments the rotation of the block by 1 degrees around a specific point
block.rotateBy(PI / 180, { x: 100, y: 100 })