<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/wiki/GetElementRelatedAngle?action=history&amp;feed=atom</id>
	<title>GetElementRelatedAngle - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/wiki/GetElementRelatedAngle?action=history&amp;feed=atom"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=GetElementRelatedAngle&amp;action=history"/>
	<updated>2026-05-14T18:24:59Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=GetElementRelatedAngle&amp;diff=76387&amp;oldid=prev</id>
		<title>LordHenry: /* Syntax */</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=GetElementRelatedAngle&amp;diff=76387&amp;oldid=prev"/>
		<updated>2023-04-04T01:09:42Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Syntax&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 01:09, 4 April 2023&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l4&quot;&gt;Line 4:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 4:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Syntax==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Syntax==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;float, &lt;/del&gt;float getElementRelatedAngle (element thisElement, element targetElement)&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;float getElementRelatedAngle (element thisElement, element targetElement)&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Required Arguments===&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Required Arguments===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* '''thisElement''': The element whose '''XY position''' will be checked to obtain the angle relative to the other element.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* '''thisElement''': The element whose '''XY position''' will be checked to obtain the angle relative to the other element.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wiki-wiki_dev_:diff::1.12:old-76386:rev-76387 --&gt;
&lt;/table&gt;</summary>
		<author><name>LordHenry</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=GetElementRelatedAngle&amp;diff=76386&amp;oldid=prev</id>
		<title>LordHenry: Created page with &quot;__NOTOC__ {{Useful Function}} This function returns the related angle between one element to another. This is useful to check if a player is on the left side of a vehicle, for example.  ==Syntax== &lt;syntaxhighlight lang=&quot;lua&quot;&gt;float, float getElementRelatedAngle (element thisElement, element targetElement)&lt;/syntaxhighlight&gt; ===Required Arguments=== * '''thisElement''': The element whose '''XY position''' will be checked to obtain the angle relative to the other...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=GetElementRelatedAngle&amp;diff=76386&amp;oldid=prev"/>
		<updated>2023-04-04T01:09:15Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__NOTOC__ {{Useful Function}} This function returns the related angle between one &lt;a href=&quot;/wiki/Element&quot; title=&quot;Element&quot;&gt;element&lt;/a&gt; to another. This is useful to check if a player is on the left side of a vehicle, for example.  ==Syntax== &amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;float, float getElementRelatedAngle (element thisElement, element targetElement)&amp;lt;/syntaxhighlight&amp;gt; ===Required Arguments=== * &amp;#039;&amp;#039;&amp;#039;thisElement&amp;#039;&amp;#039;&amp;#039;: The element whose &amp;#039;&amp;#039;&amp;#039;XY position&amp;#039;&amp;#039;&amp;#039; will be checked to obtain the angle relative to the other...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__NOTOC__&lt;br /&gt;
{{Useful Function}}&lt;br /&gt;
This function returns the related angle between one [[Element|element]] to another. This is useful to check if a player is on the left side of a vehicle, for example.&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;float, float getElementRelatedAngle (element thisElement, element targetElement)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===Required Arguments===&lt;br /&gt;
* '''thisElement''': The element whose '''XY position''' will be checked to obtain the angle relative to the other element.&lt;br /&gt;
* '''targetElement''': The element whose '''XY position''' and '''Z rotation''' will be checked to obtain the angle relative to the other element.&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns a float indicating the related angle between the first element and the second. It returns ''false'' if invalid parameters are provided.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server/Client-side script&amp;quot; class=&amp;quot;both&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getElementRelatedAngle (thisElement, targetElement)&lt;br /&gt;
    if isElement(thisElement) and isElement(targetElement) then&lt;br /&gt;
        local pointX, pointY = getElementPosition(thisElement)&lt;br /&gt;
        local x, y = getElementPosition(targetElement)&lt;br /&gt;
        local _, _, rz = getElementRotation(targetElement)&lt;br /&gt;
        local anglePointToElement = math.atan2(pointX - x, pointY - y)&lt;br /&gt;
        local formattedAnglePointToElement = math.deg(anglePointToElement)&lt;br /&gt;
        local elementRelatedPosition = formattedAnglePointToElement + rz&lt;br /&gt;
        &lt;br /&gt;
        -- Valid angles must be from 0 to 359.9999999999&lt;br /&gt;
        if (elementRelatedPosition &amp;lt; 0) then&lt;br /&gt;
            elementRelatedPosition = elementRelatedPosition + 360 -- Sets any negative angle to valid angle.&lt;br /&gt;
        elseif (elementRelatedPosition &amp;gt;= 360) then&lt;br /&gt;
            elementRelatedPosition = elementRelatedPosition - 360 -- Sets any angle higher than 360 to valid angle. 360 becomes 0.&lt;br /&gt;
        end&lt;br /&gt;
        &lt;br /&gt;
        return elementRelatedPosition&lt;br /&gt;
    else&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example== &lt;br /&gt;
