Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-release > by-pkgid > 58828b263d8f56d90ac336dea07a4586 > files > 735

irrlicht-doc-1.6.1-1mdv2010.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Irrlicht Engine: Tutorial 4: Movement</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<table class="irrlicht" >
  <tr valign="middle"> 
    <td><font size="2"><a class="qindex" href="index.html"><font color="#FFFFFF">Home</font></a> 
      | <a class="qindex" href="namespaces.html"><font color="#FFFFFF">Namespaces</font></a> 
      | <a class="qindex" href="hierarchy.html"><font color="#FFFFFF">Hierarchy</font></a> 
      | <a class="qindex" href="classes.html"><font color="#FFFFFF">Alphabetical 
      List</font></a> | <a class="qindex" href="annotated.html"><font color="#FFFFFF"> 
      Class list</font></a> | <a class="qindex" href="files.html"><font color="#FFFFFF">Files</font></a> 
      | <a class="qindex" href="namespacemembers.html"><font color="#FFFFFF"> 
      Namespace&nbsp;Members</font></a> | <a class="qindex" href="functions.html"><font color="#FFFFFF">Class 
      members</font></a> | <a class="qindex" href="globals.html"><font color="#FFFFFF">File 
      members</font></a> | <a class="qindex" href="pages.html"><font color="#FFFFFF">Tutorials</font></a></font> </td>
  </tr>
</table>
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<h1><a class="anchor" name="example004">Tutorial 4: Movement </a></h1><div align="center">
<img src="004shot.jpg" alt="004shot.jpg">
</div>
 <p>
This Tutorial shows how to move and animate SceneNodes. The basic concept of SceneNodeAnimators is shown as well as manual movement of nodes using the keyboard. We'll demonstrate framerate independent movement, which means moving by an amount dependent on the duration of the last run of the Irrlicht loop.<p>
Example 19.MouseAndJoystick shows how to handle those kinds of input.<p>
As always, I include the header files, use the <a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> namespace, and tell the linker to link with the .lib file. <div class="fragment"><pre class="fragment"><span class="preprocessor">#ifdef _MSC_VER</span>
<span class="preprocessor"></span><span class="comment">// We'll also define this to stop MSVC complaining about sprintf().</span>
<span class="preprocessor">#define _CRT_SECURE_NO_WARNINGS</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span>
<span class="preprocessor"></span><span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>

