Skip to content

Class "Vector"⚓︎

Info

This class can be accessed by using its constructor or multiple functions:

Example Code
1
local myVector = Vector(1, -1)

Constructors⚓︎

Vector ()⚓︎

Vector Vector ( float , float )⚓︎


Constants⚓︎

Vector.Zero⚓︎

Equivalent to Vector(0, 0).


Vector.One⚓︎

Equivalent to Vector(1, 1).


Operators⚓︎

__add ()⚓︎

Vector __add ( Vector Right )⚓︎

Defines the Addition of two Vector objects using the + operator.

Example Code
1
2
3
local vec1 = Vector(2,1)
local vec2 = Vector(5,3)
local result = vec1+vec2 -- result equals Vector(7,4) now

__div ()⚓︎

Vector __div ( float Modifier )⚓︎

Defines the Division of two Vector objects using the / operator.

Example Code
1
2
3
local vec1 = Vector(6,4)
local vec2 = Vector(2,4)
local result = vec1/vec2 -- result equals Vector(3,1) now

__mul ()⚓︎

Vector __mul ( float Modifier )⚓︎

Defines the Multiplication of two Vector objects using the * operator.

Example Code
1
2
3
local vec1 = Vector(2,3)
local vec2 = Vector(5,3)
local result = vec1*vec2 -- result equals Vector(10,9) now

__sub ()⚓︎

Vector __sub ( Vector Right )⚓︎

Defines the Subtraction of two Vector objects using the - operator.

Example Code
1
2
3
local vec1 = Vector(2,1)
local vec2 = Vector(5,3)
local result = vec1-vec2 -- result equals Vector(-3,-2) now

__unm ()⚓︎

Vector __unm ( Vector Right )⚓︎

Defines the inversion of a Vector object using the - operator.

Example Code
1
2
local vec1 = Vector(2,1)
local result = -vec1 -- result equals Vector(-2,-1) now

Functions⚓︎

Clamp ()⚓︎

void Clamp ( float MinX, float MinY, float MaxX, float MaxY )⚓︎

Clamps the vector based on left, top, right, bottom boundings. Doesn't keep direction


Clamped ()⚓︎

Vector Clamped ( float MinX, float MinY, float MaxX, float MaxY )⚓︎

Returns a clamped version of the vector.


Cross ()⚓︎

float Cross ( Vector second )⚓︎

Cross product this is the 2x2 matrix determinant or the resulting z value for their 3D versions with z=0


Distance ()⚓︎

float Distance ( Vector first, Vector second )⚓︎

Returns distance between two vectors

Example Code
1
local sqtDist = Vector(2,0):Distance(Vector(4,0)) --sqtDist = 2

Distance·Squared ()⚓︎

float DistanceSquared ( Vector first, Vector second )⚓︎

Returns squared distance between two vectors

Example Code
1
local sqtDist = Vector(2,0):DistanceSquared(Vector(4,0)) --sqtDist = 4

Dot ()⚓︎

float Dot ( Vector second )⚓︎

Dot product


From·Angle ()⚓︎

static Vector FromAngle ( float AngleDegrees )⚓︎

Build a Vector from an angle, returns a normalized vector. Angle 0 will result in (1, 0). Angle 90 will result in (0, 1).

Example Code

This code returns a vector that has a 45 degree angle

1
local vec = Vector.FromAngle(45) --vec is now Vector(0.70711,0.70711)


Get·Angle·Degrees ()⚓︎

float GetAngleDegrees ( )⚓︎

Returns the angle the vector is facing. The vector (1, 0) will be at 0 degrees. The vector (0, 1) will be at 90 degrees.

Practically, this means:

  • Right: 0
  • Up: -90
  • Left: 180
  • Down: 90
Example Code

This code returns the angle between two positions.

1
2
3
4
local v1 = Vector(1,0) -- has angle 0.0
local v2 = Vector(0,1) -- has angle 90.0
local v3 = v2-v1 -- subtraction of 2 points is a vector connecting the two points
print(v3:GetAngleDegrees()) -- prints 45.0


Length ()⚓︎

float Length ( )⚓︎

Returns the length of the vector


Length·Squared ()⚓︎

float LengthSquared ( )⚓︎

Returns the length squared of the vector


Lerp ()⚓︎

Vector Lerp ( Vector first, Vector second, float t )⚓︎

Linear interpolation between two vectors. For t = 0 it returns the first Vector, for t = 1 it returns the second.

Alternate Function example

This function does the same as Lerp, but will not alter the input vectors.

1
2
3
function Lerp(vec1, vec2, percent)
    return vec1 * (1 - percent) + vec2 * percent
end

Example Code

This code will make v1 the vector 50% in between v1 and v2

1
2
3
local v1 = Vector(0,0)
local v2 = Vector(1,1)
v1:Lerp(v2,0.5) -- v1 equals  Vector(0.5,0.5)  now


Normalize ()⚓︎

void Normalize ( )⚓︎

Normalizes this vector, effectively making its length equal 1.


Normalized ()⚓︎

Vector Normalized ( )⚓︎

Returns a normalized version of this vector, effectively making its length equal 1.


Resize ()⚓︎

void Resize ( float NewLength )⚓︎

Resizes the vector length.


Resized ()⚓︎

Vector Resized ( float NewLength )⚓︎

Returns a resized version of the vector.


Rotated ()⚓︎

Vector Rotated ( float AngleDegrees )⚓︎

Returns a rotated version of the vector by AngleDegrees


__tostring ()⚓︎

void __tostring ( )⚓︎

Vector objects can be cast to a string object, which returns information about this object in the following format:

1
Vector(X,Y)

Variables⚓︎

X⚓︎

float X⚓︎

Components of vector.


Y⚓︎

float Y⚓︎



Last update: April 26, 2024