<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Maratis forum - Shaders and Mouse Clicking]]></title>
		<link>http://forum.maratis3d.com/viewtopic.php?id=143</link>
		<description><![CDATA[The most recent posts in Shaders and Mouse Clicking.]]></description>
		<lastBuildDate>Sat, 29 Sep 2012 23:33:03 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2702#p2702</link>
			<description><![CDATA[<p>ok. here what i managed to make:</p><div class="codebox"><pre><code>box = getObject(&quot;Entity6&quot;)
camera = getObject(&quot;Camera0&quot;)
RAYDIST = 500 -- distance to click the objects in game units
fov = getCameraFov(camera)
    
-- scene update
function onSceneUpdate()
    
    -- mouse click objects
    if onKeyDown(&quot;MOUSE_BUTTON1&quot;) then
        startPT = getPosition(camera) -- take camera position as start point
        -- now calculate end point by converting 3D vector to coordinates
                   camRot = getRotation(camera)
            camRotX = 180-camRot[1] -- corrected camera rotation from down to up (0-180 deg)
            camRotZ = 90+camRot[3] -- camera rotation left/right
           
            -- calculate angle of mouse cursor relative to camera
            mx = getAxis(&quot;MOUSE_X&quot;) - 0.5
            my = getAxis(&quot;MOUSE_Y&quot;) - 0.5
           
             mRotX = math.deg(math.asin(my / fov)) * fov
            mRotY = -math.deg(math.asin(mx / fov)) * fov * 1.75
           
            endPointX = RAYDIST * math.sin(math.rad(camRotX + mRotX)) * math.cos(math.rad(camRotZ + mRotY)) + startPT[1]
            endPointY = RAYDIST * math.sin(math.rad(camRotX + mRotX)) * math.sin(math.rad(camRotZ + mRotY)) + startPT[2]
            endPointZ = RAYDIST * math.cos(math.rad(camRotX + mRotX)) + startPT[3]
           
            endPT = {endPointX, endPointY, endPointZ}
            
            point = rayHit(startPT, endPT) -- trace to any object in given direction
            if point then
                setScale(box, {0.2, 0.2, 0.2})
                setPosition(box, {point[1], point[2], point[3]}) -- send scaled box there
            end

     end

end</code></pre></div><p>but i have problem with mRotX and mRotY variables. it looks like camera have some lens distortion.</p><p>anyway, wait for that function. it will make this much easier <img src="http://forum.maratis3d.com/img/smilies/smile.png" width="15" height="15" alt="smile" /></p>]]></description>
			<author><![CDATA[dummy@example.com (Felis)]]></author>
			<pubDate>Sat, 29 Sep 2012 23:33:03 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2702#p2702</guid>
		</item>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2701#p2701</link>
			<description><![CDATA[<p>trying to test in Jules example project. i&#039;ve set camera to follow Jules closely. the problem is i cannot detect readable camera rotation.<br />what axises does getRotation() function returns?<br />my small code:<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; camRot = getRotation(camera)<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; camRotX = camRot[1]<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; camRotY = camRot[2]<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; camRotZ = camRot[3]<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; print(&quot;camera rotation:&quot;, camRotX, camRotY, camRotZ)<br />it returns this:<br />camera rotation:&nbsp; &nbsp; 63.430767059326&nbsp; &nbsp; -8.5377365621753e-07&nbsp; &nbsp; 19.999326705933<br />camera rotation:&nbsp; &nbsp; 63.430732727051&nbsp; &nbsp; -8.5377359937411e-06&nbsp; &nbsp; 104.99935150146<br />camera rotation:&nbsp; &nbsp; 63.430698394775&nbsp; &nbsp; 1.7075473124351e-06&nbsp; &nbsp; -150.00065612793<br />camera rotation:&nbsp; &nbsp; 63.430725097656&nbsp; &nbsp; 5.122642050992e-06&nbsp; &nbsp; -35.000694274902</p><p>so i think results are:<br />camRotX is rotation of camera upward from the direct down (0) to up (180)<br />camRotY is camera tilt<br />camRotZ is camera rotation left/right</p><p>now to trigonometry...</p>]]></description>
			<author><![CDATA[dummy@example.com (Felis)]]></author>
			<pubDate>Sat, 29 Sep 2012 22:17:49 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2701#p2701</guid>
		</item>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2700#p2700</link>
			<description><![CDATA[<p>i think we can use rayHit() for detecting object&#039;s position by directing it to the camera and mouse vector, then put our &quot;cursor&quot; object there and check the object by isCollisionBetween().<br />of-course we will have to check all the &quot;trigger&quot; object, to find if we hit one of them.</p>]]></description>
			<author><![CDATA[dummy@example.com (Felis)]]></author>
			<pubDate>Sat, 29 Sep 2012 20:44:44 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2700#p2700</guid>
		</item>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2699#p2699</link>
			<description><![CDATA[<p>yes, we&#039;ll add it by default for next version.</p><p>If you want doing something with lua only, if your game is 2d you could use a ghost box and detect it&#039;s collision with objects.</p><p>For 3d it&#039;s a bit more complex, it&#039;s possible to use rayHit :<br />- link an object to the camera, it will act as a cursor (it will stick to the cam)<br />- move the cursor using the mouse position multiplied by a ratio<br />- you can find the ray using the cursor position and the camera position :<br />&nbsp; &nbsp;beginRay = cameraPosition<br />&nbsp; &nbsp;endRay = cameraPosition + (cursorPosition - cameraPosition)*factor</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Sat, 29 Sep 2012 16:05:06 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2699#p2699</guid>
		</item>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2698#p2698</link>
			<description><![CDATA[<p>unfortunately i&#039;m not C++ programmer and can make only simple scripts in Lua.<br />hope to see this function implemented in engine soon.</p>]]></description>
			<author><![CDATA[dummy@example.com (Felis)]]></author>
			<pubDate>Sat, 29 Sep 2012 14:13:00 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2698#p2698</guid>
		</item>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2697#p2697</link>
			<description><![CDATA[<p>rayHit doesn&#039;t return the object but the intersection point,<br />to know if it hit an object, the object is passed in argument :</p><p>point = rayHit(start, end, object)<br />if point then<br />&nbsp; &nbsp; -- there is a hit with object<br />end</p><p>Mikey shared a additional script function (written in c++) :<br /><a href="http://forum.maratis3d.com/viewtopic.php?id=434">http://forum.maratis3d.com/viewtopic.php?id=434</a></p><p>Here is the function code :<br /></p><div class="codebox"><pre><code>int pickObject(void)
{
    MEngine * engine = MEngine::getInstance();
    MScriptContext * script = engine-&gt;getScriptContext();
    
    if (script-&gt;getArgsNumber() == 3)
    {
        MOCamera * camera = (MOCamera *)script-&gt;getPointer(0);
        float x = script-&gt;getFloat(1);
        float y = script-&gt;getFloat(2);

        MPhysicsContext * physics = engine-&gt;getPhysicsContext();
        
        unsigned int width, height;
        engine-&gt;getSystemContext()-&gt;getScreenSize(&amp;width, &amp;height);

        MVector3 rayO = camera-&gt;getTransformedPosition();
        MVector3 rayD = camera-&gt;getUnProjectedPoint(MVector3(x * float(width), (1 - y) * float(height), 1));
        rayD = rayO + ((rayD - rayO).getNormalized() * (camera-&gt;getClippingFar() - camera-&gt;getClippingNear()));

        unsigned int objId;
        if (physics-&gt;isRayHit(rayO, rayD, &amp;objId))
        {
            MLevel * level = engine-&gt;getLevel();
            MScene * scene = level-&gt;getCurrentScene();
            unsigned int entCount = scene-&gt;getEntitiesNumber();
            for (unsigned int i = 0; i &lt; entCount; ++i)
            {
                MOEntity * entity = scene-&gt;getEntityByIndex(i);
                MPhysicsProperties * phyProps = entity-&gt;getPhysicsProperties();
                if (phyProps)
                {
                    if (phyProps-&gt;getCollisionObjectId() == objId)
                    {
                        script-&gt;pushPointer(entity);
                        return 1;
                    }
                }
            }
        }
    }
    
    return 0;
}</code></pre></div><p>Used like that in lua :<br /></p><div class="codebox"><pre><code>obj = pickObject(Camera, x, y)
if obj then
...
end</code></pre></div><p>This is the way to add the function through a game plugin (to do in StartPlugin) :<br /></p><div class="codebox"><pre><code>MEngine* engine = MEngine::getInstance();
MScriptContext* script = engine-&gt;getScriptContext();
if(script)
    script-&gt;addFunction(&quot;pickObject&quot;, pickObject);</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Sat, 29 Sep 2012 08:37:42 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2697#p2697</guid>
		</item>
		<item>
			<title><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2694#p2694</link>
			<description><![CDATA[<p>same problem here. <br />i thought about rayHit(), but i&#039;m not sure if it return name of hited object. and there will be some math to calculate hit vector, using mouse coordinates and camera orientation.<br />also i think about some ghost object as mouse pointer, but it will require the full list of objects in scene.</p>]]></description>
			<author><![CDATA[dummy@example.com (Felis)]]></author>
			<pubDate>Sat, 29 Sep 2012 00:50:34 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2694#p2694</guid>
		</item>
		<item>
			<title><![CDATA[Shaders and Mouse Clicking]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=543#p543</link>
			<description><![CDATA[<p>Hello,</p><p>I have been messing around with Maratis for a while and I&#039;ve seen different kinds of shaders. Now, the problem is, I have no idea how I can add these to my game. Also, I don&#039;t know where to get them.</p><p>Is there any possible way to find what object the mouse is clicking?</p><p>Many thanks,</p><p>Iguana</p>]]></description>
			<author><![CDATA[dummy@example.com (Iguana)]]></author>
			<pubDate>Tue, 30 Aug 2011 18:25:56 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=543#p543</guid>
		</item>
	</channel>
</rss>