<span class="keyword">using namespace </span>irr;
</pre></div><p>
To receive events like mouse and keyboard input, or GUI events like "the OK button has been clicked", we need an object which is derived from the <a class="el" href="classirr_1_1_i_event_receiver.html" title="Interface of an object which can receive events.">irr::IEventReceiver</a> object. There is only one method to override: <a class="el" href="classirr_1_1_i_event_receiver.html#571f744ceffc3b4fe8a81f529163eb97" title="Called if an event happened.">irr::IEventReceiver::OnEvent()</a>. This method will be called by the engine once when an event happens. What we really want to know is whether a key is being held down, and so we will remember the current state of each key. <div class="fragment"><pre class="fragment"><span class="keyword">class </span>MyEventReceiver : <span class="keyword">public</span> IEventReceiver
{
<span class="keyword">public</span>:
        <span class="comment">// This is the one method that we have to implement</span>
        <span class="keyword">virtual</span> <span class="keywordtype">bool</span> OnEvent(<span class="keyword">const</span> SEvent&amp; event)
        {
                <span class="comment">// Remember whether each key is down or up</span>
                <span class="keywordflow">if</span> (event.EventType == <a class="code" href="namespaceirr.html#c9eed96e06e85ce3c86fcbbbe9e48a0c6f90390f3147a1693e5e2e3422d6ca09" title="A key input event.">irr::EET_KEY_INPUT_EVENT</a>)
                        KeyIsDown[<span class="keyword">event</span>.KeyInput.Key] = <span class="keyword">event</span>.KeyInput.PressedDown;

                <span class="keywordflow">return</span> <span class="keyword">false</span>;
        }

        <span class="comment">// This is used to check whether a key is being held down</span>
        <span class="keyword">virtual</span> <span class="keywordtype">bool</span> IsKeyDown(<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb3">EKEY_CODE</a> keyCode)<span class="keyword"> const</span>
<span class="keyword">        </span>{
                <span class="keywordflow">return</span> KeyIsDown[keyCode];
        }
        
        MyEventReceiver()
        {
                <span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> i=0; i&lt;<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb3205b48c0c4ed7489ab8980657343283e">KEY_KEY_CODES_COUNT</a>; ++i)
                        KeyIsDown[i] = <span class="keyword">false</span>;
        }

<span class="keyword">private</span>:
        <span class="comment">// We use this array to store the current state of each key</span>
        <span class="keywordtype">bool</span> KeyIsDown[<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb3205b48c0c4ed7489ab8980657343283e">KEY_KEY_CODES_COUNT</a>];
};
</pre></div><p>
The event receiver for keeping the pressed keys is ready, the actual responses will be made inside the render loop, right before drawing the scene. So lets just create an <a class="el" href="classirr_1_1_irrlicht_device.html" title="The Irrlicht device. You can create it with createDevice() or createDeviceEx().">irr::IrrlichtDevice</a> and the scene node we want to move. We also create some other additional scene nodes, to show that there are also some different possibilities to move and animate scene nodes. <div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
{
        <span class="comment">// let user select driver type</span>

        <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d04691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">video::EDT_DIRECT3D9</a>;

        printf(<span class="stringliteral">"Please select the driver you want for this example:\n"</span>\
                <span class="stringliteral">" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"</span>\
                <span class="stringliteral">" (d) Software Renderer\n (e) Burning's Software Renderer\n"</span>\
                <span class="stringliteral">" (f) NullDevice\n (otherKey) exit\n\n"</span>);

        <span class="keywordtype">char</span> i;
        std::cin &gt;&gt; i;

        <span class="keywordflow">switch</span>(i)
        {
                <span class="keywordflow">case</span> <span class="charliteral">'a'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d04691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">video::EDT_DIRECT3D9</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'b'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d08cc3807f6f28404f3424ad7e31b3142f" title="Direct3D8 device, only available on Win32 platforms.">video::EDT_DIRECT3D8</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'c'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d02715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">video::EDT_OPENGL</a>;   <span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'d'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d01598cd235a1a6bd052e2011b559e8995" title="The Irrlicht Engine Software renderer.">video::EDT_SOFTWARE</a>; <span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'e'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0e85481da26159b967191ccc6de1e4a05" title="The Burning&amp;#39;s Software Renderer, an alternative software renderer.">video::EDT_BURNINGSVIDEO</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'f'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0cfdbd476cbfd4d05e72f9adffcc42210" title="Null driver, useful for applications to run the engine without visualisation.">video::EDT_NULL</a>;     <span class="keywordflow">break</span>;
                <span class="keywordflow">default</span>: <span class="keywordflow">return</span> 0;
        }       

        <span class="comment">// create device</span>
        MyEventReceiver receiver;

        IrrlichtDevice* device = <a class="code" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice</a>(driverType,
                        core::dimension2d&lt;u32&gt;(640, 480), 16, <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, &amp;receiver);

        <span class="keywordflow">if</span> (device == 0)
                <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>

        video::IVideoDriver* driver = device-&gt;getVideoDriver();
        scene::ISceneManager* smgr = device-&gt;getSceneManager();
</pre></div><p>
Create the node which will be moved with the WSAD keys. We create a sphere node, which is a built-in geometry primitive. We place the node at (0,0,30) and assign a texture to it to let it look a little bit more interesting. Because we have no dynamic lights in this scene we disable lighting for each model (otherwise the models would be black). <div class="fragment"><pre class="fragment">        scene::ISceneNode * node = smgr-&gt;addSphereSceneNode();
        <span class="keywordflow">if</span> (node)
        {
                node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,30));
                node-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">"../../media/wall.bmp"</span>));
                node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#8a3bc00ae8137535b9fbc5f40add70d3cea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
        }
</pre></div><p>
Now we create another node, movable using a scene node animator. Scene node animators modify scene nodes and can be attached to any scene node like mesh scene nodes, billboards, lights and even camera scene nodes. Scene node animators are not only able to modify the position of a scene node, they can also animate the textures of an object for example. We create a cube scene node and attach a 'fly circle' scene node animator to it, letting this node fly around our sphere scene node. <div class="fragment"><pre class="fragment">        scene::ISceneNode* n = smgr-&gt;addCubeSceneNode();

        <span class="keywordflow">if</span> (n)
        {
                n-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">"../../media/t351sml.jpg"</span>));
                n-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#8a3bc00ae8137535b9fbc5f40add70d3cea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
                scene::ISceneNodeAnimator* anim =
                        smgr-&gt;createFlyCircleAnimator(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,30), 20.0f);
                <span class="keywordflow">if</span> (anim)
                {
                        n-&gt;addAnimator(anim);
                        anim-&gt;drop();
                }
        }
</pre></div><p>
The last scene node we add to show possibilities of scene node animators is a md2 model, which uses a 'fly straight' animator to run between to points. <div class="fragment"><pre class="fragment">        scene::IAnimatedMeshSceneNode* anms =
                smgr-&gt;addAnimatedMeshSceneNode(smgr-&gt;getMesh(<span class="stringliteral">"../../media/ninja.b3d"</span>));

        <span class="keywordflow">if</span> (anms)
        {
                scene::ISceneNodeAnimator* anim =
                        smgr-&gt;createFlyStraightAnimator(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(100,0,60),
                        <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-100,0,60), 3500, <span class="keyword">true</span>);
                <span class="keywordflow">if</span> (anim)
                {
                        anms-&gt;addAnimator(anim);
                        anim-&gt;drop();
                }
</pre></div><p>
To make the model look right we disable lighting, set the frames between which the animation should loop, rotate the model around 180 degrees, and adjust the animation speed and the texture. To set the right animation (frames and speed), we would also be able to just call "anms-&gt;setMD2Animation(scene::EMAT_RUN)" for the 'run' animation instead of "setFrameLoop" and "setAnimationSpeed", but this only works with MD2 animations, and so you know how to start other animations. But a good advice is to not use hardcoded frame-numbers... <div class="fragment"><pre class="fragment">                anms-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#8a3bc00ae8137535b9fbc5f40add70d3cea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);

                anms-&gt;setFrameLoop(0, 13);
                anms-&gt;setAnimationSpeed(15);
