Vector is a 3D vector with x, y, z components. World positions,
velocities, normals, trace endpoints - almost everything spatial is a
Vector. If you’re writing game logic, you’ll use this type more than
any other.
All Vector arithmetic methods return a new Vector - the original is
never modified. The exceptions are
normalize() and direct property
assignment (vec.x = 10), which mutate in place.Overview
Creating vectors
Constructor and static factories.
Arithmetic
Add, subtract, multiply, scale - all return new instances.
Length & distance
Magnitude, squared variants, and distance between points.
Products
Dot product, cross product.
Normalization
Unit vectors - mutating and non-mutating.
Utility
Clone, validity checks, conversion, string output.
Properties
Creating vectors
Constructor
Vector.zero
(0, 0, 0) vector.
Vector.one
(1, 1, 1) vector. Useful as a default scale or as a
starting multiplier.
Arithmetic
All arithmetic methods return a new Vector. The original is never modified.add
sub
mul
x*x, y*y, z*z. Returns a new
Vector. This is not dot or cross product - it’s the Hadamard product.
scale
Length & distance
length
√(x² + y² + z²).
lengthSqr
x² + y² + z². Faster than length()
because it skips the square root - use it when you’re comparing
magnitudes, not when you need the actual value.
length2D
√(x² + y²). Ignores the
vertical component. Useful for horizontal speed or ground-plane distance.
distTo
vec.sub(other).length() but slightly faster.
distToSqr
lengthSqr - no square root.
Products
dot
x*ox + y*oy + z*oz. Measures how much
two vectors point in the same direction.
Returns
number:
- Positive → vectors point roughly the same way
0→ perpendicular- Negative → point away from each other
cross
Normalization
normalize
this.
normalized
Utility
isZero
true if all components are within epsilon of zero. Default
epsilon is 1e-6.
isFinite
true if all three components are finite numbers (not NaN,
not ±Infinity).
clone
toArray
[x, y, z] as a plain array.
toString
Static methods
Vector.lerp
t=0 returns a, t=1
returns b, t=0.5 returns the midpoint. Not clamped - t outside
[0, 1] extrapolates.
- Smooth follow
- Midpoint
- Predictive
Vector.min
(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)).
Vector.max
(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)).
Common patterns
Real-world patterns that combine Vector methods with the Math API.- Direction + distance
- Is enemy behind me?
- Project onto plane
- Bounding box check