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
Summary
Properties from Polygon
Array<Vector2d> |
|
Vector2d |
|
string |
|
Methods from Polygon
Polygon |
|
boolean |
|
Bounds |
|
Array<number> |
|
boolean |
|
Polygon |
|
Polygon |
|
Polygon |
|
Polygon |
|
Polygon |
|
Polygon |
|
|
|
Polygon |
|
Polygon |
|
Polygon |
|
Polygon |
|
Bounds |
|
Public Properties
points: Array<Vector2d>
Array<Vector2d>
Array of points defining the Polygon
Note: If you manually change points
, you must call recalc
afterwards so that the changes get applied correctly.
Public Methods
clone() → {Polygon}
clone this Polygon
Type | Description |
---|---|
Polygon |
new Polygon |
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
}
Name | Type | Attributes | Description |
---|---|---|---|
x | number | Vector2d |
x coordinate or a vector point to check |
|
y | number |
<optional> |
y coordinate |
Type | Description |
---|---|
boolean |
True if the polygon contain the point, otherwise false |
getBounds() → {Bounds}
returns the bounding box for this shape, the smallest Rectangle object completely containing this shape.
Type | Description |
---|---|
Bounds |
this shape bounding box Rectangle object |
getIndices() → {Array<number>}
returns a list of indices for all triangles defined in this polygon
Type | Description |
---|---|
Array<number> |
an array of vertex indices for all triangles forming this polygon. |
isConvex() → {boolean}
Returns true if the vertices composing this polygon form a convex shape (vertices must be in clockwise order).
Type | Description |
---|---|
boolean |
true if the vertices are convex, false if not, null if not computable |
recalc() → {Polygon}
Computes the calculated collision polygon.
This must be called if the points
array, angle
, or offset
is modified manually.
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
rotate(angle: number, v: Vector2d | ObservableVector2d) → {Polygon}
Rotate this Polygon (counter-clockwise) by the specified angle (in radians).
Name | Type | Attributes | Description |
---|---|---|---|
angle | number |
The angle to rotate (in radians) |
|
v | Vector2d | ObservableVector2d |
<optional> |
an optional point to rotate around |
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
scale(x: number, y: number) → {Polygon}
Scale this Polygon by the given scalar.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | |||
y | number |
<optional> |
x |
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
scaleV(v: Vector2d) → {Polygon}
Scale this Polygon by the given vector
Name | Type | Description |
---|---|---|
v | Vector2d |
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
setShape(x: number, y: number, points: Array<Vector2d> | Array<number>) → {Polygon}
set new value to the Polygon
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 |
Type | Description |
---|---|
Polygon |
this instance for objecf chaining |
setVertices(vertices: Array<Vector2d>) → {Polygon}
set the vertices defining this Polygon
Name | Type | Description |
---|---|---|
vertices | Array<Vector2d> |
array of vector or vertice defining the Polygon |
Type | Description |
---|---|
Polygon |
this instance for objecf chaining |
shift(x: number | Vector2d, y: number) → {}
Shifts the Polygon to the given position vector.
polygon.shift(10, 10);
// or
polygon.shift(myVector2d);
Name | Type | Attributes | Description |
---|---|---|---|
x | number | Vector2d |
x coordinate or a vector point to shift to |
|
y | number |
<optional> |
to2d() → {Polygon}
apply a 2d projection to this shapen
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
toIso() → {Polygon}
apply an isometric projection to this shape
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
transform(m: Matrix2d) → {Polygon}
apply the given transformation matrix to this Polygon
Name | Type | Description |
---|---|---|
m | Matrix2d |
the transformation matrix |
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
translate(x: number | Vector2d, y: number) → {Polygon}
translate the Polygon by the specified offset
polygon.translate(10, 10);
// or
polygon.translate(myVector2d);
Name | Type | Attributes | Description |
---|---|---|---|
x | number | Vector2d |
x offset or a vector point to translate by |
|
y | number |
<optional> |
y offset |
Type | Description |
---|---|
Polygon |
Reference to this object for method chaining |
updateBounds() → {Bounds}
update the bounding box for this shape.
Type | Description |
---|---|
Bounds |
this shape bounding box Rectangle object |