Skip to content

Extended: Vector

dounai2333 edited this page Jan 1, 2025 · 1 revision

Introduction

How to get this userdata:

Vector()

Math

The Vector userdata now support calculating:

local vector1 = Vector(1, 2, 3)
local vector2 = Vector(4, 5, 6)

print(vector1 + vector2) -- Output: "5.000000 7.000000 9.000000"
print(vector1 / vector2) -- Output: "0.250000 0.400000 0.500000"
print(vector1 * vector2) -- Output: "4.000000 10.000000 18.000000"
print(vector1 - vector2) -- Output: "-3.000000 -3.000000 -3.000000"
print(-vector1) -- Output: "-1.000000 -2.000000 -3.000000"
print(vector1 == vector2) -- Output: "false"

tostring

tostring(vector)

Format vector to a proper string "x y z", with maximum 6 decimal places.

Add

vector:Add(vector)

Adds the values of the argument vector to the original vector.

This function is the same as vector1 + vector2 without creating a new vector object, skipping object construction and garbage collection.

vector: vector

Angle

vector:Angle()

Returns an angle representing the normal of the vector.

Return: angle

Cross

vector:Cross(vector)

Calculates the cross product of this vector and the passed one.

Return: vector

vector: vector

Distance

vector:Distance(vector)

Returns the Euclidean distance between the vector and the other vector.

Return: number

vector: vector

DistToSqr

vector:DistToSqr(vector)

Returns the squared distance of 2 vectors.

This is quicker to call than vector:Distance() as DistToSqr does not need to calculate the square root, which is an expensive process.

Return: number

vector: vector

Div

vector:Div(divisor)

Divides all values of the original vector by a scalar.

This functions the same as vector1 / num without creating a new vector object, skipping object construction and garbage collection.

divisor: number

Dot

vector:Dot(vector)

Returns the dot product of this vector and the passed one.

Return: number

vector: vector

IsZero

vector:IsZero()

Checks whenever all fields of the vector are 0.

Return: true or false

Length

vector:Length()

Returns the Euclidean length of the vector.

Return: number

Length2D

vector:Length2D()

Returns the length of the vector in two dimensions, without the Z axis.

Return: number

Length2DSqr

vector:Length2DSqr()

Returns the squared length of the vectors x and y value.

Return: number

LengthSqr

vector:LengthSqr()

Returns the squared length of the vector.

Return: number

Mul

vector:Mul(multiplier)

Multiplies a scalar to all the values of the orignal vector.

This functions the same as vector1 * num without creating a new vector object, skipping object construction and garbage collection.

multiplier: number

Sub

vector:Sub(vector)

Subtracts the values of the argument vector to the orignal vector.

This functions the same as vector1 - vector2 without creating a new vector object, skipping object construction and garbage collection.

vector: vector

Clone this wiki locally