Spatial Vectors¶
A spatial vector is similar to a position, but instead using absolute world coordinates, it uses relative coordinates, relative to no particular point.
Internally, it is implemented as a table with the 3 fields
x, y and z. Example: {x = 0, y = 1, z = 0}.
For the following functions, v, v1, v2 are vectors,
p1, p2 are positions,
s is a scalar (a number):
vector.new(a[, b, c]):- Returns a vector.
- A copy of
aifais a vector. {x = a, y = b, z = c}, if all ofa,b,care defined numbers.
vector.zero():- Returns a new vector
(0, 0, 0).
- Returns a new vector
vector.copy(v):- Returns a copy of the vector
v.
- Returns a copy of the vector
vector.direction(p1, p2):- Returns a vector of length 1 with direction
p1top2. - If
p1andp2are identical, returns{x = 0, y = 0, z = 0}.
- Returns a vector of length 1 with direction
vector.distance(p1, p2):- Returns zero or a positive number, the distance between
p1andp2.
- Returns zero or a positive number, the distance between
vector.length(v):- Returns zero or a positive number, the length of vector
v.
- Returns zero or a positive number, the length of vector
vector.normalize(v):- Returns a vector of length 1 with direction of vector
v. - If
vhas zero length, returns{x = 0, y = 0, z = 0}.
- Returns a vector of length 1 with direction of vector
vector.floor(v):- Returns a vector, each dimension rounded down.
vector.round(v):- Returns a vector, each dimension rounded to nearest integer.
- At a multiple of 0.5, rounds away from zero.
vector.apply(v, func):- Returns a vector where the function
funchas been applied to each component.
- Returns a vector where the function
vector.equals(v1, v2):- Returns a boolean,
trueif the vectors are identical.
- Returns a boolean,
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.dot(v1, v2):- Returns the dot product of
v1andv2.
- Returns the dot product of
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 = x, y = y, z = z}.
- Returns the sum of the vectors
For the following functions x can be either a vector or a number:
vector.add(v, x):- Returns a vector.
- If
xis a vector: Returns the sum ofvandx. - If
xis a number: Addsxto each component ofv.
vector.subtract(v, x):- Returns a vector.
- If
xis a vector: Returns the difference ofvsubtracted byx. - If
xis a number: Subtractsxfrom each component ofv.
vector.multiply(v, s):- Returns a scaled vector.
- Deprecated: If
sis a vector: Returns the Schur product.
vector.divide(v, s):- Returns a scaled vector.
- Deprecated: If
sis a vector: Returns the Schur quotient.
For the following functions a is an angle in radians and r is a rotation
vector ({x =
vector.rotate(v, r):- Applies the rotation
rtovand returns the result. vector.rotate({x = 0, y = 0, z = 1}, r)andvector.rotate({x = 0, y = 1, z = 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