<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Daddy</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Daddy"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Daddy"/>
	<updated>2026-05-20T22:17:00Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=GetElementMatrix&amp;diff=67564</id>
		<title>GetElementMatrix</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=GetElementMatrix&amp;diff=67564"/>
		<updated>2020-10-16T22:06:49Z</updated>

		<summary type="html">&lt;p&gt;Daddy: /* Syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Shared function}}&lt;br /&gt;
__NOTOC__ &lt;br /&gt;
This function gets an [[element]]'s transform [[matrix]]. This contains 16 float values that multiplied to a point will give you the point transformed. It is most useful for matrix calculations such as calculating offsets. For further information, please refer to a tutorial of matrices in computer graphics programming.&lt;br /&gt;
{{Note|The [[matrix]] returned by this function is [http://bugs.mtasa.com/view.php?id{{=}}6984 not setup correctly for some calculations] unless the '''legacy''' argument is set to '''''false'''''.}}&lt;br /&gt;
{{Tip|''For [[matrix]] manipulation which goes beyond the basic examples given on this page, see the [[Lua matrix library]].'' If you are using MTA: SA 1.4 or higher, using the built-in [[matrix]] class is also recommended.}}&lt;br /&gt;
==Syntax==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
table getElementMatrix ( element theElement [, bool legacy = true ] )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
{{OOP||[[element]]:getMatrix|matrix|setElementMatrix}}&lt;br /&gt;
&lt;br /&gt;
===Required Arguments=== &lt;br /&gt;
*'''theElement:''' The [[element]] which you wish to retrieve the [[matrix]] for.&lt;br /&gt;
&lt;br /&gt;
===Optional Arguments=== &lt;br /&gt;
*'''legacy:''' Set to ''false'' to return correctly setup [['''matrix''']] (i.e. Last column in the first 3 rows set to zero).&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns a multi-dimensional array (which can be transformed into a proper [[matrix]] class using ''Matrix.create'' method) containing a 4x4 matrix. Returns ''false'' if the element is not streamed in, and not a [[vehicle]], [[ped]] or [[object]].&lt;br /&gt;
&lt;br /&gt;
==Example== &lt;br /&gt;
This example creates a utility function that turns an offset into a position that is relative to the specified element.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getPositionFromElementOffset(element,offX,offY,offZ)&lt;br /&gt;
    local m = getElementMatrix ( element )  -- Get the matrix&lt;br /&gt;
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform&lt;br /&gt;
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]&lt;br /&gt;
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]&lt;br /&gt;
    return x, y, z                               -- Return the transformed point&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Get the position of a point 3 units to the right of the element:&lt;br /&gt;
x,y,z = getPositionFromElementOffset(element,3,0,0)&lt;br /&gt;
&lt;br /&gt;
-- Get the position of a point 2 units in front of the element:&lt;br /&gt;
x,y,z = getPositionFromElementOffset(element,0,2,0)&lt;br /&gt;
&lt;br /&gt;
-- Get the position of a point 1 unit above the element:&lt;br /&gt;
x,y,z = getPositionFromElementOffset(element,0,0,1)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example creates some more matrix utility functions&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getMatrixLeft(m)&lt;br /&gt;
    return m[1][1], m[1][2], m[1][3]&lt;br /&gt;
end&lt;br /&gt;
function getMatrixForward(m)&lt;br /&gt;
    return m[2][1], m[2][2], m[2][3]&lt;br /&gt;
end&lt;br /&gt;
function getMatrixUp(m)&lt;br /&gt;
    return m[3][1], m[3][2], m[3][3]&lt;br /&gt;
end&lt;br /&gt;
function getMatrixPosition(m)&lt;br /&gt;
    return m[4][1], m[4][2], m[4][3]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local mat = getElementMatrix(element)  -- Get the matrix&lt;br /&gt;
x,y,z = getMatrixLeft(mat)     -- Get the matrix left direction&lt;br /&gt;
x,y,z = getMatrixForward(mat)  -- Get the matrix forward direction&lt;br /&gt;
x,y,z = getMatrixUp(mat)       -- Get the matrix up direction&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example function allows you to get the element matrix of an element that is not streamed in.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getElementMatrix(element)&lt;br /&gt;
    local rx, ry, rz = getElementRotation(element, &amp;quot;ZXY&amp;quot;)&lt;br /&gt;
    rx, ry, rz = math.rad(rx), math.rad(ry), math.rad(rz)&lt;br /&gt;
    local matrix = {}&lt;br /&gt;
    matrix[1] = {}&lt;br /&gt;
    matrix[1][1] = math.cos(rz)*math.cos(ry) - math.sin(rz)*math.sin(rx)*math.sin(ry)&lt;br /&gt;
    matrix[1][2] = math.cos(ry)*math.sin(rz) + math.cos(rz)*math.sin(rx)*math.sin(ry)&lt;br /&gt;
    matrix[1][3] = -math.cos(rx)*math.sin(ry)&lt;br /&gt;
    matrix[1][4] = 1&lt;br /&gt;
    &lt;br /&gt;
    matrix[2] = {}&lt;br /&gt;
    matrix[2][1] = -math.cos(rx)*math.sin(rz)&lt;br /&gt;
    matrix[2][2] = math.cos(rz)*math.cos(rx)&lt;br /&gt;
    matrix[2][3] = math.sin(rx)&lt;br /&gt;
    matrix[2][4] = 1&lt;br /&gt;
	&lt;br /&gt;
    matrix[3] = {}&lt;br /&gt;
    matrix[3][1] = math.cos(rz)*math.sin(ry) + math.cos(ry)*math.sin(rz)*math.sin(rx)&lt;br /&gt;
    matrix[3][2] = math.sin(rz)*math.sin(ry) - math.cos(rz)*math.cos(ry)*math.sin(rx)&lt;br /&gt;
    matrix[3][3] = math.cos(rx)*math.cos(ry)&lt;br /&gt;
    matrix[3][4] = 1&lt;br /&gt;
	&lt;br /&gt;
    matrix[4] = {}&lt;br /&gt;
    matrix[4][1], matrix[4][2], matrix[4][3] = getElementPosition(element)&lt;br /&gt;
    matrix[4][4] = 1&lt;br /&gt;
	&lt;br /&gt;
    return matrix&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server side: Front to Front&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
-- create a Ped (0, 0, 5, 0) and put the player to 10 m of distance, front to front&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function startedThisResource (res)&lt;br /&gt;
	if getThisResource() == res then&lt;br /&gt;
		local thePed = createPed ( 287, 0, 0, 5, 0)&lt;br /&gt;
		local matrix = getElementMatrix(thePed)&lt;br /&gt;
		nx = 0 * matrix[1][1] + 10 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1]&lt;br /&gt;
		ny = 0 * matrix[1][2] + 10 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2]&lt;br /&gt;
		nz = 0 * matrix[1][3] + 10 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3]&lt;br /&gt;
		for a, z in ipairs(getElementsByType(&amp;quot;player&amp;quot;)) do&lt;br /&gt;
			setElementPosition (z, nx, ny, nz)&lt;br /&gt;
			local playerX, playerY, playerZ = getElementPosition(z)&lt;br /&gt;
			local pedX, pedY, pedZ = getElementPosition(thePed)&lt;br /&gt;
			local rotZ = findRotation( playerX, playerY, pedX, pedY ) &lt;br /&gt;
			setElementRotation(z, 0, 0, rotZ)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
addEventHandler(&amp;quot;onResourceStart&amp;quot;, getRootElement(), startedThisResource)&lt;br /&gt;
&lt;br /&gt;
function findRotation( x1, y1, x2, y2 ) &lt;br /&gt;
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) )&lt;br /&gt;
    return t &amp;lt; 0 and t + 360 or t&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
{{ChangelogHeader}}&lt;br /&gt;
{{ChangelogItem|1.3.0-9.04186|Added legacy argument}}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Client_element_functions}}&lt;/div&gt;</summary>
		<author><name>Daddy</name></author>
	</entry>
</feed>