Geometry

View source

Stores the local-space polygon data used by a Collider2D.

A geometry owns the source Vector2 vertices and the normalized face normals derived from them. Geometry.transform uses the source vertices to update an output vertex array, while Geometry.getNormals returns rotated normal copies for a collider’s current orientation.

Examples

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

const collider = new Collider2D([
  new Vector2(-1, -1),
  new Vector2(1, -1),
  new Vector2(0, 1)
])

const normals = Geometry.getNormals(collider.geometry, collider.angle)
console.log(normals.length)

Constructor


constructor(vertices: Vector2[])

View source

Creates polygon geometry from an ordered list of local-space vertices.

The supplied array becomes Geometry#vertices. Face normals are calculated immediately, and Geometry#dynNormals is initialized with copies of those normals.

Parameters

NameDescriptionDefault
vertices

Polygon vertices in local-space order.

—

Methods


staticserialize(value: Geometry): GeometrySerial

View source

Converts geometry to its serialized vertex representation.

Derived normal arrays are omitted because deserialization calculates them again from the serialized vertices.

Parameters

NameDescriptionDefault
value

Geometry to serialize.

—

Returns

Serialized geometry vertices.

staticdeserialize(value: GeometrySerial, out?: Geometry): Geometry

View source

Restores geometry from serialized vertices.

The method replaces out.vertices, recalculates its base normals, and reinitializes its dynamic-normal copies. It does not validate value before reading it. When out is supplied, that geometry is mutated and returned.

Parameters

NameDescriptionDefault
value

Serialized geometry vertices.

—
out?

Geometry to update.

...

Returns

The new or updated geometry.

staticvalidateSerial(value: unknown): value is GeometrySerial

View source

Checks whether a value has the serialized geometry shape expected by Geometry.deserialize.

The check requires an object with a vertices array whose entries pass Vector2.validateSerial. It does not check polygon size, winding, or whether the vertices describe a non-degenerate polygon.

Parameters

NameDescriptionDefault
value

Value to check.

—

Returns

true when the serialized vertex data is valid.

staticgetNormals(geometry: Geometry, angle: number, out?: Vector2[]): Vector2[]

View source

Appends the geometry’s face normals rotated by an angle.

Existing values in out are preserved. A new vector is appended for each base normal, and the supplied output array is returned.

Parameters

NameDescriptionDefault
geometry

Geometry whose base normals are used.

—
angle

Rotation angle in radians.

—
out?

Array to which rotated normals are appended.

[]

Returns

The supplied or newly created output array.

staticcalcFaceNormals(vertices: Vector2[]): Vector2[]

View source

Calculates normalized face-normal axes for polygon vertices.

Edges are evaluated from each vertex to the preceding vertex, including the closing edge from the first vertex to the last. Axes that the implementation considers equivalent are included only once.

Parameters

NameDescriptionDefault
vertices

Polygon vertices in winding order.

—

Returns

The calculated normal axes.

statictransform(geometry: Geometry, pos: Vector2, angle: number, scale: Vector2, out: Vector2[]): void

View source

Transforms the source vertices into an output vertex array.

Each source vertex is rotated, multiplied by scale, and translated by pos. The method writes into the existing Vector2 at each matching index in out; it does not allocate output vectors or return the array.

Parameters

NameDescriptionDefault
geometry

Geometry whose source vertices are transformed.

—
pos

Translation to add after rotation and scaling.

—
angle

Rotation angle in radians.

—
scale

Per-axis scale to apply after rotation.

—
out

Preallocated output vectors to update.

—