<?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=Jirppa</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=Jirppa"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Jirppa"/>
	<updated>2026-04-18T07:50:38Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=FindRotation3D&amp;diff=51675</id>
		<title>FindRotation3D</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=FindRotation3D&amp;diff=51675"/>
		<updated>2017-07-16T12:12:57Z</updated>

		<summary type="html">&lt;p&gt;Jirppa: Created page with &amp;quot;{{Useful Function}} &amp;lt;lowercasetitle&amp;gt;&amp;lt;/lowercasetitle&amp;gt; __NOTOC__ This function takes two sets of XYZ coordinates. It returns the 3D direction from point A to point B. This can...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Useful Function}}&lt;br /&gt;
&amp;lt;lowercasetitle&amp;gt;&amp;lt;/lowercasetitle&amp;gt;&lt;br /&gt;
__NOTOC__&lt;br /&gt;
This function takes two sets of XYZ coordinates. It returns the 3D direction from point A to point B. This can for example be used to make objects point at specified point&lt;br /&gt;
==Syntax==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;float float float findRotation3D( float x1, float y1, float z1, float x2, float y2, float z2 )&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Required Arguments===&lt;br /&gt;
* '''x1''': The X coordinate of the starting point.&lt;br /&gt;
* '''y1''': The Y coordinate of the starting point.&lt;br /&gt;
* '''z1''': The Z coordinate of the starting point.&lt;br /&gt;
* '''x2''': The X coordinate of the target point.&lt;br /&gt;
* '''y2''': The Y coordinate of the target point.&lt;br /&gt;
* '''z2''': The Z coordinate of the starting point.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function findRotation3D( x1, y1, z1, x2, y2, z2 ) &lt;br /&gt;
	local rotx = math.atan2 ( z2 - z1, getDistanceBetweenPoints2D ( x2,y2, x1,y1 ) )&lt;br /&gt;
	rotx = math.deg(rotx)&lt;br /&gt;
	local rotz = -math.deg( math.atan2( x2 - x1, y2 - y1 ) )&lt;br /&gt;
	rotz = rotz &amp;lt; 0 and rotz + 360 or rotz&lt;br /&gt;
	return rotx, 0,rotz&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This command makes the player's vehicle to rotate at vehicle near it.&lt;br /&gt;
''Note: This example needs [[OOP]] to be enabled in order to work''&lt;br /&gt;
&amp;lt;section name=&amp;quot;Client-side example&amp;quot; class=&amp;quot;client&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;
local function setRotationAtNearestVehicle()&lt;br /&gt;
&lt;br /&gt;
	if not localPlayer.vehicle then return end&lt;br /&gt;
&lt;br /&gt;
	local nearestVehicle = false&lt;br /&gt;
	local dist = 10000&lt;br /&gt;
	for index,vehicle in ipairs(getElementsByType(&amp;quot;vehicle&amp;quot;,root,true)) do&lt;br /&gt;
		if getDistanceBetweenPoints3D(localPlayer.vehicle.position,vehicle.position) &amp;lt; dist and localPlayer.vehicle ~= vehicle then&lt;br /&gt;
			nearestVehicle = vehicle&lt;br /&gt;
			dist = getDistanceBetweenPoints3D(localPlayer.vehicle.position,vehicle.position)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local x1,y1,z1 = getElementPosition(localPlayer.vehicle)&lt;br /&gt;
	local x2,y2,z2 = getElementPosition(nearestVehicle)&lt;br /&gt;
	local rx,ry,rz = findRotation3D(x1,y1,z1,x2,y2,z2)&lt;br /&gt;
&lt;br /&gt;
	localPlayer.vehicle.rotation = Vector3(rx,ry,rz)&lt;br /&gt;
	&lt;br /&gt;
end&lt;br /&gt;
	&lt;br /&gt;
addCommandHandler(&amp;quot;lookatnearestveh&amp;quot;,setRotationAtNearestVehicle)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thanks for '''ccw''' for alternative [[getCameraRotation]] function which formula is used in this function. Also thanks for '''Doomed_Space_Marine''' for more accurate formula for z-axis which is used in [[findRotation]]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Useful_Functions}}&lt;/div&gt;</summary>
		<author><name>Jirppa</name></author>
	</entry>
</feed>