This mod adds a 3d vector class to Minetest.
Some functions exist in two versions. One version returns a new vector, the other version modifys a vector.
To create a new vector you have to use the method vec3d()
local v = vec3d() -- creates the vector {x = 0, y = 0, z = 0}
local v = vec3d(5,3,1) -- creates the vector {x = 5, y = 3, z = 1}
local x = v.x
local y = v.y
local z = v.z
If a vector is printed to the console with the print()
command the output is formatted in the order x, y, z
local v1 = vec3d(5, 3, 1)
print(v1)
The output is: (5, 3, 1)
Creates a copy of a vector
local v1 = vec3d(10, 20, 2)
local v2 = v1:clone()
v2.x = 0
print(v1, v2)
Prints the output (10, 20, 2) (0, 20, 2)
Returns the single componets of the vector
local v1 = vec3d(10, 20, 2)
local x, y, z = v1:unpack()
- returns new unit vector pointing up (+y)
- returns new unit vector pointing down (-y)
- returns new unit vector pointing right (+x)
- returns new unit vector pointing left (-x)
- returns new unit vector pointing forward (+z)
- returns new unit vector pointing backward (-z)
This class has two ways for basic arithmetics.
The first uses regular math operators +, -, *, /. This way creates a new vector as result. Both operands can be a vector or a number.
local v1 = vec3d(5, 3, 1)
local v2 = vec3d(2, 4, 6)
local v3 = v1 + v2
print(v3)
This prints (7, 7, 7)
local v1 = vec3d(5, 3, 1)
local v2 = v1 * 5
print(v2)
This prints (25, 15, 5)
The second way uses a class methode to modify a vector. The class methodes don't create a new vector and is slightly faster.
x
can be a vec3d or a number
vec3d:add(x)
vec3d:sub(x)
vec3d:mul(x)
vec3d:div(x)
local v1 = vec3d(5, 3, 1)
local v2 = vec3d(2, 4, 6)
v1:add(v2)
print(v1)
This prints (7, 7, 7)
Vectors can be compared with ==
local v1 = vec3d(10, 10, 10)
local v2 = vec3d(20, 20, 20)
print(v1 == v2)
Prints false
to the console
Return the distance between two vectors
local v1 = vec3d(10, 20, 2)
local v2 = vec3d(20, 30, 5)
print(vec3d.distance(v1, v2))
print(v1:distance_to(v2))
Both functions return 14.456832294801
Returns the real lenght of a vector
local v1 = vec3d(10, 20, 2)
print(v1:len())
Returns 22.449944320644
Returns the squared lenght of a vector
local v1 = vec3d(10, 20, 2)
print(v1:lenSq())
Returns 504
Normalizes a vector
local v1 = vec3d(10, 20, 2)
v1:normalize()
print(v1)
---------------------------
local v1 = vec3d(10, 20, 2)
local v2 = v1:normalized()
print(v2)
Returns (0.4454354, 0.8908708, 0.08908708)
Rounds a vector
local v1 = vec3d(10.3, 20.7, 2.6)
v1:round()
print(v1)
---------------------------------
local v1 = vec3d(10.3, 20.7, 2.6)
local v2 = v1:rounded()
print(v2)
Returns (10, 21, 3)
Rounds the vector down
local v1 = vec3d(10.3, 20.7, 2.6)
v1:floor()
print(v1)
--------------------------------
local v1 = vec3d(10.3, 20.7, 2.6)
local v2 = v1:floored()
print(v2)
Returns (10, 20, 2)