<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Maratis forum - Shaders and Mouse Clicking]]></title>
	<link rel="self" href="http://forum.maratis3d.com/extern.php?action=feed&amp;tid=143&amp;type=atom"/>
	<updated>2012-09-29T23:33:03Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.maratis3d.com/viewtopic.php?id=143</id>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2702#p2702"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Felis]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1126</uri>
			</author>
			<updated>2012-09-29T23:33:03Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2702#p2702</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2701#p2701"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Felis]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1126</uri>
			</author>
			<updated>2012-09-29T22:17:49Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2701#p2701</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2700#p2700"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Felis]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1126</uri>
			</author>
			<updated>2012-09-29T20:44:44Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2700#p2700</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2699#p2699"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[anael]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=2</uri>
			</author>
			<updated>2012-09-29T16:05:06Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2699#p2699</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2698#p2698"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Felis]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1126</uri>
			</author>
			<updated>2012-09-29T14:13:00Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2698#p2698</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2697#p2697"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[anael]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=2</uri>
			</author>
			<updated>2012-09-29T08:37:42Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2697#p2697</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=2694#p2694"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Felis]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1126</uri>
			</author>
			<updated>2012-09-29T00:50:34Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=2694#p2694</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Shaders and Mouse Clicking]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=543#p543"/>
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Iguana]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=55</uri>
			</author>
			<updated>2011-08-30T18:25:56Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=543#p543</id>
		</entry>
</feed>
