main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. // Add an MD2 node, which uses vertex-based animation.
  173. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "faerie.md2"),
  174. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  175. if ( node )
  176. {
  177. node->setPosition(core::vector3df(-90,-15,-140)); // Put its feet on the floor.
  178. node->setScale(core::vector3df(1.6f)); // Make it appear realistically scaled
  179. node->setMD2Animation(scene::EMAT_POINT);
  180. node->setAnimationSpeed(20.f);
  181. video::SMaterial& material = node->getMaterial(0);
  182. material.setTexture(0, driver->getTexture(mediaPath + "faerie2.bmp"));
  183. material.Lighting = true;
  184. material.NormalizeNormals = true;
  185. }
  186. // Now create a triangle selector for it. The selector will know that it
  187. // is associated with an animated node, and will update itself as necessary.
  188. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  189. node->setTriangleSelector(selector);
  190. selector->drop(); // We're done with this selector, so drop it now.
  191. // And this B3D file uses skinned skeletal animation.
  192. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "ninja.b3d"),
  193. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  194. if ( node )
  195. {
  196. node->setScale(core::vector3df(10));
  197. node->setPosition(core::vector3df(-75,-66,-80));
  198. node->setRotation(core::vector3df(0,90,0));
  199. node->setAnimationSpeed(8.f);
  200. node->getMaterial(0).NormalizeNormals = true;
  201. node->getMaterial(0).Lighting = true;
  202. // Just do the same as we did above.
  203. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  204. node->setTriangleSelector(selector);
  205. selector->drop();
  206. }
  207. // This X files uses skeletal animation, but without skinning.
  208. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "dwarf.x"),
  209. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  210. if ( node )
  211. {
  212. node->setPosition(core::vector3df(-70,-66,-30)); // Put its feet on the floor.
  213. node->setRotation(core::vector3df(0,-90,0)); // And turn it towards the camera.
  214. node->setAnimationSpeed(20.f);
  215. node->getMaterial(0).Lighting = true;
  216. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  217. node->setTriangleSelector(selector);
  218. selector->drop();
  219. }
  220. // And this mdl file uses skinned skeletal animation.
  221. node = smgr->addAnimatedMeshSceneNode(smgr->getMesh(mediaPath + "yodan.mdl"),
  222. 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
  223. if ( node )
  224. {
  225. node->setPosition(core::vector3df(-90,-25,20));
  226. node->setScale(core::vector3df(0.8f));
  227. node->getMaterial(0).Lighting = true;
  228. node->setAnimationSpeed(20.f);
  229. // Just do the same as we did above.
  230. selector = smgr->createTriangleSelector(node, separateMeshBuffers);
  231. node->setTriangleSelector(selector);
  232. selector->drop();
  233. }
  234. // Add a light, so that the unselected nodes aren't completely dark.
  235. scene::ILightSceneNode * light = smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
  236. video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
  237. light->setID(ID_IsNotPickable); // Make it an invalid target for selection.
  238. // Remember which scene node is highlighted
  239. scene::ISceneNode* highlightedSceneNode = 0;
  240. scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
  241. // draw the selection triangle only as wireframe
  242. irr::video::SMaterial materialWireframe;
  243. materialWireframe.Lighting = false;
  244. materialWireframe.Wireframe=true;
  245. while(device->run())
  246. if (device->isWindowActive())
  247. {
  248. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0));
  249. smgr->drawAll();
  250. // Unlight any currently highlighted scene node
  251. if (highlightedSceneNode)
  252. {
  253. highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);
  254. highlightedSceneNode = 0;
  255. }
  256. // All intersections in this example are done with a ray cast out from the camera to
  257. // a distance of 1000. You can easily modify this to check (e.g.) a bullet
  258. // trajectory or a sword's position, or create a ray from a mouse click position using
  259. // ISceneCollisionManager::getRayFromScreenCoordinates()
  260. core::line3d<f32> ray;
  261. ray.start = camera->getPosition();
  262. ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.0f;
  263. // This call is all you need to perform ray/triangle collision on every scene node
  264. // that has a triangle selector, including the Quake level mesh. It finds the nearest
  265. // collision point/triangle, and returns the scene node containing that point.
  266. // Irrlicht provides other types of selection, including ray/triangle selector,
  267. // ray/box and ellipse/triangle selector, plus associated helpers.
  268. // You might also want to check the other methods of ISceneCollisionManager.
  269. irr::io::SNamedPath hitTextureName;
  270. scene::SCollisionHit hitResult;
  271. scene::ISceneNode * selectedSceneNode =collMan->getSceneNodeAndCollisionPointFromRay(
  272. hitResult, // Returns all kind of info about the collision
  273. ray,
  274. IDFlag_IsPickable, // This ensures that only nodes that we have
  275. // set up to be pickable are considered
  276. 0); // Check the entire scene (this is actually the implicit default)
  277. // If the ray hit anything, move the billboard to the collision position
  278. // and draw the triangle that was hit.
  279. if(selectedSceneNode)
  280. {
  281. bill->setPosition(hitResult.Intersection); // Show the current intersection point with the level or a mesh
  282. // We need to reset the transform before doing our own rendering.
  283. driver->setTransform(video::ETS_WORLD, core::matrix4());
  284. driver->setMaterial(materialWireframe);
  285. driver->draw3DTriangle(hitResult.Triangle, video::SColor(0,255,0,0)); // Show which triangle has been hit
  286. // We can check the flags for the scene node that was hit to see if it should be
  287. // highlighted. The animated nodes can be highlighted, but not the Quake level mesh
  288. if((selectedSceneNode->getID() & IDFlag_IsHighlightable) == IDFlag_IsHighlightable)
  289. {
  290. highlightedSceneNode = selectedSceneNode;
  291. // Highlighting in this case means turning lighting OFF for this node,
  292. // which means that it will be drawn with full brightness.
  293. highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);
  294. }
  295. // When separateMeshBuffers is set to true we can now find out which material was hit
  296. if ( hitResult.MeshBuffer && hitResult.Node && hitResult.Node->getMaterial(hitResult.MaterialIndex).TextureLayer[0].Texture )
  297. {
  298. // Note we are interested in the node material and not in the meshbuffer material.
  299. // Otherwise we wouldn't get the fairy2 texture which is only set on the node.
  300. hitTextureName = hitResult.Node->getMaterial(hitResult.MaterialIndex).TextureLayer[0].Texture->getName();
  301. }
  302. }
  303. // We're all done drawing, so end the scene.
  304. driver->endScene();
  305. // Show some info in title-bar
  306. int fps = driver->getFPS();
  307. static core::stringw lastString;
  308. core::stringw str = L"Collision detection example - Irrlicht Engine [";
  309. str += driver->getName();
  310. str += "] FPS:";
  311. str += fps;
  312. if ( !hitTextureName.getInternalName().empty() )
  313. {
  314. str += " ";
  315. irr::io::path texName(hitTextureName.getInternalName());
  316. str += core::deletePathFromFilename(texName);
  317. }
  318. if ( str != lastString ) // changing caption is somewhat expensive, so don't when nothing changed
  319. {
  320. device->setWindowCaption(str.c_str());
  321. lastString = str;
  322. }
  323. }
  324. device->drop();
  325. return 0;
  326. }
  327. /*
  328. **/