Spatial Vectors¶
Luanti stores 3-dimensional spatial vectors in Lua as tables of 3 coordinates,
and has a class to represent them (vector.*), which this chapter is about.
For details on what a spatial vector is, please refer to Wikipedia.
Spatial vectors are used for various things, including, but not limited to:
- any 3D spatial vector (x/y/z-directions)
- Euler angles (pitch/yaw/roll in radians) (Spatial vectors have no real semantic meaning here. Therefore, most vector operations make no sense in this use case.)
The API documentation may refer to spatial vectors, as produced by vector.new,
by any of the following notations:
(x, y, z)(Used rarely, and only if it's clear that it's a vector.)vector.new(x, y, z){x=num, y=num, z=num}(Even here you are still supposed to usevector.new.)
Compatibility notes¶
Vectors used to be defined as tables of the form {x = num, y = num, z = num}.
Since version 5.5.0, vectors additionally have a metatable to enable easier use.
Note: Those old-style vectors can still be found in old mod code. Hence, mod and
engine APIs still need to be able to cope with them in many places.
Manually constructed tables are deprecated and highly discouraged. This interface should be used to ensure seamless compatibility between mods and the Luanti API. This is especially important to callback function parameters and functions overwritten by mods. Also, though not likely, the internal implementation of a vector might change in the future. In your own code, or if you define your own API, you can, of course, still use other representations of vectors.
Vectors provided by API functions will provide an instance of this class if not stated otherwise. Mods should adapt this for convenience reasons.
Special properties of the class¶
For special properties common to all vector types (indexing, method syntax, operators, etc.), see Common to all vector types.
Functions¶
For common functions available to both vector and vector2,
see Common functions.
The following functions are specific to vector (3D vectors).
For the following functions,
v, v1, v2 are vectors,
p1, p2 are position vectors,
s is a scalar (a number),
vectors are written like this: (x, y, z):
vector.new([a[, b, c]]):- Returns a new vector
(a, b, c). - Deprecated:
vector.new()does the same asvector.zero()andvector.new(v)does the same asvector.copy(v)
- Returns a new vector
vector.sort(v1, v2):- Returns in order minp, maxp vectors of the cuboid defined by
v1,v2.
- Returns in order minp, maxp vectors of the cuboid defined by
vector.angle(v1, v2):- Returns the angle between
v1andv2in radians.
- Returns the angle between
vector.cross(v1, v2):- Returns the cross product of
v1andv2.
- Returns the cross product of
vector.offset(v, x, y, z):- Returns the sum of the vectors
vand(x, y, z).
- Returns the sum of the vectors
vector.random_in_area(min, max):- Returns a random integer position in area formed by
minandmax minandmaxare inclusive.- You can use
vector.sortif you have two vectors and don't know which are the minimum and the maximum.
- Returns a random integer position in area formed by
Operators¶
For vector operators (+, -, *, /, ==, unary -), see Common to all vector types.
Rotation-related functions¶
For the following functions a is an angle in radians and r is a rotation
vector ({x = <pitch>, y = <yaw>, z = <roll>}) where pitch, yaw and roll are
angles in radians.
vector.rotate(v, r):- Applies the rotation
rtovand returns the result. - Uses (extrinsic) Z-X-Y rotation order and is right-handed, consistent with
ObjectRef:set_rotation. vector.rotate(vector.new(0, 0, 1), r)andvector.rotate(vector.new(0, 1, 0), r)return vectors pointing forward and up relative to an entity's rotationr.
- Applies the rotation
vector.rotate_around_axis(v1, v2, a):- Returns
v1rotated around axisv2byaradians according to the right hand rule.
- Returns
vector.dir_to_rotation(direction[, up]):- Returns a rotation vector for
directionpointing forward usingupas the up vector. - If
upis omitted, the roll of the returned vector defaults to zero. - Otherwise
directionandupneed to be vectors in a 90 degree angle to each other.
- Returns a rotation vector for
Further helpers¶
There are more helper functions involving vectors, but they are listed elsewhere because they only work on specific sorts of vectors or involve things that are not vectors.
For example:
core.hash_node_position(Only works on node positions.)core.dir_to_wallmounted(Involves wallmounted param2 values.)