Collider2D

View source

Defines the 2D collision-shape component attached to an entity.

A collider stores the shape’s current vertices, its polygon geometry, and its current angle in radians. Polygon vertices begin as local-space values and can be transformed in place by Collider2D.update; circle colliders store their center in the first vertex.

Use Collider2D.rectangle, Collider2D.triangle, or Collider2D.line for polygon shapes. Use Collider2D.circle for the special circle representation. The polygon factories leave type as ShapeType.POLYGON; the circle factory sets it to ShapeType.Circle.

Examples

import { World } from '@wimaengine/ecs'
import { Collider2D } from '@wimaengine/narrowphase'

const world = new World()
const body = world.spawn([
  Collider2D.rectangle(2, 1)
])

const collider = world.get(body, Collider2D)
console.log(collider.type === Collider2D.POLYGON)

Constructor


constructor(vertices: Vector2[])

View source

Creates a polygon collider from local-space vertices.

The values are copied into vertices, while geometry is constructed from the supplied vertex array. The constructor does not validate vertex count, order, finiteness, or winding, so empty and degenerate polygons are accepted.

Parameters

NameDescriptionDefault
vertices

Vertices in local-space coordinates.

—

Properties


angle: number

View source

Current angle of the collider in radians.

Constructors and factories initialize this to 0. Each call to Collider2D.update replaces it, while copy and deserialization restore the stored value.

Default: 0

vertices: Vector2[]

View source

Vertices describing the collider’s current shape.

Polygon constructors copy the supplied local-space vectors. For polygon colliders, Collider2D.update overwrites these vectors with transformed coordinates. For a circle, vertices[0] is its center and vertices[1].x is its radius; the circle factory also initializes vertices[1].y to that radius.

geometry: Geometry

View source

Polygon geometry used for normals and transformations.

It is built from the constructor’s input vertices and is not updated when vertices is edited directly. Polygon updates use this geometry as their source. Copying or deserializing a collider replaces the geometry object.

Methods


staticcopy(source: Collider2D, target?: Collider2D): Collider2D

View source

Copies a collider into a new or supplied output collider.

The current vertex values, type, and angle are copied. A fresh Geometry is built from the source’s current vertices, so the result does not share the source’s vertex array or geometry object. A supplied target is mutated and returned.

Parameters

NameDescriptionDefault
source

Collider to copy.

—
target?

Collider to update and return.

...

Returns

The new or updated collider.

staticclone(target: Collider2D): Collider2D

View source

Creates a detached copy of a collider.

Parameters

NameDescriptionDefault
target

Collider to clone.

—

Returns

A new collider with copied vertices, geometry, type, and angle.

staticserialize(value: Collider2D): Collider2DSerial

View source

Converts a collider to its serialized field object.

The result contains the numeric type, angle, serialized current vertices, and serialized geometry. The geometry field is preserved as part of the serialized representation.

Parameters

NameDescriptionDefault
value

Collider to serialize.

—

Returns

The serialized collider fields.

staticdeserialize(value: Collider2DSerial, out?: Collider2D): Collider2D

View source

Deserializes collider fields into a new or supplied output collider.

The method assigns type, angle, and newly deserialized vertex and geometry objects. It does not call Collider2D.validateSerial and does not rebuild geometry from vertices, so the serialized fields are restored as provided.

Parameters

NameDescriptionDefault
value

Serialized collider fields.

—
out?

Collider to update and return.

...

Returns

The new or updated collider.

staticvalidateSerial(value: unknown): value is Collider2DSerial

View source

Checks the structure and primitive types of serialized collider fields.

The check requires type, angle, vertices, and geometry; every vertex must pass Vector2.validateSerial, and geometry must pass Geometry.validateSerial. It does not restrict type to the two ShapeType values, require finite numbers, enforce polygon size or winding, compare geometry with the vertices, or validate circle radius encoding. Deserialization does not perform this validation.

Parameters

NameDescriptionDefault
value

Candidate serialized value.

—

Returns

true when the required structure and types are present.

staticrectangle(width: number, height: number): Collider2D

View source

Creates a four-vertex polygon centered on the origin.

The vertices are ordered as (-width / 2, -height / 2), (-width / 2, height / 2), (width / 2, height / 2), and (width / 2, -height / 2). The result has polygon type and angle 0. Dimensions are used as supplied without range or finiteness checks.

Parameters

NameDescriptionDefault
width

Width along the x axis.

—
height

Height along the y axis.

—

Returns

A polygon collider with four vertices.

