Polygon represents a polygon which can be used to create collisions masks for RuntimeObject.

Constructors

Properties

center: FloatPoint = ...

The center of the polygon. This property is only valid after calling computeCenter, and remains valid until vertices are modified.

edges: FloatPoint[] = []

The edges of the polygon. This property is only valid after calling computeEdges, and remains valid until vertices are modified.

makeNewCollisionTestResult: (() => CollisionTestResult) = makeNewCollisionTestResult

Type declaration

makeNewRaycastTestResult: (() => RaycastTestResult) = makeNewRaycastTestResult

Type declaration

vertices: FloatPoint[] = []

The vertices of the polygon

Methods

  • Do a collision test between two polygons. Please note that polygons must convexes!

    You can read the result but do not keep a reference to it as it's a static object reused between function calls. If you need to keep the results, use copyCollisionTestResult.

    Uses Separating Axis Theorem .
    Based on this and this article.

    Parameters

    • p1: Polygon

      The first polygon

    • p2: Polygon

      The second polygon

    • ignoreTouchingEdges: boolean

      If true, then edges that are touching each other, without the polygons actually overlapping, won't be considered in collision.

    Returns CollisionTestResult

    A collision result. collision property is equal to true if polygons are overlapping. Do NOT keep a reference to this.

  • Copy a RaycastTestResult into another one. Use gdjs.Polygon.makeNewRaycastTestResult() to build a new destination before copying the existing source inside it.

    Parameters

    Returns void

  • Parameters

    • minA: number
    • maxA: number
    • minB: number
    • maxB: number

    Returns number

  • Check if a point is inside a polygon.

    Uses PNPOLY by W. Randolph Franklin.

    Parameters

    • poly: Polygon

      The polygon to test

    • x: number

      The point x coordinate

    • y: number

      The point y coordinate

    Returns boolean

    true if the point is inside the polygon

  • Do a raycast test. Please note that the polygon must be convex!

    You can read the result but do not keep a reference to it as it's a static object reused between function calls. If you need to keep the results, use copyRaycastTestResult.

    For some theory, check Find the Intersection Point of Two Line Segments.

    Parameters

    • poly: Polygon

      The polygon to test

    • startX: number

      The raycast start point X

    • startY: number

      The raycast start point Y

    • endX: number

      The raycast end point X

    • endY: number

      The raycast end point Y

    Returns RaycastTestResult

    A raycast result with the contact points and distances. Do NOT keep a reference to this.