verticesContain

View sourceverticesContain(vertices: Vector2[], point: Vector2): boolean

Tests a point against a convex polygon represented by ordered vertices.

The engine’s polygon colliders use clockwise winding, so the point must be on or to the right of every directed edge. Points on an edge are contained. The helper returns false for fewer than two vertices and does not mutate the input array. It is a half-plane test rather than a general polygon algorithm: concave polygons and counterclockwise vertex order are not valid inputs for reliable containment results.

Use the current vertices from a Collider2D, such as the clockwise vertices created by Collider2D.rectangle.

Examples

import { Vector2 } from '@wimaengine/math'
import { Collider2D, verticesContain } from '@wimaengine/narrowphase'

const square = Collider2D.rectangle(2, 2)
verticesContain(square.vertices, new Vector2(0, 0)) // true
verticesContain(square.vertices, new Vector2(2, 0)) // false

Parameters

NameDescriptionDefault
vertices

Convex polygon vertices in clockwise order.

—
point

Point to test.

—

Returns

true when the point is inside the polygon or on one of its edges.