Vector/Vector2: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Updated Vector2 class)
Line 1: Line 1:
__NOTOC__
[[Category:Incomplete]]
The Vector2 class is a class introduced in 1.4
==Methods==
==Methods==
===create===
===create===
This is default constructor for the Vector2 class and returns a Vector2 object.
This is the default constructor for the Vector2 class and returns a Vector2 object.


====Syntax====
====Syntax====
<syntaxhighlight lang="lua">vector2 Vector2 ( float x = 0, float y = 0 )</syntaxhighlight>
<syntaxhighlight lang="lua">
vector2 Vector2 ( mixed vectorOrX [, float y ] )
</syntaxhighlight>


* '''x''', and '''y''' coordinates for the vector. If not specified, they default to 0.
====Required Arguments====
* Instead of these two coordinates, a single Vector2 object may be inserted to clone it.
* '''vectorOrX''': [[{{PAGENAME}}|Vector2]], [[table|Table]] or [[float|Float]] indicating vector's coordinates
 
====Optional Arguments====
* '''y''': if ''vectorOrX'' is a float, this will indicate the vector's Y coordinate


====Example====
====Example====
This example checks if the player is using a low resolution.
This example checks if the player is using a low resolution.
<section name="Client" class="client" show="true">
<section name="client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function resolution ()
addEventHandler ( "onClientResourceStart", resourceRoot, function()
  local screenSize = Vector2(guiGetScreenSize())
local screenSize = Vector2(guiGetScreenSize())
  if ( screenSize.x < 1360 ) and ( screenSize.y < 768 ) then  
if screenSize.x < 1360 and screenSize.y < 768 then  
    outputChatBox ("You are running on a low resolution")
outputChatBox ("You are running on a low resolution")
  end
end
end
end)
addEventHandler ( "onClientResourceStart",resourceRoot,resolution)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
===normalize===
===normalize===
===dot===
This function normalizes the vector
 
====Syntax====
<syntaxhighlight lang="lua">
bool Vector2.normalize ( vector2 vector )
</syntaxhighlight>
{{OOP|Normalizes a vector|[[{{PAGENAME}}|Vector2]]:normalize}}
 
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to normalize
 
====Example====
This example demonstrates how to draw a line that is 200 pixels long and red.
The line is drawn from the center of the screen to the mouse cursor's position.
<section name="client" class="client" show="true">
<syntaxhighlight lang="lua">
local screenW, screenH = guiGetScreenSize() -- get screen size
 
local centerVec = Vector2(screenW / 2, screenH / 2) -- get screen center point
local mouseVec = Vector2() -- predefined variable that will change later
 
local lineLength = 200 -- sets length of the drawn line
local lineColor = tocolor(255, 0, 0) -- set line color
 
showCursor(true) -- show cursor
 
addEventHandler('onClientRender', root, function()
local curX, curY = getCursorPosition()
mouseVec.x, mouseVec.y = curX * screenW, curY * screenH -- get absolute cursor position
   
local dVec = Vector2(mouseVec - centerVec) -- get the distance between the mouse position and the center of the screen
local scaleFactor = lineLength / dVec.length
   
mouseVec = centerVec + (dVec * scaleFactor)
 
dxDrawLine(centerVec.x, centerVec.y, mouseVec.x, mouseVec.y, color)
end)
</syntaxhighlight>
</section>
 
===getX===
This function gets the X coordinate of a vector.
 
====Syntax====
<syntaxhighlight lang="lua">
float Vector2.getX ( vector2 vector )
</syntaxhighlight>
{{OOP|Gets X coordinate of a vector|[[{{PAGENAME}}|Vector2]]:getX|x}}
 
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to get X coordinate from
 
===getY===
This function gets the Y coordinate of a vector.
 
====Syntax====
<syntaxhighlight lang="lua">
float Vector2.getY ( vector2 vector )
</syntaxhighlight>
{{OOP|Gets Y coordinate of a vector|[[{{PAGENAME}}|Vector2]]:getY|y}}
 
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to get Y coordinate from
 
===setX===
This function sets the X coordinate of a vector.
 
====Syntax====
<syntaxhighlight lang="lua">
bool Vector2.setX ( vector2 vector, float x )
</syntaxhighlight>
{{OOP|Sets X coordinate of a vector|[[{{PAGENAME}}|Vector2]]:setX|x}}


===getX/Y and setX/Y===
====Required Arguments====
These functions allow you get and set specific coordinates:
* '''vector''': [[{{PAGENAME}}|Vector2]] to set X coordinate from
* getX and setX
* '''x''': New X coordinate
* getY and setY


====Generic set syntax====
===setY===
<syntaxhighlight lang="lua">bool vec:setX ( float x = 0 )</syntaxhighlight>
This function sets the Y coordinate of a vector.
* '''x''': number value to set the coordinate to
 
* This value can also be set by the variable '''vec.x'''
====Syntax====
* Returns ''true'' if doesn't fail
<syntaxhighlight lang="lua">
bool Vector2.setY ( vector2 vector, float y )
</syntaxhighlight>
{{OOP|Sets Y coordinate of a vector|[[{{PAGENAME}}|Vector2]]:setY|y}}
 
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to set Y coordinate from
* '''Y''': New Y coordinate


====Generic get syntax====
<syntaxhighlight lang="lua">float vec:getX()</syntaxhighlight>
* This value can also be accessed by the variable '''vec.x'''
* Returns a '''float''' if doesn't fail
===getNormalized===
===getNormalized===
This function gets a normalized vector.
====Syntax====
<syntaxhighlight lang="lua">
vector2 Vector2.getNormalized ( vector2 vector )
</syntaxhighlight>
{{OOP|Gets a normalized vector|[[{{PAGENAME}}|Vector2]]:getNormalized|normalized}}
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to get normalized version of
===getLength===
This function gets the length of a vector.
====Syntax====
<syntaxhighlight lang="lua">
vector2 Vector2.getLength ( vector2 vector )
</syntaxhighlight>
{{OOP|Gets the length of a vector|[[{{PAGENAME}}|Vector2]]:getLength|length}}
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to get length from
===getSquaredLength===
===getSquaredLength===
===getLength===
This function gets the squared length of a vector.
 
