irrlicht/examples/07.Collision/tutorial.html

208 lines
28 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Tutorial 7: Collision</title>
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Wanted to avoid copying .css to each folder, so copied default .css from doxyen in here, kicked out most stuff we don't need for examples and modified some a little bit.
Target was having a single html in each example folder which is created from the main.cpp files and needs no files besides some images below media folder.
Feel free to improve :)
-->
<style>
body, table, div, p, dl {
font: 400 14px/22px;
}
body {
background-color: #F0F0F0;
color: black;
margin-left: 5%;
margin-right: 5%;
}
p.reference, p.definition {
font: 400 14px/22px;
}
.title {
font: 400 14px/28px;
font-size: 150%;
font-weight: bold;
margin: 10px 2px;
}
h1, h2, h3, h4, h5, h6 {
-webkit-transition: text-shadow 0.5s linear;
-moz-transition: text-shadow 0.5s linear;
-ms-transition: text-shadow 0.5s linear;
-o-transition: text-shadow 0.5s linear;
transition: text-shadow 0.5s linear;
margin-right: 15px;
}
caption {
font-weight: bold;
}
h3.version {
font-size: 90%;
text-align: center;
}
a {
color: #3D578C;
font-weight: normal;
text-decoration: none;
}
.contents a:visited {
color: #4665A2;
}
a:hover {
text-decoration: underline;
}
a.el {
font-weight: bold;
}
a.code, a.code:visited, a.line, a.line:visited {
color: #4665A2;
}
a.codeRef, a.codeRef:visited, a.lineRef, a.lineRef:visited {
color: #4665A2;
}
pre.fragment {
border: 1px solid #C4CFE5;
background-color: #FBFCFD;
padding: 4px 6px;
margin: 4px 8px 4px 2px;
overflow: auto;
word-wrap: break-word;
font-size: 9pt;
line-height: 125%;
font-family: monospace, fixed;
font-size: 105%;
}
div.fragment {
padding: 0px;
margin: 4px 8px 4px 2px;
background-color: #FBFCFD;
border: 1px solid #C4CFE5;
}
div.line {
font-family: monospace, fixed;
font-size: 13px;
min-height: 13px;
line-height: 1.0;
text-wrap: unrestricted;
white-space: -moz-pre-wrap; /* Moz */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: pre-wrap; /* CSS3 */
word-wrap: break-word; /* IE 5.5+ */
text-indent: -53px;
padding-left: 53px;
padding-bottom: 0px;
margin: 0px;
-webkit-transition-property: background-color, box-shadow;
-webkit-transition-duration: 0.5s;
-moz-transition-property: background-color, box-shadow;
-moz-transition-duration: 0.5s;
-ms-transition-property: background-color, box-shadow;
-ms-transition-duration: 0.5s;
-o-transition-property: background-color, box-shadow;
-o-transition-duration: 0.5s;
transition-property: background-color, box-shadow;
transition-duration: 0.5s;
}
div.contents {
margin-top: 10px;
margin-left: 12px;
margin-right: 8px;
}
div.center {
text-align: center;
margin-top: 0px;
margin-bottom: 0px;
padding: 0px;
}
div.center img {
border: 0px;
}
span.keyword {
color: #008000
}
span.keywordtype {
color: #604020
}
span.keywordflow {
color: #e08000
}
span.comment {
color: #800000
}
span.preprocessor {
color: #806020
}
span.stringliteral {
color: #002080
}
span.charliteral {
color: #008080
}
blockquote {
background-color: #F7F8FB;
border-left: 2px solid #9CAFD4;
margin: 0 24px 0 4px;
padding: 0 12px 0 16px;
}
hr {
height: 0px;
border: none;
border-top: 1px solid #4A6AAA;
}
address {
font-style: normal;
color: #2A3D61;
}
div.header {
background-image:url('nav_h.png');
background-repeat:repeat-x;
background-color: #F9FAFC;
margin: 0px;
border-bottom: 1px solid #C4CFE5;
}
div.headertitle {
padding: 5px 5px 5px 10px;
}
.image {
text-align: center;
}
.caption {
font-weight: bold;
}
div.zoom {
border: 1px solid #90A5CE;
}
tr.heading h2 {
margin-top: 12px;
margin-bottom: 4px;
}
</style>
</head>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<!--END TITLEAREA-->
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Tutorial 7: Collision </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="image">
<img src="../../media/007shot.jpg" alt="007shot.jpg"/>
</div>
<p>We will describe 2 methods: Automatic collision detection for moving through 3d worlds with stair climbing and sliding, and manual scene node and triangle picking using a ray. In this case, we will use a ray coming out from the camera, but you can use any ray.</p>
<p>To start, we take the program from tutorial 2, which loads and displays a quake 3 level. We will use the level to walk in it and to pick triangles from. In addition we'll place 3 animated models into it for triangle picking. The following code starts up the engine and loads the level, as per tutorial 2. </p><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;irrlicht.h&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;driverChoice.h&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>irr;</div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef _MSC_VER</span></div><div class="line"><span class="preprocessor">#pragma comment(lib, &quot;Irrlicht.lib&quot;)</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="keyword">enum</span></div><div class="line">{</div><div class="line"> <span class="comment">// I use this ISceneNode ID to indicate a scene node that is</span></div><div class="line"> <span class="comment">// not pickable by getSceneNodeAndCollisionPointFromRay()</span></div><div class="line"> ID_IsNotPickable = 0,</div><div class="line"></div><div class="line"> <span class="comment">// I use this flag in ISceneNode IDs to indicate that the</span></div><div class="line"> <span class="comment">// scene node can be picked by ray selection.</span></div><div class="line"> IDFlag_IsPickable = 1 &lt;&lt; 0,</div><div class="line"></div><div class="line"> <span class="comment">// I use this flag in ISceneNode IDs to indicate that the</span></div><div class="line"> <span class="comment">// scene node can be highlighted. In this example, the</span></div><div class="line"> <span class="comment">// homonids can be highlighted, but the level mesh can&#39;t.</span></div><div class="line"> IDFlag_IsHighlightable = 1 &lt;&lt; 1</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> <span class="comment">// ask user for driver</span></div><div class="line"> video::E_DRIVER_TYPE driverType=driverChoiceConsole();</div><div class="line"> <span class="keywordflow">if</span> (driverType==video::EDT_COUNT)</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"></div><div class="line"> <span class="comment">// create device</span></div><div class="line"></div><div class="line"> IrrlichtDevice *device =</div><div class="line"> createDevice(driverType, core::dimension2d&lt;u32&gt;(640, 480), 16, <span class="keyword">false</span>);</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (device == 0)</div><div class="line"> <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span></div><div class="line"></div><div class="line"> video::IVideoDriver* driver = device-&gt;getVideoDriver();</div><div class="line"> scene::ISceneManager* smgr = device-&gt;getSceneManager();</div><div class="line"></div><div class="line"> device-&gt;getFileSystem()-&gt;addFileArchive(<span class="stringliteral">&quot;../../media/map-20kdm2.pk3&quot;</span>);</div><div class="line"></div><div class="line"> scene::IAnimatedMesh* q3levelmesh = smgr-&gt;getMesh(<span class="stringliteral">&quot;20kdm2.bsp&quot;</span>);</div><div class="line"> scene::IMeshSceneNode* q3node = 0;</div><div class="line"></div><div class="line"> <span class="comment">// The Quake mesh is pickable, but doesn&#39;t get highlighted.</span></div><div class="line"> <span class="keywordflow">if</span> (q3levelmesh)</div><div class="line"> q3node = smgr-&gt;addOctreeSceneNode(q3levelmesh-&gt;getMesh(0), 0, IDFlag_IsPickable);</div></div><!-- fragment --><p> So far so good, we've loaded the quake 3 level like in tutorial 2. Now, here comes something different: We create a t
<p>Now we'll take a closer look on the parameters of createCollisionResponseAnimator(). The first parameter is the TriangleSelector, which specifies how the world, against collision detection is done looks like. The second parameter is the scene node, which is the object, which is affected by collision detection, in our case it is the camera. The third defines how big the object is, it is the radius of an ellipsoid. Try it out and change the radius to smaller values, the camera will be able to move closer to walls after this. The next parameter is the direction and speed of gravity. We'll set it to (0, -10, 0), which approximates to realistic gravity, assuming that our units are metres. You could set it to (0,0,0) to disable gravity. And the last value is just a translation: Without this, the ellipsoid with which collision detection is done would be around the camera, and the camera would be in the middle of the ellipsoid. But as human beings, we are used to have our eyes on top of the body, with which we collide with our world, not in the middle of it. So we place the scene node 50 units over the center of the ellipsoid with this parameter. And that's it, collision detection works now. </p><div class="fragment"><div class="line"><span class="comment">// Set a jump speed of 3 units per second, which gives a fairly realistic jump</span></div><div class="line"><span class="comment">// when used with the gravity of (0, -10, 0) in the collision response animator.</span></div><div class="line">scene::ICameraSceneNode* camera =</div><div class="line"> smgr-&gt;addCameraSceneNodeFPS(0, 100.0f, .3f, ID_IsNotPickable, 0, 0, <span class="keyword">true</span>, 3.f);</div><div class="line">camera-&gt;setPosition(core::vector3df(50,50,-60));</div><div class="line">camera-&gt;setTarget(core::vector3df(-70,30,-60));</div><div class="line"></div><div class="line"><span class="keywordflow">if</span> (selector)</div><div class="line">{</div><div class="line"> scene::ISceneNodeAnimator* anim = smgr-&gt;createCollisionResponseAnimator(</div><div class="line"> selector, camera, core::vector3df(30,50,30),</div><div class="line"> core::vector3df(0,-10,0), core::vector3df(0,30,0));</div><div class="line"> selector-&gt;drop(); <span class="comment">// As soon as we&#39;re done with the selector, drop it.</span></div><div class="line"> camera-&gt;addAnimator(anim);</div><div class="line"> anim-&gt;drop(); <span class="comment">// And likewise, drop the animator when we&#39;re done referring to it.</span></div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Now I create three animated characters which we can pick, a dynamic light for</span></div><div class="line"><span class="comment">// lighting them, and a billboard for drawing where we found an intersection.</span></div><div class="line"></div><div class="line"><span class="comment">// First, let&#39;s get rid of the mouse cursor. We&#39;ll use a billboard to show</span></div><div class="line"><span class="comment">// what we&#39;re looking at.</span></div><div class="line">device-&gt;getCursorControl()-&gt;setVisible(<span class="keyword">false</span>);</div><div class="line"></div><div class="line"><span class="comment">// Add the billboard.</span></div><div class="line">scene::IBillboardSceneNode * bill = smgr-&gt;addBillboardSceneNode();</div><div class="line">bill-&gt;setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );</div><div class="line">bill-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/particle.bmp&quot;</span>));</div><div class="line">bill-&gt;setMaterialFlag(video::EMF_LIGHTING, <span class="keyword">false</span>);</div><div class="line">bill-&gt;setMaterialFlag(video::EMF_ZBUFFER, <span class="keyword">false</span>);</div><div class="line">bill-&gt;setSize(core::dimension2d&lt;f32&gt;(20.0f, 20.0f));</div><div class="line">bill-&gt;setID(ID_IsNotPickable); <span class="comment">// This ensures that we don&#39;t accidentally ray-pick it</span></div></div><!--
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<p>&nbsp;</p>
</body>
</html>