Vector3
The Vector3 class represents a three-dimensional vector with x, y, and z components, providing essential functionalities for vector operations.
Usage
To use this class, add the following require at the top of your script:
Reference
Operators
+
Component-wise addition
-
Unary minus (negation)
*
Rotate vector by quaternion
/
Divide vector by scalar
==
Approximate equality (delta \< 1e-10)
tostring
String representation "[x,y,z]"
fromData
Create a vector object from data
Parameters
data(table): the data table
Returns
Vector3: new Vector3 object
new
Create new vector
Parameters
x(number): x componenty(number): y componentz(number): z component
Returns
Vector3: new vector
set
Set values of vector
Parameters
x(number): x componenty(number): y componentz(number): z component
get
Get values of vector
Returns
number: The X componentnumber: The Y componentnumber: The Z component
toData
Get values of vector as table
Returns
table: values
clone
Clone the vector
Returns
Vector3: clone
distance
Calculate distance between 2 vectors
Parameters
va(Vector3): first vectorvb(Vector3): second vector
Returns
number: between vectors
dot
Calculate dot product of 2 vectors
Parameters
lhs(Vector3): first vectorrhs(Vector3): second vector
Returns
number: dot product
lerp
Linear interpolation between 2 vectors
Parameters
from(Vector3): first vectorto(Vector3): second vectort(number): value between 0 and 1 to determine which position between those vectors should be calculated
Returns
Vector3: vector
magnitude
Calculate magnitude of the vector
Returns
number: value
sqrMagnitude
Calculate squared magnitude of the vector (faster than magnitude)
Returns
number: magnitude value
max
Return the maximum values from both vectors for each component
Parameters
lhs(Vector3): first vectorrhs(Vector3): second vector
Returns
Vector3: Vector with the maximum values of each component
min
Return the minimum values from both vectors for each component
Parameters
lhs(Vector3): first vectorrhs(Vector3): second vector
Returns
Vector3: Vector with the minimum values of each component
normalized
Return the normalized version of the vector
Returns
Vector3: vector
normalize
Normalize this vector
Returns
Vector3: itself
clampMagnitude
Clamp the magnitude of the vector
Parameters
maxLength(number): maximum length
Returns
Vector3: itself
project
Project vector onto another vector
Parameters
a(Vector3): vector to projectb(Vector3): vector to project onto
Returns
Vector3: vector
projectOnPlane
Project vector onto a plane defined by normal
Parameters
planeNormal(Vector3): normal of the plane
Returns
Vector3: vector
reflect
Reflect vector off a plane defined by normal
Parameters
inNormal(Vector3): normal of the plane
Returns
Vector3: vector
rotateTowards
Rotate vector towards target vector
Parameters
current(Vector3): current vectortarget(Vector3): target vectormaxRadiansDelta(number): maximum angle to rotatemaxMagnitudeDelta(number): maximum change in magnitude
Returns
Vector3: vector
moveTowards
Move vector towards target vector
Parameters
current(Vector3): current vectortarget(Vector3): target vectormaxDistanceDelta(number): maximum distance to move
Returns
Vector3: vector
smoothDamp
Smoothly interpolates a vector toward a target with velocity damping
Parameters
current(Vector3): current positiontarget(Vector3): target positioncurrentVelocity(Vector3): reference to velocity vectorsmoothTime(number): time to reach targetdeltaTime(number): time since last frame
Returns
Vector3: positionVector3: velocity
add
Adds another vector to this vector
Parameters
vb(Vector3): other vector
Returns
Vector3: itself
sub
Subtracts another vector from this vector
Parameters
vb(Vector3): other vector
Returns
Vector3: itself
mul
Multiplies this vector by a scalar
Parameters
d(number): scalar value
Returns
Vector3: itself
div
Divides this vector by a scalar
Parameters
d(number): scalar value
Returns
Vector3: itself
mulVec
Multiplies this vector by another vector (component-wise)
Parameters
vb(Vector3): other vector
Returns
Vector3: itself
mulQuat
Multiply quaternion with this vector
Parameters
quat(Quaternion): input quaternion
Returns
Vector3: itself
angle
Calculate angle between two vectors
Parameters
from(Vector3): first vectorto(Vector3): second vector
Returns
number: in degrees
angleAroundAxis
Calculate signed angle between two vectors around an axis
Parameters
from(Vector3): first vectorto(Vector3): second vectoraxis(Vector3): axis vector
Returns
number: angle in degrees
createFrustumRays
Creates an array of Vector3 objects which are the direction vectors of the frustum
Parameters
viewDirection(Vector3): a view vectorfov(number): Field of view value in degreesresX(number): resolution width. Needs to be at least 2resY(number): resolution height Needs to be at least 2
Returns
table: of Vector3 objects which have the count resX * resY
cross
Cross product of two vectors
Parameters
lhs(Vector3): first vectorrhs(Vector3): second vector
Returns
Vector3: product vector
xy
xz
yz
zy
zx
slerp
Spherical interpolation between two vectors
Parameters
from(Vector3): first vectorto(Vector3): second vectort(number): interpolation value (0-1)
Returns
Vector3: vector
orthoNormalize
Orthonormalize three vectors
Parameters
va(Vector3): first vectorvb(Vector3): second vectorvc(Vector3): third vector
Returns
Vector3,Vector3,Vector3: vectors
orthoNormalVector
Find a vector orthogonal to the input vector
Parameters
vec(Vector3): input vector
Returns
Vector3: vector