====Syntax====
<syntaxhighlight lang="lua">
vector2 Vector2.getSquaredLength ( vector2 vector )
</syntaxhighlight>
{{OOP|Gets the squared length of a vector|[[{{PAGENAME}}|Vector2]]:getSquaredLength|squaredLength}}
 
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to get squared length from
 
===dot===
This function gets the dot product of two vectors.
 
====Syntax====
<syntaxhighlight lang="lua">
vector2 Vector2.dot( vector2 vectorOne, vector2 vectorTwo )
</syntaxhighlight>
{{OOP|Gets the dot product of two vectors|[[{{PAGENAME}}|Vector2]]:dot}}
 
====Required Arguments====
* '''vectorOne''': First [[{{PAGENAME}}|Vector2]] to calculate the dot product of
* '''vectorTwo''': Second [[{{PAGENAME}}|Vector2]] to calculate the dot product of

Revision as of 12:09, 1 August 2023

Methods

create

This is the default constructor for the Vector2 class and returns a Vector2 object.

Syntax

vector2 Vector2 ( mixed vectorOrX [, float y ] )

Required Arguments

Optional Arguments

  • y: if vectorOrX is a float, this will indicate the vector's Y coordinate

Example

This example checks if the player is using a low resolution.

Click to collapse [-]
client
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)

normalize

This function normalizes the vector

Syntax

bool Vector2.normalize ( vector2 vector )

OOP Syntax Help! I don't understand this!

Note: Normalizes a vector
Method: Vector2:normalize(...)


Required Arguments

Example

This example demonstrates how to draw a line that is 200 pixels long and red. The line is drawn from the center of the screen to the mouse cursor's position.

Click to collapse [-]
client
local screenW, screenH = guiGetScreenSize() -- get screen size

local centerVec = Vector2(screenW / 2, screenH / 2) -- get screen center point
local mouseVec = Vector2() -- predefined variable that will change later

local lineLength = 200 -- sets length of the drawn line
local lineColor = tocolor(255, 0, 0) -- set line color

showCursor(true) -- show cursor

addEventHandler('onClientRender', root, function()
	local curX, curY = getCursorPosition()
	mouseVec.x, mouseVec.y = curX * screenW, curY * screenH -- get absolute cursor position
    
	local dVec = Vector2(mouseVec - centerVec) -- get the distance between the mouse position and the center of the screen
	local scaleFactor = lineLength / dVec.length
    
	mouseVec = centerVec + (dVec * scaleFactor)

	dxDrawLine(centerVec.x, centerVec.y, mouseVec.x, mouseVec.y, color)
end)

getX

This function gets the X coordinate of a vector.

Syntax

float Vector2.getX ( vector2 vector )

OOP Syntax Help! I don't understand this!

Note: Gets X coordinate of a vector
Method: Vector2:getX(...)
Variable: .x


Required Arguments

  • vector: Vector2 to get X coordinate from

getY

This function gets the Y coordinate of a vector.

Syntax

float Vector2.getY ( vector2 vector )

OOP Syntax Help! I don't understand this!

Note: Gets Y coordinate of a vector
Method: Vector2:getY(...)
Variable: .y


Required Arguments

  • vector: Vector2 to get Y coordinate from

setX

This function sets the X coordinate of a vector.

Syntax

bool Vector2.setX ( vector2 vector, float x )

OOP Syntax Help! I don't understand this!

Note: Sets X coordinate of a vector
Method: Vector2:setX(...)
Variable: .x


Required Arguments

  • vector: Vector2 to set X coordinate from
  • x: New X coordinate

setY

This function sets the Y coordinate of a vector.

Syntax

bool Vector2.setY ( vector2 vector, float y )

OOP Syntax Help! I don't understand this!

Note: Sets Y coordinate of a vector
Method: Vector2:setY(...)
Variable: .y


Required Arguments

  • vector: Vector2 to set Y coordinate from
  • Y: New Y coordinate

getNormalized

This function gets a normalized vector.

Syntax

vector2 Vector2.getNormalized ( vector2 vector )

OOP Syntax Help! I don't understand this!

Note: Gets a normalized vector
Method: Vector2:getNormalized(...)
Variable: .normalized


Required Arguments

  • vector: Vector2 to get normalized version of

getLength

This function gets the length of a vector.

Syntax

vector2 Vector2.getLength ( vector2 vector )

OOP Syntax Help! I don't understand this!

Note: Gets the length of a vector
Method: Vector2:getLength(...)
Variable: .length


Required Arguments

getSquaredLength

This function gets the squared length of a vector.

Syntax

vector2 Vector2.getSquaredLength ( vector2 vector )

OOP Syntax Help! I don't understand this!

Note: Gets the squared length of a vector
Method: Vector2:getSquaredLength(...)
Variable: .squaredLength


Required Arguments

  • vector: Vector2 to get squared length from

dot

This function gets the dot product of two vectors.

Syntax

vector2 Vector2.dot( vector2 vectorOne, vector2 vectorTwo )

OOP Syntax Help! I don't understand this!

Note: Gets the dot product of two vectors
Method: Vector2:dot(...)


Required Arguments

  • vectorOne: First Vector2 to calculate the dot product of
  • vectorTwo: Second Vector2 to calculate the dot product of