Constructor
new Vec2(x, y)
Create a new Vec2.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | 0 | X-value of the vector |
y | number | 0 | Y-value of the vector |
- Source
Classes
Methods
add(other) → {Vec2}
Add two vectors together.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other vector to add |
- Source
Returns:
New Vec2 with resulting values
- Type:
- Vec2
addv(x, y) → {Vec2}
Add x and y values to the vector.
Parameters:
Name | Type | Description |
---|---|---|
x | number | X-value to add |
y | number | Y-value to add |
- Source
Returns:
New Vec2 with resulting values
- Type:
- Vec2
angle() → {number}
Get the angle of the vector (in radians).
- Source
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:
Name | Type | Default | Description |
---|---|---|---|
v | number | 1 | (Optional) Multiplies values by this value before ceiling, then divides it afterwords. Useful for rounding to a specific precision or specific decimal places. |
- Source
Returns:
Resulting rounded up Vec2
- Type:
- Vec2
copy() → {Vec2}
Get an exact copy of the vector.
- Source
Returns:
Copy of vector
- Type:
- Vec2
dist(other) → {number}
Get the distance (magnitude) from the vector to another.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other Vec2 to get distance to |
- Source
Returns:
Distance between the two vectors
- Type:
- number
div(other) → {Vec2}
Divide the vector by another.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other vector to divide by |
- Source
Returns:
New Vec2 with resulting values
- Type:
- Vec2
divv(val) → {Vec2}
Divide the vector by a scalar.
Parameters:
Name | Type | Description |
---|---|---|
val | number | Scalar value |
- Source
Returns:
New Vec2 with scaled values
- Type:
- Vec2
dot(other) → {number}
Get the dot product of the vector with another.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other Vec2 to use |
- Source
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:
Name | Type | Description |
---|---|---|
x | number | X-value to use |
y | number | Y-value to use |
- Source
Returns:
Resulting dot product
- Type:
- number
equals(other, range) → {boolean}
Check if another vector is equal within an optionally specified range.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | Vec2 | Other vector to compare with | |
range | number | 0.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). |
- Source
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:
Name | Type | Default | Description |
---|---|---|---|
v | number | 1 | (Optional) Multiplies values by this value before flooring, then divides it afterwords. Useful for rounding to a specific precision or specific decimal places. |
- Source
Returns:
Resulting floored Vec2
- Type:
- Vec2
inverseTransform(matrix) → {Vec2}
Inversely transform the vector using a gl-matrix matrix.
Parameters:
Name | Type | Description |
---|---|---|
matrix | Matrix to inversely transform with |
- Source
Returns:
Inversely transformed vector
- Type:
- Vec2
invert() → {Vec2}
Invert the vector (inverts the signs of the values).
- Source
Returns:
Inverted Vec2
- Type:
- Vec2
isZero() → {boolean}
Check if the vector is zero or not (both values equal to zero).
- Source
Returns:
Whether the vector is zero
- Type:
- boolean
lerp(other, alpha) → {Vec2}
Perform linear interpolation between the vector and another.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other Vec2 to interpolate to |
alpha | number | Progress between the values (0 - 1) |
- Source
Returns:
Resulting Vec2
- Type:
- Vec2
mag() → {number}
Get the magnitude/length of the vector.
- Source
Returns:
Magnitude of vector
- Type:
- number
mirrorOver(other) → {Vec2}
Mirror over another vector.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Vector to mirror over |
- Source
Returns:
Mirrored vector
- Type:
- Vec2
mul(other) → {Vec2}
Multiply two vectors together.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other vector to multiply by |
- Source
Returns:
New Vec2 with resulting values
- Type:
- Vec2
mulv(val) → {Vec2}
Multiply the vector by a scalar.
Parameters:
Name | Type | Description |
---|---|---|
val | number | Scalar value |
- Source
Returns:
New Vec2 with scaled values
- Type:
- Vec2
norm() → {Vec2}
Get the normalization of the vector.
- Source
Returns:
Normalized version of the vector
- Type:
- Vec2
normWith(other) → {Vec2}
Get the normal vector from another vector.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other Vec2 to use |
- Source
Returns:
Resulting normal vector
- Type:
- Vec2
perp() → {Vec2}
Calculate the perpendicular vector.
- Source
Returns:
Perpendicular vector
- Type:
- Vec2
project(other) → {Vec2}
Project onto another vector.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Vector to project onto |
- Source
Returns:
Projected vector
- Type:
- Vec2
rot(rad) → {Vec2}
Get a new vector by rotating (in radians) the existing vector.
Parameters:
Name | Type | Description |
---|---|---|
rad | number | Radians to rotate by |
- Source
Returns:
Resulting rotated vector
- Type:
- Vec2
sign() → {Vec2}
Get a vector denoting the signs of each axis.
- Source
Returns:
Sign vector
- Type:
- Vec2
smoothLerp(other, alpha, edge) → {Vec2}
Perform linear interpolation between the vector and another, smoothly.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | Vec2 | Other Vec2 to interpolate to | |
alpha | number | Progress between the values (0 - 1) | |
edge | number | 0.1 | Amount (percentage) of the interpolation to smooth (0 - 1) |
- Source
Returns:
Resulting Vec2
- Type:
- Vec2
sub(other) → {Vec2}
Subtract a vector from another.
Parameters:
Name | Type | Description |
---|---|---|
other | Vec2 | Other vector to subtract by |
- Source
Returns:
New Vec2 with resulting values
- Type:
- Vec2
subv(x, y) → {Vec2}
Subtract x and y values from the vector.
Parameters:
Name | Type | Description |
---|---|---|
x | number | X-value to subtract |
y | number | Y-value to subtract |
- Source
Returns:
New Vec2 with resulting values
- Type:
- Vec2
toArray() → {Array.<number>}
Convert the Vec2 to a standard array.
- Source
Returns:
Vector as an array
- Type:
- Array.<number>
toGlArray() → {Array.<number>}
Convert the Vec2 to a gl-matrix array.
- Source
Returns:
Gl-matrix array
- Type:
- Array.<number>
toScaleMatrix()
Convert to a scaling matrix.
- Source
Returns:
Scaling matrix
toTranslationMatrix()
Convert to a translation matrix.
- Source
Returns:
Translation matrix
transform(matrix) → {Vec2}
Transform the vector using a gl-matrix matrix.
Parameters:
Name | Type | Description |
---|---|---|
matrix | Matrix to transform with |
- Source
Returns:
Transformed vector
- Type:
- Vec2
(static) fromAngle(rads) → {Vec2}
Create a new Vec2 from an angle
Parameters:
Name | Type | Description |
---|---|---|
rads | number | Angle in radians |
- Source
Returns:
Resulting vector
- Type:
- Vec2
(static) fromArray(array) → {Vec2}
Create a new Vec2 from an array.
Parameters:
Name | Type | Description |
---|---|---|
array |
- Source
Returns:
Resulting vector
- Type:
- Vec2
(static) max(…vectors) → {Vec2}
Given multiple vectors, find the highest coordinates in both directions.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
vectors | Vec2 | <repeatable> | Vectors to compare |
- Source
Returns:
Resulting maximum vector
- Type:
- Vec2
(static) min(…vectors) → {Vec2}
Given multiple vectors, find the lowest coordinates in both directions.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
vectors | Vec2 | <repeatable> | Vectors to compare |
- Source
Returns:
Resulting minimum vector
- Type:
- Vec2
(static) random(radius) → {Vec2}
Create a random Vec2 within a specific radius.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
radius | number | 1 | Radius to generate in |
- Source
Returns:
Resulting random vector
- Type:
- Vec2
(static) randomOuter(radius) → {Vec2}
Create a random Vec2 on the edge of a specific radius.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
radius | number | 1 | Radius to generate on |
- Source
Returns:
Resulting random vector
- Type:
- Vec2