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:

local Vector3 = require 'engine/math/vector3'

Reference

class Vector3
module:
static fromData(data)

Create a vector object from data

Parameters:

data (table) – the data table

Returns:

the new Vector3 object

Return type:

Vector3

static new(x, y, z)

Create new vector

Parameters:
  • x (number) – x component

  • y (number) – y component

  • z (number) – z component

Returns:

the new vector

Return type:

Vector3

set(x, y, z)

Set values of vector

Parameters:
  • x (number) – x component

  • y (number) – y component

  • z (number) – z component

get()

Get values of vector

Returns:

the x,y,z values

Return type:

number or number or number

toData()

Get values of vector as table

Returns:

the values

Return type:

table

clone()

Clone the vector

Returns:

the clone

Return type:

Vector3

static distance(va, vb)

Calculate distance between 2 vectors

Parameters:
Returns:

distance between vectors

Return type:

number

static dot(lhs, rhs)

Calculate dot product of 2 vectors

Parameters:
Returns:

the dot product

Return type:

number

static lerp(from, to, t)

Linear interpolation between 2 vectors

Parameters:
  • from (Vector3) – first vector

  • to (Vector3) – second vector

  • t (number) – value between 0 and 1 to determine which position between those vectors should be calculated

Returns:

interpolated vector

Return type:

Vector3

magnitude()

Calculate magnitude of the vector

Returns:

magnitude value

Return type:

number

sqrMagnitude()

Calculate squared magnitude of the vector (faster than magnitude)

Returns:

squared magnitude value

Return type:

number

static max(lhs, rhs)

Return the maximum values from both vectors for each component

Parameters:
Returns:

new Vector with the maximum values of each component

Return type:

Vector3

static min(lhs, rhs)

Return the minimum values from both vectors for each component

Parameters:
Returns:

new Vector with the minimum values of each component

Return type:

Vector3

normalized()

Return the normalized version of the vector

Returns:

normalized vector

Return type:

Vector3

normalize()

Normalize this vector

Returns:

returns itself

Return type:

Vector3

clampMagnitude(maxLength)

Clamp the magnitude of the vector

Parameters:

maxLength (number) – maximum length

Returns:

returns itself

Return type:

Vector3

static project(a, b)

Project vector onto another vector

Parameters:
  • a (Vector3) – vector to project

  • b (Vector3) – vector to project onto

Returns:

projected vector

Return type:

Vector3

projectOnPlane(planeNormal)

Project vector onto a plane defined by normal

Parameters:

planeNormal (Vector3) – normal of the plane

Returns:

projected vector

Return type:

Vector3

reflect(inNormal)

Reflect vector off a plane defined by normal

Parameters:

inNormal (Vector3) – normal of the plane

Returns:

reflected vector

Return type:

Vector3

static rotateTowards(current, target, maxRadiansDelta, maxMagnitudeDelta)

Rotate vector towards target vector

Parameters:
  • current (Vector3) – current vector

  • target (Vector3) – target vector

  • maxRadiansDelta (number) – maximum angle to rotate

  • maxMagnitudeDelta (number) – maximum change in magnitude

Returns:

rotated vector

Return type:

Vector3

static moveTowards(current, target, maxDistanceDelta)

Move vector towards target vector

Parameters:
  • current (Vector3) – current vector

  • target (Vector3) – target vector

  • maxDistanceDelta (number) – maximum distance to move

Returns:

moved vector

Return type:

Vector3

static smoothDamp(current, target, currentVelocity, smoothTime, deltaTime)

Smoothly interpolates a vector toward a target with velocity damping

Parameters:
  • current (Vector3) – current position

  • target (Vector3) – target position

  • currentVelocity (Vector3) – reference to velocity vector

  • smoothTime (number) – time to reach target

  • deltaTime (number) – time since last frame

Returns:

smoothed position

Return type:

Vector3

Returns:

updated velocity

Return type:

Vector3

add(vb)

Adds another vector to this vector

Parameters:

vb (Vector3) – other vector

Returns:

returns itself

Return type:

Vector3

sub(vb)

Subtracts another vector from this vector

Parameters:

vb (Vector3) – other vector

Returns:

returns itself

Return type:

Vector3

mul(d)

Multiplies this vector by a scalar

Parameters:

d (number) – scalar value

Returns:

returns itself

Return type:

Vector3

div(d)

Divides this vector by a scalar

Parameters:

d (number) – scalar value

Returns:

returns itself

Return type:

Vector3

mulVec(vb)

Multiplies this vector by another vector (component-wise)

Parameters:

vb (Vector3) – other vector

Returns:

returns itself

Return type:

Vector3

mulQuat(quat)

Multiply quaternion with this vector

Parameters:

quat (Quaternion) – input quaternion

Returns:

returns itself

Return type:

Vector3

static angle(from, to)

Calculate angle between two vectors

Parameters:
Returns:

angle in degrees

Return type:

number

static angleAroundAxis(from, to, axis)

Calculate signed angle between two vectors around an axis

Parameters:
Returns:

signed angle in degrees

Return type:

number

static createFrustumRays(viewDirection, fov, resX, resY)

Creates an array of Vector3 objects which are the direction vectors of the frustum

Parameters:
  • viewDirection (Vector3) – a view vector

  • fov (number) – Field of view value in degrees

  • resX (number) – resolution width. Needs to be at least 2

  • resY (number) – resolution height Needs to be at least 2

Returns:

Array of Vector3 objects which have the count width * height

Return type:

table

static cross(lhs, rhs)

Cross product of two vectors

Parameters:
Returns:

cross product vector

Return type:

Vector3

static slerp(from, to, t)

Spherical interpolation between two vectors

Parameters:
  • from (Vector3) – first vector

  • to (Vector3) – second vector

  • t (number) – interpolation value (0-1)

Returns:

interpolated vector

Return type:

Vector3

static orthoNormalize(va, vb, vc)

Orthonormalize three vectors

Parameters:
Returns:

orthonormalized vectors

Return type:

Vector3 or Vector3 or Vector3

static orthoNormalVector(vec)

Find a vector orthogonal to the input vector

Parameters:

vec (Vector3) – input vector

Returns:

orthogonal vector

Return type:

Vector3