Vector/Vector3

From Multi Theft Auto: Wiki
Revision as of 21:09, 5 February 2015 by AlexTMjugador (talk | contribs) (Documented normalize and getNormalized methods)
Jump to navigation Jump to search

The vector3 class represents a three-dimensional vector.

Methods

create

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

Syntax

vector3 Vector3 ( [ float x = 0, float y = 0, float z = 0 ] )
Optional arguments
  • x, y and z: coordinates for the vector. If not specified, they default to 0.
  • Instead of these three coordinates, a single Vector3 object may be inserted to clone it.

Example

This example sorts all players in a nice line on the center of the map.

Click to collapse [-]
Shared
local players = getElementsByType("player")
local newPlayerPosition = Vector3(-#players - 1, 0, 10) -- Initialize the position vector for the first player in the list
for _, player in ipairs(players) do
    -- Move each player 1 unit forward in X from the previous one
    newPlayerPosition.x = newPlayerPosition.x + 1
    setElementPosition(player, newPlayerPosition)
end

cross

dot

Calculates the (standard) dot/scalar product of two vectors. This can be used to calculate the angle between them. If the standard scalar product is 0, both vectors are orthogonal.

Syntax

float Vector3:dot ( vector3 vector )
Required arguments
  • vector: a vector3 object to get the dot product with.

Example

This examples illustrates the concept of dot/scalar product and implements a useful function which can be used to get the angle between two vectors.

Click to collapse [-]
Shared
local vec1 = Vector3(1, 0, 0)
local vec2 = Vector3(0, 0, 0)
local dotproduct = vec1:dot(vec2)

if dotproduct == 0 then
    outputDebugString("vec1 is orthogonal to vec2")
end

-- Calculate angle between vec1 and vec2
function angle(vec1, vec2)
    -- Calculate the angle by applying law of cosines
    return math.acos(vec1:dot(vec2)/(vec1.length*vec2.length)
end

outputDebugString("Angle between vec1 and vec2: "..math.deg(angle(vec1, vec2)).."°")

normalize

Converts a vector to a unit vector (a vector of length 1).

Syntax

bool Vector3:normalize ( )

Example

This example slowly moves all the players' camera to look at the Mount Chilliad.

Click to collapse [-]
Client
local targetPosition = Vector3(-2627.32, -1083.2, 433.35) -- Somewhere in Mount Chilliad

local function moveCameraToTarget(deltaTime)
    local currentPosition = Vector3(getCameraMatrix())
    local direction = targetPosition - currentPosition
    direction:normalize() -- Get a direction vector by normalizing the vector from the current position to the target position
    setCameraMatrix(currentPosition + direction * deltaTime * 0.05, -2589.45, -1174.49, 418.09)
end
addEventHandler("onClientPreRender", root, moveCameraToTarget)

getX and setX

getY and setY

getZ and setZ

getNormalized

Returns a normalized vector (of length 1) of the vector it's used on. Differently from the Vector3:normalize method, this one returns a vector3 and doesn't modify the original vector.

Syntax

vector3 Vector3:getNormalized ( )

Example

This example slowly moves all the players' camera to look at the Mount Chilliad with a shorter code than the previous example.

Click to collapse [-]
Client
local targetPosition = Vector3(-2627.32, -1083.2, 433.35) -- Somewhere in Mount Chilliad

local function moveCameraToTarget(deltaTime)
    local currentPosition = Vector3(getCameraMatrix())
    local direction = (targetPosition - currentPosition):getNormalized() -- Get a direction vector by normalizing the vector from the current position to the target position
    setCameraMatrix(currentPosition + direction * deltaTime * 0.05, -2589.45, -1174.49, 418.09)
end
addEventHandler("onClientPreRender", root, moveCameraToTarget)

getSquaredLength

getLength