Vector2
A 2D vector object, e.g. a screen position or a size.
Arithmetic operators work component-wise:
vec2 + vec2, vec2 + number, number + vec2 ( same for - and * )vec2 / vec2, vec2 / number-vec2
Constructor
Vector2(x: number?, y: number?): Vector2
Fields
| Name | Type | Description |
|---|
x | number | The X coordinate. |
y | number | The Y coordinate. |
origin | Vector2 | The shared zero vector ( 0, 0 ), must not be modified. |
Operators
Static functions
copy | Creates a new independent copy of the vector. |
getAngle | Returns the angle of the vector in degrees, measured clockwise from the +y axis in range ( 0, 360 ]. |
getLength | Calculates the length of the vector. |
getLengthSqr | Calculates the squared length of the vector. |
getLerped | Returns a copy of the vector linearly interpolated to another vector or a number. |
lerp | Linear interpolation between the vector and another vector or a number. |
normalize | Scales the vector to the length of 1. |
rotate | Rotates the vector counter-clockwise ( from +x towards +y ) by the given degrees. |
scale | Multiplies all coordinates of the vector by the number. |
function Vector2.copy(self: Vector2): Vector2
Creates a new independent copy of the vector.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
Returns
| Type | Name | Description |
|---|
Vector2 | copy | The new vector. |
function Vector2.getAngle(self: Vector2, up?: Vector2): number
Returns the angle of the vector in degrees, measured clockwise from the +y axis in range ( 0, 360 ].
If up is given, its angle is added to the result.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
upoptional | Vector2 | The direction of the angle. |
Returns
| Type | Name | Description |
|---|
number | degrees | The angle of the vector. |
function Vector2.getLength(self: Vector2): number
Calculates the length of the vector.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
Returns
| Type | Name | Description |
|---|
number | length | The length of the vector. |
function Vector2.getLengthSqr(self: Vector2): number
Calculates the squared length of the vector.
Faster than getLength, use it to compare lengths.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
Returns
| Type | Name | Description |
|---|
number | length | The squared length of the vector. |
function Vector2.getLerped(self: Vector2, target: Vector2 | number, frac: number): Vector2
Returns a copy of the vector linearly interpolated to another vector or a number.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
target | Vector2 | number | The target vector or a number ( used for every coordinate ). |
frac | number | The interpolation factor. |
Returns
| Type | Name | Description |
|---|
Vector2 | lerped_vec2 | The new vector. |
function Vector2.lerp(self: Vector2, target: Vector2 | number, frac: number): Vector2
Linear interpolation between the vector and another vector or a number.
frac of 0 keeps the vector, 1 sets it to the target, values outside [0, 1] extrapolate.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
target | Vector2 | number | The target vector or a number ( used for every coordinate ). |
frac | number | The interpolation factor. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, interpolated. |
function Vector2.normalize(self: Vector2): Vector2
Scales the vector to the length of 1.
Zero vector stays unchanged.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, normalized. |
function Vector2.rotate(self: Vector2, degrees: number): Vector2
Rotates the vector counter-clockwise ( from +x towards +y ) by the given degrees.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
degrees | number | The rotation angle. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, rotated. |
function Vector2.scale(self: Vector2, scale: number): Vector2
Multiplies all coordinates of the vector by the number.
Parameters
| Name | Type | Description |
|---|
self | Vector2 | The vector. |
scale | number | The scale factor. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, scaled. |
Methods
add | Adds another vector or a number ( to every coordinate ) to the vector. |
cross | Calculates the 2D cross product ( Z component of the 3D cross product ) of two vectors. |
div | Divides the vector by another vector ( component-wise ) or a number. |
dot | Calculates the dot product of two vectors. |
getDistance | Calculates the distance between two vectors. |
getDistanceSqr | Calculates the squared distance between two vectors. |
getNormalized | Returns a copy of the vector with the length of 1. |
getRotated | Returns a copy of the vector rotated counter-clockwise ( from +x towards +y ) by the given degrees. |
getScaled | Returns a scaled copy of the vector. |
isNear | Checks if the vector is equal to the given vector with the given tolerance. |
isZero | Checks if all coordinates of the vector are exactly zero. |
mul | Multiplies the vector by another vector ( component-wise ) or a number. |
negate | Negates all coordinates of the vector. |
set | Copies the coordinates of another vector into this vector. |
setUnpacked | Sets the x and y coordinates of the vector. |
sub | Subtracts another vector or a number ( from every coordinate ) from the vector. |
unpack | Returns the x and y coordinates of the vector. |
zero | Sets all coordinates of the vector to zero. |
function Vector2:add(value: Vector2 | number): Vector2
Adds another vector or a number ( to every coordinate ) to the vector.
Parameters
| Name | Type | Description |
|---|
value | Vector2 | number | The other vector or a number. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, with the sum. |
function Vector2:cross(vector: Vector2): number
Calculates the 2D cross product ( Z component of the 3D cross product ) of two vectors.
Positive if vector is counter-clockwise from the vector, negative if clockwise, zero if parallel.
Parameters
| Name | Type | Description |
|---|
vector | Vector2 | The other vector. |
Returns
| Type | Name | Description |
|---|
number | cross_product | The cross product of two vectors. |
function Vector2:div(value: Vector2 | number): Vector2
Divides the vector by another vector ( component-wise ) or a number.
Modifies the vector in place, division by zero is not checked and results in inf / nan components.
Parameters
| Name | Type | Description |
|---|
value | Vector2 | number | The divisor vector or number. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, divided. |
function Vector2:dot(vector: Vector2): number
Calculates the dot product of two vectors.
Parameters
| Name | Type | Description |
|---|
vector | Vector2 | The other vector. |
Returns
| Type | Name | Description |
|---|
number | dot_product | The dot product of two vectors. |
function Vector2:getDistance(vector: Vector2): number
Calculates the distance between two vectors.
Parameters
| Name | Type | Description |
|---|
vector | Vector2 | The other vector. |
Returns
| Type | Name | Description |
|---|
number | distance | The distance between the two vectors. |
function Vector2:getDistanceSqr(vector: Vector2): number
Calculates the squared distance between two vectors.
Faster than getDistance, use it to compare distances.
Parameters
| Name | Type | Description |
|---|
vector | Vector2 | The other vector. |
Returns
| Type | Name | Description |
|---|
number | distance | The squared distance between the two vectors. |
function Vector2:getNormalized(): Vector2
Returns a copy of the vector with the length of 1.
Zero vector returns a zero copy.
Returns
| Type | Name | Description |
|---|
Vector2 | normalized_vec2 | The new vector. |
function Vector2:getRotated(degrees: number): Vector2
Returns a copy of the vector rotated counter-clockwise ( from +x towards +y ) by the given degrees.
Parameters
| Name | Type | Description |
|---|
degrees | number | The rotation angle. |
Returns
| Type | Name | Description |
|---|
Vector2 | rotated_vec2 | The new vector. |
function Vector2:getScaled(scale: number): Vector2
Returns a scaled copy of the vector.
Parameters
| Name | Type | Description |
|---|
scale | number | The scale factor. |
Returns
| Type | Name | Description |
|---|
Vector2 | scaled_vec2 | The new vector. |
function Vector2:isNear(vector: Vector2, tolerance: number): boolean
Checks if the vector is equal to the given vector with the given tolerance.
Each coordinate is compared separately: |a.x - b.x| <= tolerance and so on.
Parameters
| Name | Type | Description |
|---|
vector | Vector2 | The vector to check. |
tolerance | number | The maximum allowed difference per coordinate. |
Returns
| Type | Name | Description |
|---|
boolean | is_near | true if the vectors are equal within the tolerance, otherwise false. |
function Vector2:isZero(): boolean
Checks if all coordinates of the vector are exactly zero.
Returns
| Type | Name | Description |
|---|
boolean | is_zero | true if the vector is zero, false otherwise. |
function Vector2:mul(value: Vector2 | number): Vector2
Multiplies the vector by another vector ( component-wise ) or a number.
Parameters
| Name | Type | Description |
|---|
value | Vector2 | number | The other vector or a number. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, with the product. |
function Vector2:negate(): Vector2
Negates all coordinates of the vector.
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, negated. |
function Vector2:set(vector: Vector2): Vector2
Copies the coordinates of another vector into this vector.
Parameters
| Name | Type | Description |
|---|
vector | Vector2 | The vector to copy from. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector. |
function Vector2:setUnpacked(x: number, y: number): Vector2
Sets the x and y coordinates of the vector.
Parameters
| Name | Type | Description |
|---|
x | number | The x coordinate of the vector. |
y | number | The y coordinate of the vector. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector. |
function Vector2:sub(value: Vector2 | number): Vector2
Subtracts another vector or a number ( from every coordinate ) from the vector.
Parameters
| Name | Type | Description |
|---|
value | Vector2 | number | The other vector or a number. |
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector, with the difference. |
function Vector2:unpack(): number, number
Returns the x and y coordinates of the vector.
Returns
| Type | Name | Description |
|---|
number | x | The x coordinate of the vector. |
number | y | The y coordinate of the vector. |
function Vector2:zero(): Vector2
Sets all coordinates of the vector to zero.
Returns
| Type | Name | Description |
|---|
Vector2 | vec2 | The same vector. |