Polygon

class Polygon

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 if its vertices (points) are declared turning to the right. The image above shows COUNTERCLOCKWISE winding.

Constructor


new Polygon(x: number, y: number, points: Array<Vector2d>) → {}
Parameters:
Name Type Attributes Default Description
x number

<optional>

0

origin point of the Polygon

y number

<optional>

0

origin point of the Polygon

points Array<Vector2d>

array of vector defining the Polygon

Public Properties


points poly.js:26
points: Array<Vector2d>

Array<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 poly.js:20
pos: Vector2d

Vector2d

origin point of the Polygon

type poly.js:58
type: string = "Polygon"

string

the shape type (used internally)

Public Methods


clone poly.js:423
clone() → {Polygon}

clone this Polygon

Returns:
Type Description
Polygon

new Polygon

contains poly.js:354
contains(x: number | Vector2d, y: number) → {boolean}

Returns true if the polygon contains the given 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)

if (polygon.contains(10, 10)) {
  // do something
}
// or
if (polygon.contains(myVector2d)) {
  // do something
}
Parameters:
Name Type Attributes Description
x number | Vector2d

x coordinate or a vector point to check

y number

<optional>

y coordinate

Returns:
Type Description
boolean

True if the polygon contain the point, otherwise false

getBounds poly.js:399
getBounds() → {Bounds}

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

Returns:
Type Description
Bounds

this shape bounding box Rectangle object

getIndices poly.js:248
getIndices() → {Array<number>}

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

Returns:
Type Description
Array<number>

an array of vertex indices for all triangles forming this polygon.

isConvex poly.js:259
isConvex() → {boolean}

Returns true if the vertices composing this polygon form a convex shape (vertices must be in clockwise order).

Returns:
Type Description
boolean

true if the vertices are convex, false if not, null if not computable

recalc poly.js:197
recalc() → {Polygon}

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

Returns:
Type Description
Polygon

Reference to this object for method chaining

rotate poly.js:152
rotate(angle: number, v: Vector2d | ObservableVector2d) → {Polygon}

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

Parameters:
Name Type Attributes Description
angle number

The angle to rotate (in radians)

v Vector2d | ObservableVector2d

<optional>

an optional point to rotate around

Returns:
Type Description
Polygon

Reference to this object for method chaining

scale poly.js:171
scale(x: number, y: number) → {Polygon}

Scale this Polygon by the given scalar.

Parameters:
Name Type Attributes Default Description
x number
y number

<optional>

x
Returns:
Type Description
Polygon

Reference to this object for method chaining

scaleV poly.js:188
scaleV(v: Vector2d) → {Polygon}

Scale this Polygon by the given vector

Parameters:
Name Type Description
v Vector2d
Returns:
Type Description
Polygon

Reference to this object for method chaining

setShape poly.js:72
setShape(x: number, y: number, points: Array<Vector2d> | Array<number>) → {Polygon}

set new value to the Polygon

Parameters:
Name Type Description
x number

position of the Polygon

y number

position of the Polygon

points Array<Vector2d> | Array<number>

array of vector or vertice defining the Polygon

Returns:
Type Description
Polygon

this instance for objecf chaining

setVertices poly.js:85
setVertices(vertices: Array<Vector2d>) → {Polygon}

set the vertices defining this Polygon

Parameters:
Name Type Description
vertices Array<Vector2d>

array of vector or vertice defining the Polygon

Returns:
Type Description
Polygon

this instance for objecf chaining

shift poly.js:329
shift(x: number | Vector2d, y: number) → {}

Shifts the Polygon to the given position vector.

polygon.shift(10, 10);
// or
polygon.shift(myVector2d);
Parameters:
Name Type Attributes Description
x number | Vector2d

x coordinate or a vector point to shift to

y number

<optional>

to2d poly.js:144
to2d() → {Polygon}

apply a 2d projection to this shapen

Returns:
Type Description
Polygon

Reference to this object for method chaining

toIso poly.js:136
toIso() → {Polygon}

apply an isometric projection to this shape

Returns:
Type Description
Polygon

Reference to this object for method chaining

transform poly.js:120
transform(m: Matrix2d) → {Polygon}

apply the given transformation matrix to this Polygon

Parameters:
Name Type Description
m Matrix2d

the transformation matrix

Returns:
Type Description
Polygon

Reference to this object for method chaining

translate poly.js:299
translate(x: number | Vector2d, y: number) → {Polygon}

translate the Polygon by the specified offset

polygon.translate(10, 10);
// or
polygon.translate(myVector2d);
Parameters:
Name Type Attributes Description
x number | Vector2d

x offset or a vector point to translate by

y number

<optional>

y offset

Returns:
Type Description
Polygon

Reference to this object for method chaining

updateBounds poly.js:410
updateBounds() → {Bounds}

update the bounding box for this shape.

Returns:
Type Description
Bounds

this shape bounding box Rectangle object


Powered by webdoc!