result
SourceValidates one raw vector and returns an explicit result.
const position = Vector2.result({ x: 16, y: 24 })
Data Structures
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.
// 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)
Returns the sum of two branded vectors.
subtractReturns the component-wise difference between two branded vectors.
scaleScales one vector by one validated finite scalar.
lengthSquaredReturns the squared length of one vector.
lengthReturns the length of one vector.
normalizeReturns the normalized vector when its length is non-zero.
normalizeOrZeroReturns the normalized vector or the canonical zero vector when the input length is zero.
Non-throwing constructor boundaries that validate raw input and produce branded vectors.
Validates one raw vector and returns an explicit result.
const position = Vector2.result({ x: 16, y: 24 })
Validates one raw vector and returns null on failure.
Checks whether one raw vector already satisfies the vector invariants.
Returns the canonical zero vector.
Pure readers and views that expose branded vector components without mutation.
Immutable vector math and normalization helpers on already-validated values.
Returns the sum of two branded vectors.
Returns the component-wise difference between two branded vectors.
Scales one vector by one validated finite scalar.
Returns the squared length of one vector.
Returns the length of one vector.
Returns the normalized vector when its length is non-zero.
const direction = Vector2.normalize(Vector2.zero())
if (!direction.ok) {
return
}
Returns the normalized vector or the canonical zero vector when the input length is zero.