<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Maratis forum - rotate the object?]]></title>
	<link rel="self" href="http://forum.maratis3d.com/extern.php?action=feed&amp;tid=1011&amp;type=atom"/>
	<updated>2014-07-07T12:31:42Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.maratis3d.com/viewtopic.php?id=1011</id>
		<entry>
			<title type="html"><![CDATA[Re: rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6962#p6962"/>
			<content type="html"><![CDATA[<div class="codebox"><pre><code>obj1 = getObject(&quot;obj1&quot;)
obj2 = getObject(&quot;obj2&quot;)

function onSceneUpdate()

    dir = normalize(getPosition(obj2) - getPosition(obj1))
    lookAt(obj1, dir, 1)

end</code></pre></div>]]></content>
			<author>
				<name><![CDATA[anael]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=2</uri>
			</author>
			<updated>2014-07-07T12:31:42Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6962#p6962</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6961#p6961"/>
			<content type="html"><![CDATA[<p>I don&#039;t use behavior, I use Lua function.<br />I copied all three functions:<br /></p><div class="codebox"><pre><code>function getLength2d(vec)
    ...
end
function normalize2d(vec)
    ...
end
function lookAt(object, dir, rotSpeed)
    ...
end
obj1 = getObject(&quot;obj1&quot;)
obj2 = getObject(&quot;obj2&quot;)

function onSceneUpdate()
    dir = getPosition(obj2)
    lookAt(obj1, dir, 1)
end</code></pre></div><p>nothing happens.</p>]]></content>
			<author>
				<name><![CDATA[ant0n]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1562</uri>
			</author>
			<updated>2014-07-07T11:58:23Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6961#p6961</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6960#p6960"/>
			<content type="html"><![CDATA[<p>look-at is a behavior :<br />select your object in the editor &gt; go to behavior tab &gt; add a look-at behavior &gt; set the name of the target</p><p>(except is you use the script I gave before, in this case copy/paste it on top of your script)</p>]]></content>
			<author>
				<name><![CDATA[anael]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=2</uri>
			</author>
			<updated>2014-07-07T11:36:47Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6960#p6960</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6959#p6959"/>
			<content type="html"><![CDATA[<p>I have not function lookAt(). What am I doing wrong?<br />--onSceneUpdate()<br />dir = getPosition(object2)<br />lookAt(object1, dir, 1)</p>]]></content>
			<author>
				<name><![CDATA[ant0n]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1562</uri>
			</author>
			<updated>2014-07-07T09:43:37Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6959#p6959</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6944#p6944"/>
			<content type="html"><![CDATA[<p>Thanks anael! This is exactly what I need.</p>]]></content>
			<author>
				<name><![CDATA[ant0n]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1562</uri>
			</author>
			<updated>2014-06-24T10:42:06Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6944#p6944</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6943#p6943"/>
			<content type="html"><![CDATA[<p>You can use the look-at behavior :<br />- create an invisible object called &quot;dummy&quot;<br />- add a look-at behavior to your first object and set the target to the dummy<br />- in script, set the dummy X and Z position to be the X and Z position of the object you want to point at<br />- set the dummy Y position to 0 or to your first object Y position</p><br /><p>Or use some math in pure lua, but it&#039;s more complicated (this example rotate on Z axis) :</p><div class="codebox"><pre><code>-- get 2d vector length
function getLength2d(vec)

    return math.sqrt(vec[1]*vec[1] + vec[2]*vec[2])

end

-- normalize 2d vector
function normalize2d(vec)

    length = getLength2d(vec)
    vec[1] = vec[1] / length
    vec[2] = vec[2] / length
    return vec

end

-- lookAt
function lookAt(object, dir, rotSpeed)

    -- compute object X dir and Y dir
    rot = getRotation(object)

    zAngle = math.rad(rot[3])
    sinZ = math.sin(zAngle)
    cosZ = math.cos(zAngle)

    YDir = normalize2d({sinZ, -cosZ})
    XDir = {-YDir[2], YDir[1]}

    -- dot products for orientation and angle
    ori = (dir[1]*XDir[1] + dir[2]*XDir[2])
    angle = math.acos(dir[1]*YDir[1] + dir[2]*YDir[2])

    if ori &lt; 0 then
        angle = - angle
    end

    rotate(object, {0, 0, 1}, math.deg(angle) * rotSpeed)

end</code></pre></div>]]></content>
			<author>
				<name><![CDATA[anael]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=2</uri>
			</author>
			<updated>2014-06-24T09:32:28Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6943#p6943</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[rotate the object?]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=6942#p6942"/>
			<content type="html"><![CDATA[<p>hi all! <br />how to rotate the object in the direction of another entity only on the Y axis (lua)?</p>]]></content>
			<author>
				<name><![CDATA[ant0n]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1562</uri>
			</author>
			<updated>2014-06-24T03:16:37Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=6942#p6942</id>
		</entry>
</feed>
