Vec2

A two-dimensional vector. Used to represent coordinates in 2D space.

Constructor

new Vec2(x, y)

Create a new Vec2.
Parameters:
NameTypeDefaultDescription
xnumber0X-value of the vector
ynumber0Y-value of the vector

Classes

from

Methods

add(other) → {Vec2}

Add two vectors together.
Parameters:
NameTypeDescription
otherVec2Other vector to add
Returns:
New Vec2 with resulting values
Type: 
Vec2

addv(x, y) → {Vec2}

Add x and y values to the vector.
Parameters:
NameTypeDescription
xnumberX-value to add
ynumberY-value to add
Returns:
New Vec2 with resulting values
Type: 
Vec2

angle() → {number}

Get the angle of the vector (in radians).
Returns:
Angle of vector
Type: 
number

ceil(v) → {Vec2}

Ceiling the values of the vector. Can be used to round to a specified precision.
Parameters:
NameTypeDefaultDescription
vnumber1(Optional) Multiplies values by this value before ceiling, then divides it afterwords. Useful for rounding to a specific precision or specific decimal places.
Returns:
Resulting rounded up Vec2
Type: 
Vec2

copy() → {Vec2}

Get an exact copy of the vector.
Returns:
Copy of vector
Type: 
Vec2

dist(other) → {number}

Get the distance (magnitude) from the vector to another.
Parameters:
NameTypeDescription
otherVec2Other Vec2 to get distance to
Returns:
Distance between the two vectors
Type: 
number

div(other) → {Vec2}

Divide the vector by another.
Parameters:
NameTypeDescription
otherVec2Other vector to divide by
Returns:
New Vec2 with resulting values
Type: 
Vec2

divv(val) → {Vec2}

Divide the vector by a scalar.
Parameters:
NameTypeDescription
valnumberScalar value
Returns:
New Vec2 with scaled values
Type: 
Vec2

dot(other) → {number}

Get the dot product of the vector with another.
Parameters:
NameTypeDescription
otherVec2Other Vec2 to use
Returns:
Dot product of the two vectors
Type: 
number

dotv(x, y) → {number}

Get the dot product of the vector with specific x and y values.
Parameters:
NameTypeDescription
xnumberX-value to use
ynumberY-value to use
Returns:
Resulting dot product
Type: 
number

equals(other, range) → {boolean}

Check if another vector is equal within an optionally specified range.
Parameters:
NameTypeDefaultDescription
otherVec2Other vector to compare with
rangenumber0.001(Optional) Precision of the comparison. Set to 0.001 by default, this value is useful for avoiding floating-point precision issues (defaults to 0.001).
Returns:
Whether or not the vectors are equal, within the specified range.
Type: 
boolean

floor(v) → {Vec2}

Floor the values of the vector. Can be used to round to a specified precision.
Parameters:
NameTypeDefaultDescription
vnumber1(Optional) Multiplies values by this value before flooring, then divides it afterwords. Useful for rounding to a specific precision or specific decimal places.
Returns:
Resulting floored Vec2
Type: 
Vec2

inverseTransform(matrix) → {Vec2}

Inversely transform the vector using a gl-matrix matrix.
Parameters:
NameTypeDescription
matrixMatrix to inversely transform with
Returns:
Inversely transformed vector
Type: 
Vec2

invert() → {Vec2}

Invert the vector (inverts the signs of the values).
Returns:
Inverted Vec2
Type: 
Vec2

isZero() → {boolean}

Check if the vector is zero or not (both values equal to zero).
Returns:
Whether the vector is zero
Type: 
boolean

lerp(other, alpha) → {Vec2}

Perform linear interpolation between the vector and another.
Parameters:
NameTypeDescription
otherVec2Other Vec2 to interpolate to
alphanumberProgress between the values (0 - 1)
Returns:
Resulting Vec2
Type: 
Vec2

mag() → {number}

Get the magnitude/length of the vector.
Returns:
Magnitude of vector
Type: 
number

mirrorOver(other) → {Vec2}

Mirror over another vector.
Parameters:
NameTypeDescription
otherVec2Vector to mirror over
Returns:
Mirrored vector
Type: 
Vec2

mul(other) → {Vec2}

