Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Nightre/Rapid.js/llms.txt
Use this file to discover all available pages before exploring further.
Vec2 is a 2D vector class used throughout Rapid.js for positions, velocities, origins, offsets, and directions. Most arithmetic methods — add, subtract, multiply, divide, normalized, inverted, abs, floor, ceil, clone, copy — return new Vec2 instances and leave the original unchanged. Mutating methods — set, normalize, perpendicular, invert, setToPolar, fix, clear — modify the vector in-place and return this to support chaining.
Constructor
x and y default to 0 when omitted.
Static Constants
These are shared singleton instances. Do not mutate them.| Constant | Value |
|---|---|
Vec2.ZERO | (0, 0) |
Vec2.ONE | (1, 1) |
Vec2.UP | (0, 1) |
Vec2.DOWN | (0, -1) |
Vec2.LEFT | (-1, 0) |
Vec2.RIGHT | (1, 0) |
Static Factory Methods
Vec2.fromAngle(angle)
Create a unit vector pointing in the direction given by angle in radians. Equivalent to new Vec2(Math.cos(angle), Math.sin(angle)).
Vec2.FromArray(array)
Convert an array of [x, y] pairs into an array of Vec2 instances.
Properties
| Property | Type | Description |
|---|---|---|
x | number | Horizontal component. |
y | number | Vertical component. |
Methods
set(x, y?)
Mutate this vector in-place. If y is omitted, both x and y are set to the value of the first argument. Returns this.
add(v)
Return a new Vec2 equal to this + v.
subtract(v) / sub(v)
Return a new Vec2 equal to this - v. sub is a short alias for subtract.
multiply(f)
Return a new Vec2 with components scaled by a scalar f, or multiplied component-wise by another Vec2.
mul(f)
Alias for multiply.
divide(f)
Return a new Vec2 with components divided by a scalar or component-wise by another Vec2.
dot(v)
Return the scalar dot product of this vector and v.
cross(v)
Return the 2D cross product (scalar) of this vector and v. Positive when v is to the left of this.
length()
Return the Euclidean length (√(x² + y²)).
squaredLength()
Return the squared length (x² + y²). Cheaper than length() when only comparisons are needed.
normalize()
Mutate this vector so its length is 1. If the vector is zero-length the components are set to 0. Returns this.
normalized()
Return a new unit vector in the same direction as this vector without mutating the original.
distanceTo(v)
Return the Euclidean distance between this point and v.
angle()
Return the angle of this vector in radians using Math.atan2(y, x). The result is in the range (-π, π].
perpendicular()
Rotate this vector 90° counter-clockwise in-place ((-y, x)). Returns this.
invert()
Negate both components in-place ((-x, -y)). Returns this.
inverted()
Return a new Vec2 with both components negated, leaving the original unchanged.
abs()
Return a new Vec2 with the absolute value of each component.
floor()
Return a new Vec2 with each component floored to the nearest integer.
ceil()
Return a new Vec2 with each component ceiled to the nearest integer.
clone()
Return a new Vec2 with the same x and y values.
copy()
Return a new Vec2 with the same x and y values. Identical to clone().
to(vec)
Copy vec’s x and y values into this instance. Mutates in-place.
equals(v)
Return true if both x and y are equal to those of v.
isZero()
Return true if both x and y are exactly 0.
isNotZero()
Return true if at least one component is non-zero.
isPrettyMuchZero()
Return true if both components have an absolute value below 0.0001. Useful for floating-point near-zero checks.
setToPolar(azimuth, radius?)
Set this vector to the Cartesian equivalent of polar coordinates: x = cos(azimuth) * radius, y = sin(azimuth) * radius. radius defaults to 1. Returns this.
fix(limit?)
Zero out any component whose absolute value is below limit (default 1e-13). Mutates in-place. Useful for eliminating floating-point noise after trigonometric operations.
fixed(limit?)
Return a new Vec2 with near-zero components zeroed out, without mutating the original.
clear()
Set both x and y to 0 in-place.
stringify()
Return a human-readable string in the form "Vec2(x, y)".