<span class="comment">//              anms-&gt;setMD2Animation(scene::EMAT_RUN);</span>

                anms-&gt;setScale(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(2.f,2.f,2.f));
                anms-&gt;setRotation(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,-90,0));
<span class="comment">//              anms-&gt;setMaterialTexture(0, driver-&gt;getTexture("../../media/sydney.bmp"));</span>

        }
</pre></div><p>
To be able to look at and move around in this scene, we create a first person shooter style camera and make the mouse cursor invisible. <div class="fragment"><pre class="fragment">        smgr-&gt;addCameraSceneNodeFPS();
        device-&gt;getCursorControl()-&gt;setVisible(<span class="keyword">false</span>);
</pre></div><p>
Add a colorful irrlicht logo <div class="fragment"><pre class="fragment">        device-&gt;getGUIEnvironment()-&gt;addImage(
                driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlichtlogoalpha2.tga"</span>),
                core::position2d&lt;s32&gt;(10,20));

        gui::IGUIStaticText* diagnostics = device-&gt;getGUIEnvironment()-&gt;addStaticText(
                L<span class="stringliteral">""</span>, core::rect&lt;s32&gt;(10, 10, 400, 20));
        diagnostics-&gt;setOverrideColor(video::SColor(255, 255, 255, 0));
</pre></div><p>
We have done everything, so lets draw it. We also write the current frames per second and the name of the driver to the caption of the window. <div class="fragment"><pre class="fragment">        <span class="keywordtype">int</span> lastFPS = -1;

        <span class="comment">// In order to do framerate independent movement, we have to know</span>
        <span class="comment">// how long it was since the last frame</span>
        <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> then = device-&gt;getTimer()-&gt;getTime();

        <span class="comment">// This is the movemen speed in units per second.</span>
        <span class="keyword">const</span> <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> MOVEMENT_SPEED = 5.f;

        <span class="keywordflow">while</span>(device-&gt;run())
        {
                <span class="comment">// Work out a frame delta time.</span>
                <span class="keyword">const</span> <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> now = device-&gt;getTimer()-&gt;getTime();
                <span class="keyword">const</span> <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> frameDeltaTime = (<a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a>)(now - then) / 1000.f; <span class="comment">// Time in seconds</span>
                then = now;
</pre></div> Check if keys W, S, A or D are being held down, and move the sphere node around respectively. <div class="fragment"><pre class="fragment">                <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a> nodePosition = node-&gt;getPosition();

                <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb3e559e3169016a3180c45c2828f391af2">irr::KEY_KEY_W</a>))
                        nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
                <span class="keywordflow">else</span> <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb3e52bafc112fc6c52f6b49cea42fa246e">irr::KEY_KEY_S</a>))
                        nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;

                <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb32fe10b4309013968b9cd14811c3d3c85">irr::KEY_KEY_A</a>))
                        nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
                <span class="keywordflow">else</span> <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#54da2a0e231901735e3da1b0edf72eb3d20e7e220103e611752b90edeb6cbc9d">irr::KEY_KEY_D</a>))
                        nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;

                node-&gt;setPosition(nodePosition);

                driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, video::SColor(255,113,113,133));

                smgr-&gt;drawAll(); <span class="comment">// draw the 3d scene</span>
                device-&gt;getGUIEnvironment()-&gt;drawAll(); <span class="comment">// draw the gui environment (the logo)</span>

                driver-&gt;endScene();

                <span class="keywordtype">int</span> fps = driver-&gt;getFPS();

                <span class="keywordflow">if</span> (lastFPS != fps)
                {
                        <a class="code" href="namespaceirr_1_1core.html#ef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">core::stringw</a> tmp(L<span class="stringliteral">"Movement Example - Irrlicht Engine ["</span>);
                        tmp += driver-&gt;getName();
                        tmp += L<span class="stringliteral">"] fps: "</span>;
                        tmp += fps;

                        device-&gt;setWindowCaption(tmp.c_str());
                        lastFPS = fps;
                }
        }
</pre></div><p>
In the end, delete the Irrlicht device. <div class="fragment"><pre class="fragment">        device-&gt;drop();
        
        <span class="keywordflow">return</span> 0;
}
</pre></div><p>
That's it. Compile and play around with the program. </div>
<hr size="1">
<address style="align: right;">
<small> </small>
</address>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr> 
    <td width="0"> <div align="left"><small><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="irrlicht.png" alt="The Irrlicht Engine" align="middle" border=0 width=88 height=31></a></small></div></td>
    <td> <div align="left"><small><em><font size="2">The <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht 
        Engine</a> Documentation &copy; 2003-2009 by Nikolaus Gebhardt. Generated 
        on Sun Jan 10 09:24:06 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> 
        (1.5.6)</font></em></small></div></td>
  </tr>
</table>
<address style="align: right;">
</address>
</body>
</html>