Multiply two vectors together.
Parameters:
NameTypeDescription
otherVec2Other vector to multiply by
Returns:
New Vec2 with resulting values
Type: 
Vec2

mulv(val) → {Vec2}

Multiply the vector by a scalar.
Parameters:
NameTypeDescription
valnumberScalar value
Returns:
New Vec2 with scaled values
Type: 
Vec2

norm() → {Vec2}

Get the normalization of the vector.
Returns:
Normalized version of the vector
Type: 
Vec2

normWith(other) → {Vec2}

Get the normal vector from another vector.
Parameters:
NameTypeDescription
otherVec2Other Vec2 to use
Returns:
Resulting normal vector
Type: 
Vec2

perp() → {Vec2}

Calculate the perpendicular vector.
Returns:
Perpendicular vector
Type: 
Vec2

project(other) → {Vec2}

Project onto another vector.
Parameters:
NameTypeDescription
otherVec2Vector to project onto
Returns:
Projected vector
Type: 
Vec2

rot(rad) → {Vec2}

Get a new vector by rotating (in radians) the existing vector.
Parameters:
NameTypeDescription
radnumberRadians to rotate by
Returns:
Resulting rotated vector
Type: 
Vec2

sign() → {Vec2}

Get a vector denoting the signs of each axis.
Returns:
Sign vector
Type: 
Vec2

smoothLerp(other, alpha, edge) → {Vec2}

Perform linear interpolation between the vector and another, smoothly.
Parameters:
NameTypeDefaultDescription
otherVec2Other Vec2 to interpolate to
alphanumberProgress between the values (0 - 1)
edgenumber0.1Amount (percentage) of the interpolation to smooth (0 - 1)
Returns:
Resulting Vec2
Type: 
Vec2

sub(other) → {Vec2}

Subtract a vector from another.
Parameters:
NameTypeDescription
otherVec2Other vector to subtract by
Returns:
New Vec2 with resulting values
Type: 
Vec2

subv(x, y) → {Vec2}

Subtract x and y values from the vector.
Parameters:
NameTypeDescription
xnumberX-value to subtract
ynumberY-value to subtract
Returns:
New Vec2 with resulting values
Type: 
Vec2

toArray() → {Array.<number>}

Convert the Vec2 to a standard array.
Returns:
Vector as an array
Type: 
Array.<number>

toGlArray() → {Array.<number>}

Convert the Vec2 to a gl-matrix array.
Returns:
Gl-matrix array
Type: 
Array.<number>

toScaleMatrix()

Convert to a scaling matrix.
Returns:
Scaling matrix

toTranslationMatrix()

Convert to a translation matrix.
Returns:
Translation matrix

transform(matrix) → {Vec2}

Transform the vector using a gl-matrix matrix.
Parameters:
NameTypeDescription
matrixMatrix to transform with
Returns:
Transformed vector
Type: 
Vec2

(static) fromAngle(rads) → {Vec2}

Create a new Vec2 from an angle
Parameters:
NameTypeDescription
radsnumberAngle in radians
Returns:
Resulting vector
Type: 
Vec2

(static) fromArray(array) → {Vec2}

Create a new Vec2 from an array.
Parameters:
NameTypeDescription
array
Returns:
Resulting vector
Type: 
Vec2

(static) max(…vectors) → {Vec2}

Given multiple vectors, find the highest coordinates in both directions.
Parameters:
NameTypeAttributesDescription
vectorsVec2<repeatable>
Vectors to compare
Returns:
Resulting maximum vector
Type: 
Vec2

(static) min(…vectors) → {Vec2}

Given multiple vectors, find the lowest coordinates in both directions.
Parameters:
NameTypeAttributesDescription
vectorsVec2<repeatable>
Vectors to compare
Returns:
Resulting minimum vector
Type: 
Vec2

(static) random(radius) → {Vec2}

Create a random Vec2 within a specific radius.
Parameters:
NameTypeDefaultDescription
radiusnumber1Radius to generate in
Returns:
Resulting random vector
Type: 
Vec2

(static) randomOuter(radius) → {Vec2}

Create a random Vec2 on the edge of a specific radius.
Parameters:
NameTypeDefaultDescription
radiusnumber1Radius to generate on
Returns:
Resulting random vector
Type: 
Vec2