Skip to content
T61qn@*rGR edited this page Jun 23, 2021 · 2 revisions

All functions except set create new vectors so that the original vector is not modified. All the functions (except set which return the modified vector) return the new vector so that operations can be chained easily.

Constructors

vector3(x, y, z)

Creates a new vector with components x, y and z. Components defaults to zero.

vector3.fromSpherical(r, theta, phi)

Creates a new vector from spherical coordinates. theta is the polar angle (from the upwards y axis) and phi the azimut (counting clockwise around y, starting from x).

vector3.random(length)

Creates a vector with a random direction and magnitude of length (defaults to 1).

Meta functions

print

Allows to print a vector.

concatenation

Allows to create strings from vector concatenation.

negative

Returns the opposite vector.

equality

Return true if two vectors have their components equals

addition

Return the sum of two vectors component wise. If one argument is a number then this number is added to each component of the vector.

subtraction

Return the difference of two vectors component wise. If one argument is a number then this number is subtracted to each component of the vector.

multiplication

Return the product of two vectors component wise. If one argument is a number then each component of the vector is multiplied by this number.

division

Return the division of two vectors component wise. If one argument is a number then each component of the vector is divided by this number.

Functions

set(x, y, z)

Sets the x,y and z components of the vector and return it. If a parameter is nil then the corresponding component is unchanged.

clone()

Return a new vector which is a copy of the initial vector.

length()

Returns the magnitude of the vector.

norm()

Return the corresponding normalized vector (with magnitude one).

scale(mag)

Return a new vector which is scaled to magnitude mag

limit(max)

Returns a new vector which is scaled to magnitude max if its magnitude if greater than max.

floor()

Return a new vector with the components floored.

round()

Return a new vector with the components rounded to the closest integer.

offset(a, b, c)

Return a new vector with components x, y, z offset by a, b and c. If a parameter is nil the corresponding component is unchanged.

apply(f)

Return a new vector with the function f applied to its components.

dist(b)

Returns the distance between the current vector and b (as if they were representing points).

dot(b)

Returns the dot product of the current vector and b.

cross(b)

Returns a vector which is the cross product of the current vector and b.

rotate_around(axis, angle)

Returns a new vector which is the current vector rotated around axis with angle.

unpack()

Returns the unpacked components of the current vector.