&amp;lt;section name=&amp;quot;Client&amp;quot; class=&amp;quot;client&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This example allows the player to press ''H'' near a vehicle to open its door according to the vehicle related angle. If the player is on the front of the vehicle, it opens the hood, if the player is on the back of the vehicle, it opens the trunk and so on.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getNearestElement(thePlayer, elementType) -- Useful function (simplified version)&lt;br /&gt;
    local dist = 20 -- Only gets elements nearer than this distance.&lt;br /&gt;
    local x, y, z = getElementPosition(thePlayer)&lt;br /&gt;
    local nearest = false&lt;br /&gt;
    for _,veh in pairs(getElementsByType(elementType)) do&lt;br /&gt;
        local vx, vy, vz = getElementPosition(veh)&lt;br /&gt;
        local newDist = getDistanceBetweenPoints3D(x, y, z, vx, vy, vz)&lt;br /&gt;
        if newDist &amp;lt; dist then&lt;br /&gt;
            dist = newDist&lt;br /&gt;
            nearest = veh&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return nearest, dist&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
bindKey(&amp;quot;h&amp;quot;, &amp;quot;down&amp;quot;, function(key, state)&lt;br /&gt;
    local veh, distance = getNearestElement(localPlayer, &amp;quot;vehicle&amp;quot;)&lt;br /&gt;
    if veh and isElement(veh) then&lt;br /&gt;
        if distance &amp;lt; 5 then&lt;br /&gt;
            local angle = getElementRelatedAngle(localPlayer, veh)&lt;br /&gt;
            local door&lt;br /&gt;
            if angle &amp;gt;= 340 or angle &amp;lt; 20 then&lt;br /&gt;
                door = 0&lt;br /&gt;
            elseif angle &amp;gt;= 20 and angle &amp;lt; 90 then&lt;br /&gt;
                door = 3&lt;br /&gt;
            elseif angle &amp;gt;= 90 and angle &amp;lt; 160 then&lt;br /&gt;
                door = 5&lt;br /&gt;
            elseif angle &amp;gt;= 160 and angle &amp;lt; 200 then&lt;br /&gt;
                door = 1&lt;br /&gt;
            elseif angle &amp;gt;= 200 and angle &amp;lt; 270 then&lt;br /&gt;
                door = 4&lt;br /&gt;
            elseif angle &amp;gt;= 270 and angle &amp;lt; 340 then&lt;br /&gt;
                door = 2&lt;br /&gt;
            end&lt;br /&gt;
            if getVehicleDoorOpenRatio (veh, door) &amp;gt; 0 then&lt;br /&gt;
                setVehicleDoorOpenRatio (veh, door, 0, 500)&lt;br /&gt;
            else&lt;br /&gt;
                setVehicleDoorOpenRatio (veh, door, 1, 500)&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
Author: [[User:LordHenry|LordHenry]]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Useful_Functions}}&lt;/div&gt;</summary>
		<author><name>LordHenry</name></author>
	</entry>
</feed>