Skip to content

Vector2

Client-side
Server-side
Shared

Category: Vector

This is a 2D Vector class.

OOP Methods Help! I don't understand this!

create

Default constructor for the Vector2 class. Returns a Vector2 object.

Vector2(mixed vectorOrX[, float y])
  • vectorOrX: float | table | vector2 – Vector2, table, or float indicating vector's coordinates
  • y: float – If vectorOrX is a float, this is the Y coordinate

normalize

Normalizes the vector

bool Vector2.normalize(vector2 vector)
  • vector: vector2 – Vector2 to normalize

getX

Gets the X coordinate of a vector

float Vector2.getX(vector2 vector)
  • vector: vector2 – Vector2 to get X coordinate from

getY

Gets the Y coordinate of a vector

float Vector2.getY(vector2 vector)
  • vector: vector2 – Vector2 to get Y coordinate from

setX

Sets the X coordinate of a vector

bool Vector2.setX(vector2 vector, float x)
  • vector: vector2 – Vector2 to set X coordinate on
  • x: float – New X coordinate

setY

Sets the Y coordinate of a vector

bool Vector2.setY(vector2 vector, float y)
  • vector: vector2 – Vector2 to set Y coordinate on
  • y: float – New Y coordinate

getNormalized

Gets a normalized version of the vector

vector2 Vector2.getNormalized(vector2 vector)
  • vector: vector2 – Vector2 to get normalized version of

getLength

Gets the length of a vector

float Vector2.getLength(vector2 vector)
  • vector: vector2 – Vector2 to get length from

getSquaredLength

Gets the squared length of a vector

float Vector2.getSquaredLength(vector2 vector)
  • vector: vector2 – Vector2 to get squared length from

dot

Gets the dot product of two vectors

float Vector2.dot(vector2 vectorOne, vector2 vectorTwo)
  • vectorOne: vector2 – First vector
  • vectorTwo: vector2 – Second vector

Code Examples

Checks if the player is using a low resolution

addEventHandler ( "onClientResourceStart", resourceRoot, function()
local screenSize = Vector2(guiGetScreenSize())
if screenSize.x < 1360 and screenSize.y < 768 then
outputChatBox ("You are running on a low resolution")
end
end)

Draws a red line from center to cursor, 200px long

local screenW, screenH = guiGetScreenSize()
local centerVec = Vector2(screenW / 2, screenH / 2)
local mouseVec = Vector2()
local lineLength = 200
local lineColor = tocolor(255, 0, 0)
showCursor(true)
addEventHandler('onClientRender', root, function()
local curX, curY = getCursorPosition()
mouseVec.x, mouseVec.y = curX * screenW, curY * screenH
local dVec = Vector2(mouseVec - centerVec)
local scaleFactor = lineLength / dVec.length
mouseVec = centerVec + (dVec * scaleFactor)
dxDrawLine(centerVec.x, centerVec.y, mouseVec.x, mouseVec.y, lineColor)
end)