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.

Hierarchy (view full)

Constructors

  • Parameters

    • Optionalx: number = 0

      origin point of the Polygon

    • Optionaly: number = 0

      origin point of the Polygon

    • points: Vector2d[]

      array of vector defining the Polygon

    Returns Polygon

Properties

_bounds: undefined | object
points: Vector2d[]

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

origin point of the Polygon

type: string

the shape type (used internally)

"Polygon"

Methods

  • 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)

    Parameters

    • Rest...args: any[]

    Returns boolean

    True if the polygon contain the point, otherwise false

    if (polygon.contains(10, 10)) {
    // do something
    }
    // or
    if (polygon.contains(myVector2d)) {
    // do something
    }
  • returns the bounding box for this shape, the smallest Rectangle object completely containing this shape.

    Returns Bounds

    this shape bounding box Rectangle object

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

    Returns number[]

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

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

    Returns boolean

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

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

    Returns Polygon

    Reference to this object for method chaining

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

    Parameters

    Returns Polygon

    Reference to this object for method chaining

  • set new value to the Polygon

    Parameters

    • x: number

      position of the Polygon

    • y: number

      position of the Polygon

    • points: number[] | Vector2d[]

      array of vector or vertice defining the Polygon

    Returns Polygon

    this instance for objecf chaining

  • Shifts the Polygon to the given position vector.

    Parameters

    • Rest...args: any

    Returns void

    polygon.shift(10, 10);
    // or
    polygon.shift(myVector2d);
  • translate the Polygon by the specified offset

    Parameters

    • Rest...args: any

    Returns Polygon

    Reference to this object for method chaining

    polygon.translate(10, 10);
    // or
    polygon.translate(myVector2d);