vec3
Info
This type supports +
, -
, *
and /
operators.
Tip
Multiplication is done faster by division. Consider this if you want to make your script more optimized.
Properties
Name | Type | Description |
---|---|---|
x |
number | X value |
y |
number | Y value |
z |
number | Z value |
__add
vec3 + {other: vec3}
-> vec3
Adds one vector to another.
Parameters
Name | Type | Description |
---|---|---|
other |
vec3 | Other vector |
Usage
local new = math.vec3(1, 0, 0) + math.vec3(0, 0, 1); -- x = 1, y = 0, z = 1
__sub
vec3 - {other: vec3}
-> vec3
Subtracts one vector from another.
Parameters
Name | Type | Description |
---|---|---|
other |
vec3 | Other vector |
Usage
local new = math.vec3(1, 0, 0) - math.vec3(0, 0, 1); -- x = 1, y = 0, z = -1
__mul
vec3 * {other: vec3}
-> vec3
vec3 * {other: number}
-> vec3
Multiplies vector by a number or another vector.
Parameters
Name | Type | Description |
---|---|---|
other |
vec3 / number | Other vector or number |
Usage
local new = math.vec3(1, 0, 0) * 5; -- x = 5, y = 0, z = 0
__div
vec3 / {other: vec3}
-> vec3
vec3 / {other: number}
-> vec3
Divides vector by a number or another vector.
Parameters
Name | Type | Description |
---|---|---|
other |
vec3 / number | Other vector or number |
Usage
local new = math.vec3(10, 15, 5) / 5; -- x = 2, y = 3, z = 1
unpack
unpack()
-> number, number, number
Unpacks the object.
Usage
local x, y, z = my_vec:unpack();
length
length()
-> number
Returns the length.
Usage
local vec = math.vec3(1, 0, 0);
local length = vec:length();
length2d
length2d()
-> number
Returns the length, but ignores Z value.
Usage
local vec = math.vec3(1, 0, 0);
local length = vec:length2d();
length_sqr
length_sqr()
-> number
Returns the squared length.
Usage
local vec = math.vec3(1, 0, 0);
local length = vec:length_sqr();
length2d_sqr
length2d_sqr()
-> number
Returns the squared length, but ignores Z value.
Usage
local vec = math.vec3(1, 0, 0);
local length = vec:length2d_sqr();
normalize
normalize()
-> vec3
Returns a normalized vector.
Usage
local vec = math.vec3(15, 33, 2);
local vec_normalized = vec:normalize();
cross
cross(val: vec3)
-> vec3
Returns the cross product of two vectors.
Parameters
Name | Type | Description |
---|---|---|
val |
vec3 | Other vector |
Usage
local vec = math.vec3(1, 0, 0);
local vec2 = math.vec3(0, 1, 0);
local cross_product = vec:cross(vec2);
dot
dot(val: vec3)
-> vec3
Returns the dot product of two vectors.
Parameters
Name | Type | Description |
---|---|---|
val |
vec3 | Other vector |
Usage
local vec = math.vec3(6, 0, 77);
local vec2 = math.vec3(1, 33, 7);
local dot_product = vec:dot(vec2);