Foundation.Polygon

N-Point polygon implementation with collision testing methods.


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/math/_digest.ts'
  • '/foundation/core/math/Polygon.ts'

Extends

This type does not extend a base type.

Implements

This type implements the following interfaces:

All implementations exhibit default defined behaviors.

Storage types

Vertex components are stored internally in a n-slot Float32Array in the format [x1,y1,x2,y2,x3,y3...]. This should be kept in mind with respect to the precision of those values when accessing them.


Public Properties

vertexCount: number

  • Returns the number of vertexes (x,y pairs) in the polygon
  • Not settable.

Constructors

Note that once instantiated, the number of vertex slots in a polygon cannot be changed.

(n: number)

  • Creates a polygon with n vertex slots.
  • While constructors don't explicitly enforce minimum vertex counts, most methods will not work unless there are 3 vertexes or more.

(n: Array<number>|Float32Array)

  • Creates a polygon from a raw Array of numbers in the format [x1,y1,x2,y2,x3,y3...].
  • Note that values from n are copied, not owned.
  • While constructors don't explicitly enforce minimum vertex counts, most methods will not work unless there are 3 vertexes or more.

Instance Methods

getVertex(n: number, out?: Vector2): Vector2

  • Returns the nth vertex (0-indexed) as a Vector2.
  • Optionally, an out vector can be provided to avoid an instantiation.
  • If n >= vertexCount the return type is undefined.

setVertex(n: number, x: number, y: number): Polygon

  • Sets the nth vertex to x and y.
  • Returns the current polygon for chaining.
  • If n >= vertexCount nothing will be set.

setVertex(n: number, v: Vector2): Polygon

  • Sets the nth vertex to the x and y components of v.
  • Returns the current polygon for chaining.
  • If n >= vertexCount nothing will be set.

bounds(out?: Rectangle): Rectangle

  • Calculates the bounding rectangle of the polygon in its current state and returns it as a rectangle.
  • Optionally, an out rectangle can be provided to avoid instantiation.

getMTVOutOf(other: Polygon, out?: Vector2): Vector2

  • Retrieve the minimum translation vector which will move the current polygon out of the other polygon.
  • Optionally, an out vector can be provided to avoid instantiation.
  • Available: 0.6+

Static Functions

projectOnAxis(poly: Polygon, axis: Vector2, out?: Vector2): Vector2

  • Given an axis defined by the vector axis, and the polygon poly, this will project poly onto axis and will return the minimum and maximum points of projection as the x and y components of a vector, respectively.
  • Optionally, and out vector can be provided to avoid instantiation.

polyCollidesPoly(p1: Polygon, p2: Polygon, MTVOut?: Vector2): boolean

  • Returns true if p1 and p2 overlap / collide.
  • Both polygons must contain at least 3 vertexes.
  • If either polygon is concave or complex, false results may be returned (convex only).
  • This is still an relatively expensive operation and should only be used as a narrow-phase collision check.
  • [0.6+] To retrieve the minimum translation vector, pass a vector object as the optional 3rd parameter.

polyCollidesRect(p: Polygon, r: Rectangle, MTVOut?: Vector2): boolean

  • Returns true if the polygon p and the rectangle r overlap.
  • For implementation, this method creates a polygon from r and calls back polyCollidesPoly(). Please see documentation for polyCollidesPoly().
  • [0.6+] To retrieve the minimum translation vector, pass a vector object as the optional 3rd parameter.

Factories

createRect(r: Rectangle): Polygon

  • Creates a new 4-point polygon representation of the rectangle r.

createRect(x: number, y: number, width: number, height: number): Polygon

  • Creates a new 4-point polygon representation of the rectangle defined by components.

createCircle(x: number, y: number, radius: number, segments?: number)

  • Given an x and y mid-point and a radius, creates a polygon circle of 18 segments.
  • Optionally, the number of segments can be specified.

createCircle(point: Vector2, radius: number, segments?: number)

  • Given a mid-point vector and a radius, creates a polygon circle of 18 segments.
  • Optionally, the number of segments can be specified.

createTriangle(rect: Rectangle, rotation?: number): Polygon

  • Given a rectangle region to fill, creates a triangle with points at the top middle, bottom left, and bottom right of the rectangle.
  • Optionally, a rotation can then be applied to the triangle. Note here that the rotation is applied AFTER the triangle has been created--the triangle points may not be contained in the rectangle.

createTriangle(x: number, y: number, width: number, height: number, rotation?: number): Polygon;

  • Given a rectangle region to fill (specified by components), creates a triangle with points at the top middle, bottom left, and bottom right of the rectangle.
  • Optionally, a rotation can then be applied to the triangle. Note here that the rotation is applied AFTER the triangle has been created--the triangle points may not be contained in the rectangle.