statictriangle(base: number, height: number, angle?: number): Collider2D

View source

Creates a three-vertex polygon centered around its centroid.

base and height define the construction dimensions, and angle controls the second side’s direction. When omitted, angle defaults to Math.asin(height / base). The factory performs no range checks, so zero lengths, invalid ratios, or an angle with a zero sine can produce NaN or infinite vertex coordinates.

Parameters

NameDescriptionDefault
base

Length of one side.

—
height

Length of a second side.

—
angle?

Angle between the sides in radians.

...

Returns

A polygon collider with three vertices.

staticline(length: number): Collider2D

View source

Creates a two-vertex polygon along the x axis.

The vertices are (length / 2, 0) and (-length / 2, 0). The result keeps ShapeType.POLYGON and angle 0.

Parameters

NameDescriptionDefault
length

Distance between the endpoints.

—

Returns

A two-vertex polygon collider.

staticcircle(radius: number): Collider2D

View source

Creates a circle collider using the component’s two-vertex encoding.

vertices[0] starts at (0, 0) and stores the center. vertices[1] is initialized to (radius, radius), and the returned collider’s type is set to ShapeType.Circle. Shape calculations use vertices[1].x as the radius. No radius validation is performed. The circle’s angle remains 0 until Collider2D.update changes it.

Parameters

NameDescriptionDefault
radius

Circle radius.

—

Returns

A circle collider.

staticgetNormals(shapeA: Collider2D, shapeB: Collider2D, out?: Vector2[]): Vector2[]

View source

Appends separating axes for a pair of colliders to an output array.

For a polygon shapeA, it appends its face normals rotated by shapeA.angle. For a non-polygon shapeA, it appends one normalized vector from shapeA.vertices[0] to a vertex of shapeB. With ordinary finite inputs, the current selection resolves to shapeB.vertices[0].

Existing values in out are preserved; new normals are appended.

Parameters

NameDescriptionDefault
shapeA

First collider.

—
shapeB

Second collider.

—
out?

Array to which normals are appended.

[]

Returns

The supplied or newly created output array.

staticupdate(shape: Collider2D, position: Vector2, angle: number, scale: Vector2): void

View source

Updates a collider’s angle and current vertex coordinates.

The angle is always assigned. For a circle, only vertices[0] is copied from position; scale is ignored, the stored radius is not changed, and geometry is untouched. For every other type, the base geometry is transformed by angle, scale, and position into the existing vertices objects. The method returns undefined.

Polygon transforms read from geometry.vertices, so editing the current vertices array does not change the local shape used by the next update. Malformed or inconsistent deserialized data can cause the transform to fail when its geometry and output arrays have different lengths.

Parameters

NameDescriptionDefault
shape

Collider to update.

—
position

World position of the body.

—
angle

Body orientation in radians.

—
scale

Body scale for polygon vertices.

—

staticgetVertices<T extends Collider2D>(shape: T, axis: Vector2, out?: Vector2[]): Vector2[]

View source

Returns the vertices used to project the collider on an axis.

Polygon colliders return their live shape.vertices array directly and ignore out. For non-polygon types, it creates two vectors by adding and subtracting shape.vertices[1].x * axis from shape.vertices[0], writes them to out[0] and out[1], and returns out. It does not clear extra entries in a reused output array, and it uses the axis as supplied without normalizing it.

Type Parameters

T extends Collider2D

Parameters

NameDescriptionDefault
shape

Collider whose vertices are requested.

—
axis

Projection axis or direction vector.

—
out?

Output array for non-polygon shapes.

[]

Returns

The live polygon vertices or supplied output array.

staticgetArea(shape: Collider2D): number

View source

Returns the collider area value.

Area calculation is not implemented; this method currently returns 0 for both polygon and non-polygon shapes.

Parameters

NameDescriptionDefault
shape

Collider whose area would be calculated.

—

Returns

Always 0 in the current implementation.

staticcalcInertia(shape: Collider2D, mass: number): number

View source

Calculates a collider’s moment of inertia for a supplied mass.

Circles use 0.5 * mass * radius ** 2, where radius is shape.vertices[1].x. Polygon-like shapes use the current vertex array and a polygon summation; angle, geometry, and world position are not consulted. There is no input validation. Empty, collinear, or otherwise zero-area polygon data normally produces NaN.

Parameters

NameDescriptionDefault
shape

Collider whose inertia is calculated.

—
mass

Mass multiplier.

—

Returns

The calculated inertia, or the arithmetic result for degenerate input.