main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /** Example 007 Collision
  2. We will describe 2 methods: Automatic collision detection for moving through
  3. 3d worlds with stair climbing and sliding, and manual scene node and triangle
  4. picking using a ray. In this case, we will use a ray coming out from the
  5. camera, but you can use any ray.
  6. To start, we take the program from tutorial 2, which loads and displays a
  7. quake 3 level. We will use the level to walk in it and to pick triangles from.
  8. In addition we'll place 3 animated models into it for triangle picking. The
  9. following code starts up the engine and loads the level, as per tutorial 2.
  10. */
  11. #include <irrlicht.h>
  12. #include "driverChoice.h"
  13. #include "exampleHelper.h"
  14. using namespace irr;
  15. #ifdef _MSC_VER
  16. #pragma comment(lib, "Irrlicht.lib")
  17. #endif
  18. enum
  19. {
  20. // I use this ISceneNode ID to indicate a scene node that is
  21. // not pickable by getSceneNodeAndCollisionPointFromRay()
  22. ID_IsNotPickable = 0,
  23. // I use this flag in ISceneNode IDs to indicate that the
  24. // scene node can be picked by ray selection.
  25. IDFlag_IsPickable = 1 << 0,
  26. // I use this flag in ISceneNode IDs to indicate that the
  27. // scene node can be highlighted. In this example, the
  28. // homonids can be highlighted, but the level mesh can't.
  29. IDFlag_IsHighlightable = 1 << 1
  30. };
  31. int main()
  32. {
  33. // ask user for driver
  34. video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  35. if (driverType==video::EDT_COUNT)
  36. return 1;
  37. // create device
  38. IrrlichtDevice *device =
  39. createDevice(driverType, core::dimension2d<u32>(640, 480), 16, false);
  40. if (device == 0)
  41. return 1; // could not create selected driver.
  42. /*
  43. If we want to receive information about the material of a hit triangle we have to get
  44. collisions per meshbuffer. The only disadvantage of this is that getting them per
  45. meshbuffer can be a little bit slower than per mesh, but usually that's not noticeable.
  46. If you set this to false you will no longer get material names in the title bar.
  47. */
  48. const bool separateMeshBuffers = true;
  49. video::IVideoDriver* driver = device->getVideoDriver();
  50. scene::ISceneManager* smgr = device->getSceneManager();
  51. const io::path mediaPath = getExampleMediaPath();
  52. device->getFileSystem()->addFileArchive(mediaPath + "map-20kdm2.pk3");
  53. scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
  54. scene::IMeshSceneNode* q3node = 0;
  55. // The Quake mesh is pickable, but doesn't get highlighted.
  56. if (q3levelmesh)
  57. q3node = smgr->addOctreeSceneNode(q3levelmesh->getMesh(0), 0, IDFlag_IsPickable);
  58. /*
  59. So far so good, we've loaded the quake 3 level like in tutorial 2. Now,
  60. here comes something different: We create a triangle selector. A
  61. triangle selector is a class which can fetch the triangles from scene
  62. nodes for doing different things with them, for example collision
  63. detection. There are different triangle selectors, and all can be
  64. created with the ISceneManager. In this example, we create an
  65. OctreeTriangleSelector, which optimizes the triangle output a little
  66. bit by reducing it like an octree. This is very useful for huge meshes
  67. like quake 3 levels. After we created the triangle selector, we attach
  68. it to the q3node. This is not necessary, but in this way, we do not
  69. need to care for the selector, for example dropping it after we do not
  70. need it anymore.
  71. */
  72. scene::ITriangleSelector* selector = 0;
  73. if (q3node)
  74. {
  75. q3node->setPosition(core::vector3df(-1350,-130,-1400));
  76. /*
  77. There is currently no way to split an octree by material.
  78. So if we need material infos we have to create one octree per
  79. meshbuffer and put them together in a MetaTriangleSelector.
  80. */
  81. if ( separateMeshBuffers && q3node->getMesh()->getMeshBufferCount() > 1)
  82. {
  83. scene::IMetaTriangleSelector * metaSelector = smgr->createMetaTriangleSelector();
  84. for ( irr::u32 m=0; m < q3node->getMesh()->getMeshBufferCount(); ++m )
  85. {
  86. scene::ITriangleSelector*
  87. bufferSelector = smgr->createOctreeTriangleSelector(
  88. q3node->getMesh()->getMeshBuffer(m), m, q3node);
  89. if ( bufferSelector )
  90. {
  91. metaSelector->addTriangleSelector( bufferSelector );
  92. bufferSelector->drop();
  93. }
  94. }
  95. selector = metaSelector;
  96. }
  97. else
  98. {
  99. // If you don't need material infos just create one octree for the
  100. // whole mesh.
  101. selector = smgr->createOctreeTriangleSelector(
  102. q3node->getMesh(), q3node, 128);
  103. }
  104. q3node->setTriangleSelector(selector);
  105. // We're not done with this selector yet, so don't drop it.
  106. }
  107. /*
  108. We add a first person shooter camera to the scene so that we can see and
  109. move in the quake 3 level like in tutorial 2. But this, time, we add a
  110. special animator to the camera: A collision response animator. This
  111. animator modifies the scene node to which it is attached in order to
  112. prevent it from moving through walls and to add gravity to the node. The
  113. only things we have to tell the animator is how the world looks like,
  114. how big the scene node is, how much gravity to apply and so on. After the
  115. collision response animator is attached to the camera, we do not have to do
  116. anything else for collision detection, it's all done automatically.
  117. The rest of the collision detection code below is for picking. And please
  118. note another cool feature: The collision response animator can be
  119. attached also to all other scene nodes, not only to cameras. And it can
  120. be mixed with other scene node animators. In this way, collision
  121. detection and response in the Irrlicht engine is really easy.
  122. Now we'll take a closer look on the parameters of
  123. createCollisionResponseAnimator(). The first parameter is the
  124. TriangleSelector, which specifies how the world, against which collision
  125. detection is done, looks like. The second parameter is the scene node,
  126. which is the object which is affected by collision detection - in our
  127. case it is the camera. The third defines how big the object is, it is
  128. the radius of an ellipsoid. Try it out and change the radius to smaller
  129. values, the camera will be able to move closer to walls after this. The
  130. next parameter is the direction and speed of gravity. We'll set it to
  131. (0, -1000, 0), which approximates realistic gravity (depends on the units
  132. which are used in the scene model). You could set it to (0,0,0) to disable
  133. gravity. And the last value is just an offset: Without it the ellipsoid with
  134. which collision detection is done would be around the camera and the camera
  135. would be in the middle of the ellipsoid. But as human beings, we are used to
  136. have our eyes on top of the body, not in the middle of it. So we place the
  137. scene node 50 units over the center of the ellipsoid with this parameter.
  138. And that's it, collision detection works now.
  139. */
  140. // Set a jump speed of 300 units per second, which gives a fairly realistic jump
  141. // when used with the gravity of (0, -1000, 0) in the collision response animator.
  142. scene::ICameraSceneNode* camera =
  143. smgr->addCameraSceneNodeFPS(0, 100.0f, .3f, ID_IsNotPickable, 0, 0, true, 300.f);
  144. camera->setPosition(core::vector3df(50,50,-60));
  145. camera->setTarget(core::vector3df(-70,30,-60));
  146. if (selector)
  147. {
  148. scene::ISceneNodeAnimatorCollisionResponse * anim = smgr->createCollisionResponseAnimator(
  149. selector, camera, core::vector3df(30,50,30),
  150. core::vector3df(0,-1000,0), core::vector3df(0,30,0));
  151. selector->drop(); // As soon as we're done with the selector, drop it.
  152. camera->addAnimator(anim);
  153. anim->drop(); // And likewise, drop the animator when we're done referring to it.
  154. }
  155. // Now I create three animated characters which we can pick, a dynamic light for
  156. // lighting them, and a billboard for drawing where we found an intersection.
  157. // First, let's get rid of the mouse cursor. We'll use a billboard to show
  158. // what we're looking at.
  159. device->getCursorControl()->setVisible(false);
  160. // Add the billboard.
  161. scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
  162. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
  163. bill->setMaterialTexture(0, driver->getTexture(mediaPath + "particle.bmp"));
  164. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  165. bill->setMaterialFlag(video::EMF_ZBUFFER, false);
  166. bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));
  167. bill->setID(ID_IsNotPickable); // This ensures that we don't accidentally ray-pick it
  168. /* Add 3 animated hominids, which we can pick using a ray-triangle intersection.
  169. They all animate quite slowly, to make it easier to see that accurate triangle
  170. selection is being performed. */
  171. scene::IAnimatedMeshSceneNode* node = 0;
  172. video::SMaterial material;
  173. // Add an MD2 node, which uses vertex-based animation.
  174. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "faerie.md2"),
  175. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  176. node->setPosition(core::vector3df(-90,-15,-140)); // Put its feet on the floor.
  177. node->setScale(core::vector3df(1.6f)); // Make it appear realistically scaled
  178. node->setMD2Animation(scene::EMAT_POINT);
  179. node->setAnimationSpeed(20.f);
  180. material.setTexture(0, driver->getTexture(mediaPath + "faerie2.bmp"));
  181. material.Lighting = true;
  182. material.NormalizeNormals = true;
  183. node->getMaterial(0) = material;
  184. // Now create a triangle selector for it. The selector will know that it
  185. // is associated with an animated node, and will update itself as necessary.
  186. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  187. node->setTriangleSelector(selector);
  188. selector->drop(); // We're done with this selector, so drop it now.
  189. // And this B3D file uses skinned skeletal animation.
  190. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "ninja.b3d"),
  191. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  192. node->setScale(core::vector3df(10));
  193. node->setPosition(core::vector3df(-75,-66,-80));
  194. node->setRotation(core::vector3df(0,90,0));
  195. node->setAnimationSpeed(8.f);
  196. node->getMaterial(0).NormalizeNormals = true;
  197. node->getMaterial(0).Lighting = true;
  198. // Just do the same as we did above.
  199. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  200. node->setTriangleSelector(selector);
  201. selector->drop();
  202. // This X files uses skeletal animation, but without skinning.
  203. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "dwarf.x"),
  204. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  205. node->setPosition(core::vector3df(-70,-66,-30)); // Put its feet on the floor.
  206. node->setRotation(core::vector3df(0,-90,0)); // And turn it towards the camera.
  207. node->setAnimationSpeed(20.f);
  208. node->getMaterial(0).Lighting = true;
  209. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  210. node->setTriangleSelector(selector);
  211. selector->drop();
  212. // And this mdl file uses skinned skeletal animation.
  213. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "yodan.mdl"),
  214. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  215. node->setPosition(core::vector3df(-90,-25,20));
  216. node->setScale(core::vector3df(0.8f));
  217. node->getMaterial(0).Lighting = true;
  218. node->setAnimationSpeed(20.f);
  219. // Just do the same as we did above.
  220. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  221. node->setTriangleSelector(selector);
  222. selector->drop();
  223. material.setTexture(0, 0);
  224. material.Lighting = false;
  225. // Add a light, so that the unselected nodes aren't completely dark.
  226. scene::ILightSceneNode * light = smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
  227. video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
  228. light->setID(ID_IsNotPickable); // Make it an invalid target for selection.
  229. // Remember which scene node is highlighted
  230. scene::ISceneNode* highlightedSceneNode = 0;
  231. scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
  232. // draw the selection triangle only as wireframe
  233. material.Wireframe=true;
  234. while(device->run())
  235. if (device->isWindowActive())
  236. {
  237. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0));
  238. smgr->drawAll();
  239. // Unlight any currently highlighted scene node
  240. if (highlightedSceneNode)
  241. {
  242. highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);
  243. highlightedSceneNode = 0;
  244. }
  245. // All intersections in this example are done with a ray cast out from the camera to
  246. // a distance of 1000. You can easily modify this to check (e.g.) a bullet
  247. // trajectory or a sword's position, or create a ray from a mouse click position using
  248. // ISceneCollisionManager::getRayFromScreenCoordinates()
  249. core::line3d<f32> ray;
  250. ray.start = camera->getPosition();
  251. ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.0f;
  252. // This call is all you need to perform ray/triangle collision on every scene node
  253. // that has a triangle selector, including the Quake level mesh. It finds the nearest
  254. // collision point/triangle, and returns the scene node containing that point.
  255. // Irrlicht provides other types of selection, including ray/triangle selector,
  256. // ray/box and ellipse/triangle selector, plus associated helpers.
  257. // You might also want to check the other methods of ISceneCollisionManager.
  258. irr::io::SNamedPath hitTextureName;
  259. scene::SCollisionHit hitResult;
  260. scene::ISceneNode * selectedSceneNode =collMan->getSceneNodeAndCollisionPointFromRay(
  261. hitResult, // Returns all kind of info about the collision
  262. ray,
  263. IDFlag_IsPickable, // This ensures that only nodes that we have
  264. // set up to be pickable are considered
  265. 0); // Check the entire scene (this is actually the implicit default)
  266. // If the ray hit anything, move the billboard to the collision position
  267. // and draw the triangle that was hit.
  268. if(selectedSceneNode)
  269. {
  270. bill->setPosition(hitResult.Intersection); // Show the current intersection point with the level or a mesh
  271. // We need to reset the transform before doing our own rendering.
  272. driver->setTransform(video::ETS_WORLD, core::matrix4());
  273. driver->setMaterial(material);
  274. driver->draw3DTriangle(hitResult.Triangle, video::SColor(0,255,0,0)); // Show which triangle has been hit
  275. // We can check the flags for the scene node that was hit to see if it should be
  276. // highlighted. The animated nodes can be highlighted, but not the Quake level mesh
  277. if((selectedSceneNode->getID() & IDFlag_IsHighlightable) == IDFlag_IsHighlightable)
  278. {
  279. highlightedSceneNode = selectedSceneNode;
  280. // Highlighting in this case means turning lighting OFF for this node,
  281. // which means that it will be drawn with full brightness.
  282. highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);
  283. }
  284. // When separateMeshBuffers is set to true we can now find out which material was hit
  285. if ( hitResult.MeshBuffer && hitResult.Node && hitResult.Node->getMaterial(hitResult.MaterialIndex).TextureLayer[0].Texture )
  286. {
  287. // Note we are interested in the node material and not in the meshbuffer material.
  288. // Otherwise we wouldn't get the fairy2 texture which is only set on the node.
  289. hitTextureName = hitResult.Node->getMaterial(hitResult.MaterialIndex).TextureLayer[0].Texture->getName();
  290. }
  291. }
  292. // We're all done drawing, so end the scene.
  293. driver->endScene();
  294. // Show some info in title-bar
  295. int fps = driver->getFPS();
  296. static core::stringw lastString;
  297. core::stringw str = L"Collision detection example - Irrlicht Engine [";
  298. str += driver->getName();
  299. str += "] FPS:";
  300. str += fps;
  301. if ( !hitTextureName.getInternalName().empty() )
  302. {
  303. str += " ";
  304. irr::io::path texName(hitTextureName.getInternalName());
  305. str += core::deletePathFromFilename(texName);
  306. }
  307. if ( str != lastString ) // changing caption is somewhat expensive, so don't when nothing changed
  308. {
  309. device->setWindowCaption(str.c_str());
  310. lastString = str;
  311. }
  312. }
  313. device->drop();
  314. return 0;
  315. }
  316. /*
  317. **/