aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/doc/html/example004.html
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/doc/html/example004.html')
-rw-r--r--src/others/irrlicht-1.8.1/doc/html/example004.html302
1 files changed, 302 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/doc/html/example004.html b/src/others/irrlicht-1.8.1/doc/html/example004.html
new file mode 100644
index 0000000..03ee1ea
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/doc/html/example004.html
@@ -0,0 +1,302 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<title>Irrlicht 3D Engine: Tutorial 4: Movement</title>
6
7<link href="tabs.css" rel="stylesheet" type="text/css"/>
8<link href="doxygen.css" rel="stylesheet" type="text/css" />
9<link href="navtree.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="resize.js"></script>
12<script type="text/javascript" src="navtree.js"></script>
13<script type="text/javascript">
14 $(document).ready(initResizable);
15</script>
16<link href="search/search.css" rel="stylesheet" type="text/css"/>
17<script type="text/javascript" src="search/search.js"></script>
18<script type="text/javascript">
19 $(document).ready(function() { searchBox.OnSelectItem(0); });
20</script>
21
22</head>
23<body>
24<div id="top"><!-- do not remove this div! -->
25
26
27<div id="titlearea">
28<table cellspacing="0" cellpadding="0">
29 <tbody>
30 <tr style="height: 56px;">
31
32 <td id="projectlogo"><img alt="Logo" src="irrlichtlogo.png"/></td>
33
34
35 <td style="padding-left: 0.5em;">
36 <div id="projectname">Irrlicht 3D Engine
37
38 </div>
39
40 </td>
41
42
43
44
45 <td> <div id="MSearchBox" class="MSearchBoxInactive">
46 <span class="left">
47 <img id="MSearchSelect" src="search/mag_sel.png"
48 onmouseover="return searchBox.OnSearchSelectShow()"
49 onmouseout="return searchBox.OnSearchSelectHide()"
50 alt=""/>
51 <input type="text" id="MSearchField" value="Search" accesskey="S"
52 onfocus="searchBox.OnSearchFieldFocus(true)"
53 onblur="searchBox.OnSearchFieldFocus(false)"
54 onkeyup="searchBox.OnSearchFieldChange(event)"/>
55 </span><span class="right">
56 <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
57 </span>
58 </div>
59</td>
60
61
62 </tr>
63 </tbody>
64</table>
65</div>
66
67<!-- Generated by Doxygen 1.7.5.1 -->
68<script type="text/javascript">
69var searchBox = new SearchBox("searchBox", "search",false,'Search');
70</script>
71<script type="text/javascript" src="dynsections.js"></script>
72</div>
73<div id="side-nav" class="ui-resizable side-nav-resizable">
74 <div id="nav-tree">
75 <div id="nav-tree-contents">
76 </div>
77 </div>
78 <div id="splitbar" style="-moz-user-select:none;"
79 class="ui-resizable-handle">
80 </div>
81</div>
82<script type="text/javascript">
83 initNavTree('example004.html','');
84</script>
85<div id="doc-content">
86<div class="header">
87 <div class="headertitle">
88<div class="title">Tutorial 4: Movement </div> </div>
89</div>
90<div class="contents">
91<div class="textblock"><div class="image">
92<img src="004shot.jpg" alt="004shot.jpg"/>
93</div>
94 <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>
95<p>Example 19.MouseAndJoystick shows how to handle those kinds of input.</p>
96<p>As always, I include the header files, use the irr namespace, and tell the linker to link with the .lib file. </p>
97<div class="fragment"><pre class="fragment"><span class="preprocessor">#ifdef _MSC_VER</span>
98<span class="preprocessor"></span><span class="comment">// We&#39;ll also define this to stop MSVC complaining about sprintf().</span>
99<span class="preprocessor">#define _CRT_SECURE_NO_WARNINGS</span>
100<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, &quot;Irrlicht.lib&quot;)</span>
101<span class="preprocessor"></span><span class="preprocessor">#endif</span>
102<span class="preprocessor"></span>
103<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>
104<span class="preprocessor">#include &quot;<a class="code" href="driver_choice_8h.html">driverChoice.h</a>&quot;</span>
105
106<span class="keyword">using namespace </span>irr;
107</pre></div><p>To receive events like mouse and keyboard input, or GUI events like "the OK
108button 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#a571f744ceffc3b4fe8a81f529163eb97" 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. </p>
109<div class="fragment"><pre class="fragment"><span class="keyword">class </span>MyEventReceiver : <span class="keyword">public</span> IEventReceiver
110{
111<span class="keyword">public</span>:
112 <span class="comment">// This is the one method that we have to implement</span>
113 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> OnEvent(<span class="keyword">const</span> SEvent&amp; event)
114 {
115 <span class="comment">// Remember whether each key is down or up</span>
116 <span class="keywordflow">if</span> (event.EventType == <a class="code" href="namespaceirr.html#ac9eed96e06e85ce3c86fcbbbe9e48a0ca6f90390f3147a1693e5e2e3422d6ca09" title="A key input event.">irr::EET_KEY_INPUT_EVENT</a>)
117 KeyIsDown[<span class="keyword">event</span>.KeyInput.Key] = <span class="keyword">event</span>.KeyInput.PressedDown;
118
119 <span class="keywordflow">return</span> <span class="keyword">false</span>;
120 }
121
122 <span class="comment">// This is used to check whether a key is being held down</span>
123 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> IsKeyDown(<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3">EKEY_CODE</a> keyCode)<span class="keyword"> const</span>
124<span class="keyword"> </span>{
125 <span class="keywordflow">return</span> KeyIsDown[keyCode];
126 }
127
128 MyEventReceiver()
129 {
130 <span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#a0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> i=0; i&lt;<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3a205b48c0c4ed7489ab8980657343283e">KEY_KEY_CODES_COUNT</a>; ++i)
131 KeyIsDown[i] = <span class="keyword">false</span>;
132 }
133
134<span class="keyword">private</span>:
135 <span class="comment">// We use this array to store the current state of each key</span>
136 <span class="keywordtype">bool</span> KeyIsDown[<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3a205b48c0c4ed7489ab8980657343283e">KEY_KEY_CODES_COUNT</a>];
137};
138</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. </p>
139<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
140{
141 <span class="comment">// ask user for driver</span>
142 <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType=driverChoiceConsole();
143 <span class="keywordflow">if</span> (driverType==<a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0ae685cada50f8c100403134d932d0414c" title="No driver, just for counting the elements.">video::EDT_COUNT</a>)
144 <span class="keywordflow">return</span> 1;
145
146 <span class="comment">// create device</span>
147 MyEventReceiver receiver;
148
149 IrrlichtDevice* device = <a class="code" href="namespaceirr.html#abaf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.">createDevice</a>(driverType,
150 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);
151
152 <span class="keywordflow">if</span> (device == 0)
153 <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>
154
155 video::IVideoDriver* driver = device-&gt;getVideoDriver();
156 scene::ISceneManager* smgr = device-&gt;getSceneManager();
157</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). </p>
158<div class="fragment"><pre class="fragment"> scene::ISceneNode * node = smgr-&gt;addSphereSceneNode();
159 <span class="keywordflow">if</span> (node)
160 {
161 node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,30));
162 node-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/wall.bmp&quot;</span>));
163 node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
164 }
165</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. </p>
166<div class="fragment"><pre class="fragment"> scene::ISceneNode* n = smgr-&gt;addCubeSceneNode();
167
168 <span class="keywordflow">if</span> (n)
169 {
170 n-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/t351sml.jpg&quot;</span>));
171 n-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
172 scene::ISceneNodeAnimator* anim =
173 smgr-&gt;createFlyCircleAnimator(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,30), 20.0f);
174 <span class="keywordflow">if</span> (anim)
175 {
176 n-&gt;addAnimator(anim);
177 anim-&gt;drop();
178 }
179 }
180</pre></div><p>The last scene node we add to show possibilities of scene node animators is a b3d model, which uses a 'fly straight' animator to run between to points. </p>
181<div class="fragment"><pre class="fragment"> scene::IAnimatedMeshSceneNode* anms =
182 smgr-&gt;addAnimatedMeshSceneNode(smgr-&gt;getMesh(<span class="stringliteral">&quot;../../media/ninja.b3d&quot;</span>));
183
184 <span class="keywordflow">if</span> (anms)
185 {
186 scene::ISceneNodeAnimator* anim =
187 smgr-&gt;createFlyStraightAnimator(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(100,0,60),
188 <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-100,0,60), 3500, <span class="keyword">true</span>);
189 <span class="keywordflow">if</span> (anim)
190 {
191 anms-&gt;addAnimator(anim);
192 anim-&gt;drop();
193 }
194</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... </p>
195<div class="fragment"><pre class="fragment"> anms-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
196
197 anms-&gt;setFrameLoop(0, 13);
198 anms-&gt;setAnimationSpeed(15);
199<span class="comment">// anms-&gt;setMD2Animation(scene::EMAT_RUN);</span>
200
201 anms-&gt;setScale(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(2.f,2.f,2.f));
202 anms-&gt;setRotation(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,-90,0));
203<span class="comment">// anms-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/sydney.bmp&quot;));</span>
204
205 }
206</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. </p>
207<div class="fragment"><pre class="fragment"> smgr-&gt;addCameraSceneNodeFPS();
208 device-&gt;getCursorControl()-&gt;setVisible(<span class="keyword">false</span>);
209</pre></div><p>Add a colorful irrlicht logo </p>
210<div class="fragment"><pre class="fragment"> device-&gt;getGUIEnvironment()-&gt;addImage(
211 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlichtlogoalpha2.tga&quot;</span>),
212 core::position2d&lt;s32&gt;(10,20));
213
214 gui::IGUIStaticText* diagnostics = device-&gt;getGUIEnvironment()-&gt;addStaticText(
215 L<span class="stringliteral">&quot;&quot;</span>, core::rect&lt;s32&gt;(10, 10, 400, 20));
216 diagnostics-&gt;setOverrideColor(video::SColor(255, 255, 255, 0));
217</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. </p>
218<div class="fragment"><pre class="fragment"> <span class="keywordtype">int</span> lastFPS = -1;
219
220 <span class="comment">// In order to do framerate independent movement, we have to know</span>
221 <span class="comment">// how long it was since the last frame</span>
222 <a class="code" href="namespaceirr.html#a0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> then = device-&gt;getTimer()-&gt;getTime();
223
224 <span class="comment">// This is the movemen speed in units per second.</span>
225 <span class="keyword">const</span> <a class="code" href="namespaceirr.html#a0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> MOVEMENT_SPEED = 5.f;
226
227 <span class="keywordflow">while</span>(device-&gt;run())
228 {
229 <span class="comment">// Work out a frame delta time.</span>
230 <span class="keyword">const</span> <a class="code" href="namespaceirr.html#a0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> now = device-&gt;getTimer()-&gt;getTime();
231 <span class="keyword">const</span> <a class="code" href="namespaceirr.html#a0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> frameDeltaTime = (<a class="code" href="namespaceirr.html#a0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a>)(now - then) / 1000.f; <span class="comment">// Time in seconds</span>
232 then = now;
233</pre></div><p> Check if keys W, S, A or D are being held down, and move the sphere node around respectively. </p>
234<div class="fragment"><pre class="fragment"> <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a> nodePosition = node-&gt;getPosition();
235
236 <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3ae559e3169016a3180c45c2828f391af2">irr::KEY_KEY_W</a>))
237 nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
238 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3ae52bafc112fc6c52f6b49cea42fa246e">irr::KEY_KEY_S</a>))
239 nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
240
241 <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3a2fe10b4309013968b9cd14811c3d3c85">irr::KEY_KEY_A</a>))
242 nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
243 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(receiver.IsKeyDown(<a class="code" href="namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3ad20e7e220103e611752b90edeb6cbc9d">irr::KEY_KEY_D</a>))
244 nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
245
246 node-&gt;setPosition(nodePosition);
247
248 driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, video::SColor(255,113,113,133));
249
250 smgr-&gt;drawAll(); <span class="comment">// draw the 3d scene</span>
251 device-&gt;getGUIEnvironment()-&gt;drawAll(); <span class="comment">// draw the gui environment (the logo)</span>
252
253 driver-&gt;endScene();
254
255 <span class="keywordtype">int</span> fps = driver-&gt;getFPS();
256
257 <span class="keywordflow">if</span> (lastFPS != fps)
258 {
259 <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">core::stringw</a> tmp(L<span class="stringliteral">&quot;Movement Example - Irrlicht Engine [&quot;</span>);
260 tmp += driver-&gt;getName();
261 tmp += L<span class="stringliteral">&quot;] fps: &quot;</span>;
262 tmp += fps;
263
264 device-&gt;setWindowCaption(tmp.c_str());
265 lastFPS = fps;
266 }
267 }
268</pre></div><p>In the end, delete the Irrlicht device. </p>
269<div class="fragment"><pre class="fragment"> device-&gt;drop();
270
271 <span class="keywordflow">return</span> 0;
272}
273</pre></div><p>That's it. Compile and play around with the program. </p>
274</div></div>
275</div>
276 <div id="nav-path" class="navpath">
277 <ul>
278<!-- window showing the filter options -->
279<div id="MSearchSelectWindow"
280 onmouseover="return searchBox.OnSearchSelectShow()"
281 onmouseout="return searchBox.OnSearchSelectHide()"
282 onkeydown="return searchBox.OnSearchSelectKey(event)">
283<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>
284
285<!-- iframe showing the search results (closed by default) -->
286<div id="MSearchResultsWindow">
287<iframe src="javascript:void(0)" frameborder="0"
288 name="MSearchResults" id="MSearchResults">
289</iframe>
290</div>
291
292
293 <li class="footer">
294<a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht
295Engine</a> Documentation &copy; 2003-2012 by Nikolaus Gebhardt. Generated on Sun Nov 17 2013 20:18:41 for Irrlicht 3D Engine by
296<a href="http://www.doxygen.org/index.html" target="_blank">Doxygen</a> 1.7.5.1 </li>
297 </ul>
298 </div>
299
300
301</body>
302</html>