Skip to content

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

Operators

+

+(Vector3, Vector3): Vector3

Component-wise addition

-

-(): Vector3

Unary minus (negation)

*

*(Vector3, Quaternion): Vector3

Rotate vector by quaternion

/

/(Vector3, number): Vector3

Divide vector by scalar

==

==(Vector3, Vector3): boolean

Approximate equality (delta \< 1e-10)

tostring

tostring(): string

String representation "[x,y,z]"

fromData

static fromData(data)

Create a vector object from data

Parameters

  • data (table): the data table

Returns

  • Vector3: new Vector3 object

new

static new(x, y, z)

Create new vector

Parameters

  • x (number): x component
  • y (number): y component
  • z (number): z component

Returns

  • Vector3: new vector

set

set(x, y, z)

Set values of vector

Parameters

  • x (number): x component
  • y (number): y component
  • z (number): z component

get

get()

Get values of vector

Returns

  • number: The X component
  • number: The Y component
  • number: The Z component

toData

toData()

Get values of vector as table

Returns

  • table: values

clone

clone()

Clone the vector

Returns

  • Vector3: clone

distance

static distance(va, vb)

Calculate distance between 2 vectors

Parameters

  • va (Vector3): first vector
  • vb (Vector3): second vector

Returns

  • number: between vectors

dot

static dot(lhs, rhs)

Calculate dot product of 2 vectors

Parameters

  • lhs (Vector3): first vector
  • rhs (Vector3): second vector

Returns

  • number: dot product

lerp

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

  • Vector3: vector

magnitude

magnitude()

Calculate magnitude of the vector

Returns

  • number: value

sqrMagnitude

sqrMagnitude()

Calculate squared magnitude of the vector (faster than magnitude)

Returns

  • number: magnitude value

max

static max(lhs, rhs)

Return the maximum values from both vectors for each component

Parameters

  • lhs (Vector3): first vector
  • rhs (Vector3): second vector

Returns

  • Vector3: Vector with the maximum values of each component

min

static min(lhs, rhs)

Return the minimum values from both vectors for each component

Parameters

  • lhs (Vector3): first vector
  • rhs (Vector3): second vector

Returns

  • Vector3: Vector with the minimum values of each component

normalized

normalized()

Return the normalized version of the vector

Returns

  • Vector3: vector

normalize

normalize()

Normalize this vector

Returns

  • Vector3: itself

clampMagnitude

clampMagnitude(maxLength)

Clamp the magnitude of the vector

Parameters

  • maxLength (number): maximum length

Returns

  • Vector3: itself

project

static project(a, b)

Project vector onto another vector

Parameters

  • a (Vector3): vector to project
  • b (Vector3): vector to project onto

Returns

  • Vector3: vector

projectOnPlane

projectOnPlane(planeNormal)

Project vector onto a plane defined by normal

Parameters

  • planeNormal (Vector3): normal of the plane

Returns

  • Vector3: vector

reflect

reflect(inNormal)

Reflect vector off a plane defined by normal

Parameters

  • inNormal (Vector3): normal of the plane

Returns

  • Vector3: vector

rotateTowards

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

  • Vector3: vector

moveTowards

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

  • Vector3: vector

smoothDamp

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

  • Vector3: position
  • Vector3: velocity

add

add(vb)

Adds another vector to this vector

Parameters

  • vb (Vector3): other vector

Returns

  • Vector3: itself

sub

sub(vb)

Subtracts another vector from this vector

Parameters

  • vb (Vector3): other vector

Returns

  • Vector3: itself

mul

mul(d)

Multiplies this vector by a scalar

Parameters

  • d (number): scalar value

Returns

  • Vector3: itself

div

div(d)

Divides this vector by a scalar

Parameters

  • d (number): scalar value

Returns

  • Vector3: itself

mulVec

mulVec(vb)

Multiplies this vector by another vector (component-wise)

Parameters

  • vb (Vector3): other vector

Returns

  • Vector3: itself

mulQuat

mulQuat(quat)

Multiply quaternion with this vector

Parameters

  • quat (Quaternion): input quaternion

Returns

  • Vector3: itself

angle

static angle(from, to)

Calculate angle between two vectors

Parameters

  • from (Vector3): first vector
  • to (Vector3): second vector

Returns

  • number: in degrees

angleAroundAxis

static angleAroundAxis(from, to, axis)

Calculate signed angle between two vectors around an axis

Parameters

  • from (Vector3): first vector
  • to (Vector3): second vector
  • axis (Vector3): axis vector

Returns

  • number: angle in degrees

createFrustumRays

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

  • table: of Vector3 objects which have the count resX * resY

cross

static cross(lhs, rhs)

Cross product of two vectors

Parameters

  • lhs (Vector3): first vector
  • rhs (Vector3): second vector

Returns

  • Vector3: product vector

xy

xy()

xz

xz()

yz

yz()

zy

zy()

zx

zx()

slerp

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

  • Vector3: vector

orthoNormalize

static orthoNormalize(va, vb, vc)

Orthonormalize three vectors

Parameters

  • va (Vector3): first vector
  • vb (Vector3): second vector
  • vc (Vector3): third vector

Returns

  • Vector3,Vector3,Vector3: vectors

orthoNormalVector

static orthoNormalVector(vec)

Find a vector orthogonal to the input vector

Parameters

  • vec (Vector3): input vector

Returns

  • Vector3: vector