Vector/Vector2: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ Category:Incomplete The Vector2 class is a class introduced in 1.4 ===Methods=== *create *normalize *Vector2.dot|dot...")
 
m (Added space between function and parameters in syntax)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
==Methods==
[[Category:Incomplete]]
===create===
The Vector2 class is a class introduced in 1.4
This is the default constructor for the Vector2 class and returns a Vector2 object.


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


*[[Vector2.create|create]]
====Required Arguments====
*[[Vector2.normalize|normalize]]
* '''vectorOrX''': [[{{PAGENAME}}|Vector2]], [[table|Table]] or [[float|Float]] indicating vector's coordinates
*[[Vector2.dot|dot]]


*[[Vector2.getLength|getLength]]
====Optional Arguments====
*[[Vector2.getSquaredLength|getSquaredLength]]
* '''y''': if ''vectorOrX'' is a float, this will indicate the vector's Y coordinate
*[[Vector2.getNormalized|getNormalized]]
*[[Vector2.getX|getX]]
*[[Vector2.getY|getY]]


*[[Vector2.setX|setX]]
====Example====
*[[Vector2.setY|setY]]
This example checks if the player is using a low resolution.
<section name="client" class="client" show="true">
<syntaxhighlight lang="lua">
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)
</syntaxhighlight>
</section>


===Variables===
===normalize===
This function normalizes the vector


*[[Vector2.x|x]]
====Syntax====
*[[Vector2.y|y]]
<syntaxhighlight lang="lua">
*[[Vector2.length|length]]
bool Vector2.normalize ( vector2 vector )
*[[Vector2.squaredLength|squaredLength]]
</syntaxhighlight>
*[[Vector2.normalized|normalized]]
{{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}}
 
====Required Arguments====
* '''vector''': [[{{PAGENAME}}|Vector2]] to set X coordinate from
* '''x''': New X coordinate
 
===setY===
This function sets the Y coordinate of a vector.
 
====Syntax====
<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
 
===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===
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

Latest revision as of 12:11, 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