Vector¶
Overview¶
Vectors are used to represent positional and colour data, and can contain between 2, 3, or 4 float values.
Examples¶
local v = Vector2.new(0.3, 10.0)
v = v + Vector.new(5.0, 0.1)
print(v.x, v.y) -- 5.3, 10.1
print(v:Dot(Vector2.new(1.0, 0.0))) -- 5.3
local v = Vector3.new(5.0)
print(v.x, v.y, v.z) -- 5.0, 5.0, 5.0
v.y = -3.1
v.z = v.z - 9.5
print(v.x, v.y, v.z) -- 5.0, -3.1, -4.5
v = v:Abs()
print(v.x, v.y, v.z) -- 5.0, 3.1, 4.5
local colour = Vector4.new(1.0, 0.5, 0.0, 1.0)
colour = colour * Vector4.new(1.0, 0.0, 0.0, 1.0)
print(colour.x, colour.y, colour.z, colour.w) -- 1.0, 0.0, 0.0, 1.0
print(colour.r, colour.g, colour.b, colour.a) -- 1.0, 0.0, 0.0, 1.0
local rgbOnly = colour.rgb
rgbOnly = rgbOnly + Vector3.new(0.0, 0.3, 0.7)
print(rgbOnly.x, rgbOnly.y, rgbOnly.z) -- 1.0, 0.3, 0.7
print(rgbOnly.r, rgbOnly.g, rgbOnly.b) -- 1.0, 0.3, 0.7
Common Methods and Properties¶
The methods/properties listed under this section are available on vectors regardless of their size.
Info
Whenever the Vector type appears as a parameter/return type, unless otherwise stated this represents a vector of the same size as the object that function was called on.
Abs¶
Computes the absolute value representation for the provided vector and returns it.
| Returns | Description |
|---|---|
| Vector | Contains the absolute value representation of the provided vector. |
Ceil¶
Returns a vector where each element is the smallest integer that's greater than the original element.
| Returns | Description |
|---|---|
| Vector | Contains the result from ceil'ing each of the original vector's elements. |
Floor¶
Returns a vector where each element is the largest integer that's not greater than the original element.
| Returns | Description |
|---|---|
| Vector | Contains the result from floor'ing each of the original vector's elements. |
Dot¶
Returns the dot-product of two vectors.
| Parameter | Type | Description |
|---|---|---|
| other | Vector | Vector to compare elements to. |
| Returns | Description |
|---|---|
| Vector | Contains the maximum elements of both vectors. |
Example
Max¶
Returns an element-wise maximum between two vectors.
| Parameter | Type | Description |
|---|---|---|
| other | Vector | Vector to compare elements to. |
| Returns | Description |
|---|---|
| Vector | Contains the maximum elements of both vectors. |
Example
Min¶
Returns an element-wise minimum between two vectors.
| Parameter | Type | Description |
|---|---|---|
| other | Vector | Vector to compare elements to. |
| Returns | Description |
|---|---|
| Vector | Contains the minimum elements of both vectors. |
Example
Normalise¶
Normalises the current vector, rescaling its elements so it becomes a vector of length 1. Does nothing if all elements of the vector are 0.
Example
NormaliseSafe¶
Returns a normalised version of the provided vector, with its elements rescaled so it becomes a vector of length 1.
If all of the provided vector's elements are 0, returns the fallback vector instead.
| Parameter | Type | Description |
|---|---|---|
| fallback | Vector | Value to return if all elements on the vector this was called on are 0. |
| Returns | Description |
|---|---|
| Vector | Normalised version of the caller or the fallback if the caller had length 0 |
Example
Properties¶
| Name | Type | Description |
|---|---|---|
| isNormal | Bool | (Read-only) True if this is a unit-length vector. |
| length | Float | (Read-only) Length value for this vector. |
| lengthSq | Float | (Read-only) Squared length value for this vector. Faster to calculate than length. |
| invLength | Float | (Read-only) Inverse of the length value for this vector, equivalent to 1.0 / length. |
Vector2¶
Additional properties and methods provided for Vector2 types.
Constructors¶
function Vector2.new() -> Vector2
function Vector2.new(v : Float) -> Vector2
function Vector2.new(x : Float, y : Float) -> Vector2
Creates and returns a vector with the provided values, or with all elements set to 0 if no arguments were provided.
| Parameter | Type | Description |
|---|---|---|
| v | Float | Value to set all elements of the newly created vector to. |
| x | Float | Value to set the first element of the newly created vector to. |
| y | Float | Value to set the second element of the newly created vector to. |
| Returns | Description |
|---|---|
| Vector2 | The newly created element |
Properties¶
| Name | Type | Description |
|---|---|---|
| x | Float | First element in this vector. |
| y | Float | Second element in this vector. |
| u | Float | First element in this vector, aliasing x. |
| v | Float | Second element in this vector, aliasing y. |
Info
The following aliases are supported:
- u/v -> x/y
Vector3¶
Additional properties and methods provided for Vector3 types.
Constructors¶
function Vector3.new() -> Vector3
function Vector3.new(v : Float) -> Vector3
function Vector3.new(x : Float, y : Float, z : Float) -> Vector3
Creates and returns a vector with the provided values, or with all elements set to 0 if no arguments were provided.
| Parameter | Type | Description |
|---|---|---|
| v | Float | Value to set all elements of the newly created vector to. |
| x | Float | Value to set the first element of the newly created vector to. |
| y | Float | Value to set the second element of the newly created vector to. |
| z | Float | Value to set the third element of the newly created vector to. |
| Returns | Description |
|---|---|
| Vector3 | The newly created element |
Cross¶
Returns the cross-product of two 3D vectors.
| Parameter | Type | Description |
|---|---|---|
| other | Vector3 | Vector to calculate the cross-product against. |
| Returns | Description |
|---|---|
| Vector3 | Contains the maximum elements of both vectors. |
Example
Reflect¶
Calculates the reflection of the current vector along the provided normal.
Warning
Both vectors should be normalised prior to calling this method to ensure a sane value is returned.
| Parameter | Type | Description |
|---|---|---|
| normal | Vector3 | Vector treated as the normal for the surface to calculate the reflection on. |
| Returns | Description |
|---|---|
| Vector3 | Contains the reflection of the source vector on the provided surface normal. |
Example
Rotate¶
Calculates the result of rotating this vector by the provided quaternion.
| Parameter | Type | Description |
|---|---|---|
| rotation | Quaternion | Quaternion describing the rotation to apply. |
| Returns | Description |
|---|---|
| Vector3 | Contains the result of rotating the source vector by the provided quaternion. |
Example
Properties¶
| Name | Type | Description |
|---|---|---|
| x | Float | First element in this vector. |
| y | Float | Second element in this vector. |
| z | Float | Third element in this vector. |
Info
The following aliases are supported:
- r/g/b -> x/y/z
Vector4¶
Additional properties and methods provided for Vector4 types.
Constructors¶
function Vector4.new() -> Vector4
function Vector4.new(v : Float) -> Vector4
function Vector4.new(x : Float, y : Float, z : Float, w : Float) -> Vector4
function Vector4.new(xy : Vector2, zw : Vector2) -> Vector4
Creates and returns a vector with the provided values, or with all elements set to 0 if no arguments were provided.
| Parameter | Type | Description |
|---|---|---|
| v | Float | Value to set all elements of the newly created vector to. |
| x | Float | Value to set the first element of the newly created vector to. |
| y | Float | Value to set the second element of the newly created vector to. |
| z | Float | Value to set the third element of the newly created vector to. |
| w | Float | Value to set the fourth element of the newly created vector to. |
| xy | Vector2 | Value to set the first and second elements of the newly created vector to. |
| zw | Vector2 | Value to set the third and fourth elements of the newly created vector to. |
| Returns | Description |
|---|---|
| Vector4 | The newly created element |
Properties¶
| Name | Type | Description |
|---|---|---|
| x | Float | First element in this vector. |
| y | Float | Second element in this vector. |
| z | Float | Third element in this vector. |
| w | Float | Fourth element in this vector. |
| xyz | Vector3 | (Read-only) Returns x, y, and z as a separate vector. |
| xy | Vector2 | (Read-only) Returns x and y as a separate vector. |
| zw | Vector2 | (Read-only) Returns z and w as a separate vector. |
Info
The following aliases are supported:
- r/g/b/a -> x/y/z/w
- rgb -> xyz