Data Structures

Vector2

Branded two-dimensional vectors with explicit validation.

Raw { x, y } input is validated once at the constructor boundary. All other helpers work on branded vectors only and remain pure and immutable.

This is the common math value that game code threads through position, velocity, input direction, camera motion, and collision helpers. Use it when vectors should stay explicitly validated and reusable across ECS and helper modules.

Examples

// Validate one raw velocity before storing or reusing it elsewhere.
const velocity = Vector2.result({ x: 3, y: 4 })
if (!velocity.ok) return

// Derive a movement direction from the branded vector.
const direction = Vector2.normalizeOrZero(velocity.value)

Construction

Non-throwing constructor boundaries that validate raw input and produce branded vectors.

result

Source

Validates one raw vector and returns an explicit result.

const position = Vector2.result({ x: 16, y: 24 })

option

Source

Validates one raw vector and returns null on failure.

is

Source

Checks whether one raw vector already satisfies the vector invariants.

zero

Source

Returns the canonical zero vector.

Accessors

Pure readers and views that expose branded vector components without mutation.

toRaw

Source

Converts one branded vector back to a fresh raw object.

components

Source

Exposes the branded components of one vector.

x

Source

Reads the x component of one branded vector.

y

Source

Reads the y component of one branded vector.

Operations

Immutable vector math and normalization helpers on already-validated values.

add

Source

Returns the sum of two branded vectors.

subtract

Source

Returns the component-wise difference between two branded vectors.

scale

Source

Scales one vector by one validated finite scalar.

lengthSquared

Source

Returns the squared length of one vector.

length

Source

Returns the length of one vector.

normalize

Source

Returns the normalized vector when its length is non-zero.

const direction = Vector2.normalize(Vector2.zero())
if (!direction.ok) {
  return
}

normalizeOrZero

Source

Returns the normalized vector or the canonical zero vector when the input length is zero.