Skip to content

Vector4

The Vector4 class represents a four-dimensional vector with components x, y, z, and w, providing methods for common vector operations.

Usage

To use this class, add the following require at the top of your script:

local Vector4 = require 'engine/math/vector4'

Reference

Operators

+

+(Vector4, Vector4): Vector4

Component-wise addition

-

-(): Vector4

Unary minus (negation)

*

*(Vector4, number): Vector4

Multiply vector by scalar

/

/(Vector4, number): Vector4

Divide vector by scalar

==

==(Vector4, Vector4): boolean

Approximate equality (delta \< 1e-10)

tostring

tostring(): string

String representation "[x,y,z,w]"

new

static new(x, y, z, w)

Create new vector

Parameters

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

Returns

  • Vector4: vector

set

set(x, y, z, w)

Set components of the vector

Parameters

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

get

get()

Return the 4 components as array

Returns

  • array: 4 components

toData

toData()

Get values of vector

Returns

  • table: values

lerp

static lerp(from, to, t)

Linear interpolation between 2 vectors

Parameters

  • from (Vector4): first vector
  • to (Vector4): second vector
  • t (number): position between those vectors from 0 to 1

Returns

  • Vector4: vector

moveTowards

static moveTowards(current, target, maxDistanceDelta)

Move one vector in the direction of the target vector

Parameters

  • current (Vector4): current position
  • target (Vector4): target position
  • maxDistanceDelta (number): maximum distance to move towards target

Returns

  • Vector4: a new vector

scaled

scaled(scale)

Scale vector by another vector

Parameters

  • scale (Vector4): scale vector

Returns

  • Vector4: scaled vector

scale

scale(scale)

Scale this vector by another vector

Parameters

  • scale (Vector4): scale vector

Returns

  • Vector4: itself

normalized

normalized()

Return normalized version of this vector

Returns

  • Vector4: vector

normalize

normalize()

Normalize this vector

Returns

  • Vector4: itself

div

div(d)

Devide this vector by a number

Parameters

  • d (number): scalar value

Returns

  • Vector4: itself

mul

mul(d)

Multiply this vector with a number

Parameters

  • d (number): scalar value

Returns

  • Vector4: itself

add

add(b)

Add another vector to this vector

Parameters

  • b (Vector4): other vector

Returns

  • Vector4: itself

sub

sub(b)

Subtract another vector from this one

Parameters

  • b (Vector4): other vector

Returns

  • Vector4: itself

dot

static dot(a, b)

Calculates dot product from 2 vectors

Parameters

  • a (Vector4): first vector
  • b (Vector4): second vector

Returns

  • number: dot product

project

static project(a, b)

Project one vector onto another

Parameters

  • a (Vector4): first vector
  • b (Vector4): second vector

Returns

  • Vector4: vector

distance

static distance(a, b)

Calculate distance between 2 vectors

Parameters

  • a (Vector4): first vector
  • b (Vector4): second vector

Returns

  • number

magnitude

magnitude()

Calculate magnitude of the vector

Returns

  • number

sqrMagnitude

sqrMagnitude()

Calculate squared magnitude of the vector

Returns

  • number: magnitude

min

static min(lhs, rhs)

Return the minimum of all components of two vectors into a new vector

Parameters

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

Returns

  • Vector4: vector

max

static max(lhs, rhs)

Return the maximum of all components of two vectors into a new vector

Parameters

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

Returns

  • Vector4: vector