me.Polygon(x, y, points)

new Polygon(x, y, points)

a polygon Object.
Please do note that melonJS implements a simple Axis-Aligned Boxes collision algorithm, which requires all polygons used for collision to be convex with all vertices defined with clockwise winding. A polygon is convex when all line segments connecting two points in the interior do not cross any edge of the polygon (which means that all angles are less than 180 degrees), as described here below :


A polygon's winding is clockwise iff its vertices (points) are declared turning to the right. The image above shows COUNTERCLOCKWISE winding.

Parameters:
Name Type Description
x Number

origin point of the Polygon

y Number

origin point of the Polygon

points Array.<me.Vector2d>

array of vector defining the Polygon

Extends

Members

points :Array.<me.Vector2d>

Array of points defining the Polygon
Note: If you manually change points, you must call recalcafterwards so that the changes get applied correctly.

pos :me.Vector2d

origin point of the Polygon

Methods

clone() → {me.Polygon}

clone this Polygon

Returns:
me.Polygon -

new Polygon

containsPoint(x, y) → {boolean}

check if this Polygon contains the specified point
(Note: it is highly recommended to first do a hit test on the corresponding
bounding rect, as the function can be highly consuming with complex shapes)

Parameters:
Name Type Description
x Number

x coordinate

y Number

y coordinate

Returns:
boolean -

true if contains

containsPointV(point) → {boolean}

check if this Polygon contains the specified point

Parameters:
Name Type Description
point me.Vector2d
Returns:
boolean -

true if contains

getBounds() → {me.Rect}

returns the bounding box for this shape, the smallest Rectangle object completely containing this shape.

Returns:
me.Rect -

this shape bounding box Rectangle object

getIndices(a) → {me.Polygon}

returns a list of indices for all triangles defined in this polygon

Parameters:
Name Type Description
a Array.<Vector2d>

list of vector

Returns:
me.Polygon -

this Polygon

recalc() → {me.Polygon}

Computes the calculated collision polygon. This must be called if the points array, angle, or offset is modified manually.

Returns:
me.Polygon -

Reference to this object for method chaining

rotate(angle) → {me.Polygon}

Rotate this Polygon (counter-clockwise) by the specified angle (in radians).

Parameters:
Name Type Description
angle Number

The angle to rotate (in radians)

Returns:
me.Polygon -

Reference to this object for method chaining

scale(x, yopt) → {me.Polygon}

Scale this Polygon by the given scalar.

Parameters:
Name Type Attributes Default Description
x Number
y Number <optional>
x
Returns:
me.Polygon -

Reference to this object for method chaining

scaleV(v) → {me.Polygon}

Scale this Polygon by the given vector

Parameters:
Name Type Description
v me.Vector2d
Returns:
me.Polygon -

Reference to this object for method chaining

setShape(x, y, points)

set new value to the Polygon

Parameters:
Name Type Description
x Number

position of the Polygon

y Number

position of the Polygon

points Array.<me.Vector2d>

array of vector defining the Polygon

to2d() → {me.Polygon}

apply a 2d projection to this shape

Returns:
me.Polygon -

Reference to this object for method chaining

toIso() → {me.Polygon}

apply an isometric projection to this shape

Returns:
me.Polygon -

Reference to this object for method chaining

transform(matrix) → {me.Polygon}

apply the given transformation matrix to this Polygon

Parameters:
Name Type Description
matrix me.Matrix2d

the transformation matrix

Returns:
me.Polygon -

Reference to this object for method chaining

translate(x, y) → {me.Polygon}

translate the Polygon by the specified offset

Parameters:
Name Type Description
x Number

x offset

y Number

y offset

Returns:
me.Polygon -

this Polygon

translateV(v) → {me.Polygon}

translate the Polygon by the specified vector

Parameters:
Name Type Description
v me.Vector2d

vector offset

Returns:
me.Polygon -

Reference to this object for method chaining

updateBounds() → {me.Rect}

update the bounding box for this shape.

Returns:
me.Rect -

this shape bounding box Rectangle object