An instance of the Vec3 class represents a ‘3-dimensional Vector’.
Mostly a 3-dimensional vector is used to denote a position in the 3-dimensional world space or a constant velocity of an object inside that space.
However, a vector can be used for many other ‘things’ that can be described by 3 independent numerical values.
To create a vector you can call the Vec3 function:
myvec = Vec3( 1, 2, 3)
This creates a vector called ‘myvec’ with the component values x=1, y=2, z=3.
Here is an overview of the Vec3 properties:
Property | Type | read / write |
---|---|---|
x | number | r/w |
y | number | r/w |
z | number | r/w |
Here is an overview of the Vec3 functions:
Function | Parameters | Results |
---|---|---|
add | other | Vec3 |
chunk | number, number | |
dotProduct | other | Vec3 |
invert | Vec3 | |
magnitude | Vec3 | |
normalize | Vec3 | |
scale | factor | Vec3 |
sqrMagnitude | Vec3 | |
substract | other | Vec3 |
tostring | string |
Below you find short descriptions about each of the properties and some examples about how to use them in your spells.
The X-component of the vector
The Y-component of the vector
The Z-component of the vector
Below you find short descriptions about each of the functions and some examples about how to use them in your spells.
Returns a new vector that is the result of adding the other vector to the current vector.
Adding two vectors.
local p1 = Vec3( 1, 0, 8)
local p2 = Vec3( -1, 2, 3)
local p3 = p1:add( p2)
Since add()
is also used as __add
of Vec3’s metatable, you can
shorten the above example by using the +
operator.
local p1 = Vec3( 1, 0, 8)
local p2 = Vec3( -1, 2, 3)
local p3 = p1 + p2
The ‘chunk’ function interprets this vector as world coordinate, converts them into chunk coordinates, and returns them as a multi-value result.
Converting the spell’s world coordinates into chunk coordinates.
local chunkX, chunkZ = spell.pos:chunk()
Returns the ‘dot’ product of the current vector and the other vector.
Creating the dot-product of two vectors.
local p1 = Vec3( 5, 4, 3)
local p2 = Vec3( 2, 4, 6)
local p3 = p1:dotProduct( p2)
Since dotProduct()
is also used in __mul
of Vec3’s metatable, you can
shorten the above example by using the *
operator.
local p1 = Vec3( 5, 4, 3)
local p2 = Vec3( 2, 4, 6)
local p3 = p1 * p2
Returns an inverted version of the current vector.
Inverting a vector.
local p1 = Vec3( 5, 2 , -1)
local p2 = p1:invert()
Since invert()
is also used as __unm
of Vec3’s metatable, you can
shorten the above example.
local p1 = Vec3( 5, 2 , -1)
local p2 = - p1
Returns the length of the current vector.
Getting the length of a vector.
local p = Vec3( 2, 3, 4)
local len = p:magnitude()
Returns a normalized version of the current vector, which means a vector with a magnitude of 1 meter and pointing into the same direction as the original vector.
Normalizing a vector.
local vec = Vec3( 2, 3, 4)
local normalizedVec = vec:normalize()
Returns a copy of current vector, scaled by the given factor.
Scaling a vector by 3.
local p1 = Vec3( 11, 22, 33)
local p2 = p1:scale( 3)
Since scale()
is also used as __mul
of Vec3’s metatable, you can
shorten the above example.
local p1 = Vec3( 11, 22, 33)
local p2 = p1 * 3
Returns the squared length of the current vector.
Getting the ‘squared’ length of a vector.
local p = Vec3( 2, 3, 4)
local slen = p:sqrMagnitude()
Returns a new vector that is the result of substracting the other vector from the current vector.
Substracting two vectors.
local p1 = Vec3( 5, 4, 3)
local p2 = Vec3( 2, 4, 6)
local p3 = p1:substract( p2)
Since substract()
is also used as __sub
of Vec3’s metatable, you can
shorten the above example by using the -
operator.
local p1 = Vec3( 5, 4, 3)
local p2 = Vec3( 2, 4, 6)
local p3 = p1 - p2
Returns a string with the following format ‘(x, y, z)’.
Printing the vector using tostring().
local p = Vec3( 11, 22, 33)
print( p:tostring() )
Since tostring()
is also used as __tostring
of Vec3’s metatable, you can
shorten the above example.
local p = Vec3( 11, 22, 33)
print( p)