Getting to know the Irrlicht 3D engine – Collision Detection

Google+ Pinterest LinkedIn Tumblr +



Collision detection and response is a vital part of pretty much any 3D game. It stops your character walking through walls or falling through the ground. Surprisingly, you will find more than one 3D engine requires you to build your own collision detection system. You may get a few collision or intersection tests for simple shapes like spheres, axis aligned boxes and line, but it is up to you to actually test for collisions, and react accordingly.

Thankfully Irrlicht does include collision detection for most of the common situations you would expect to find in a 3D game. In this tutorial we will build off the terrain tutorial and add collision detection that stops the camera from moving through the terrain mesh.

ApplicationManager.h / ApplicationManager.cpp

With the terrain created, using the code from the terrain tutorial, we then create a terrain triangle selector.

Triangle selectors provide a way for an object, like a terrain mesh, a 3D model or a static level, to report the triangles that exist in a specific area. Triangle selectors are used extensively in collision detection.

We then set the triangle selector we have created as the triangle selector for the terrain by calling the ISceneNode setTriangleSelector funcion. This might seem a little redundant, seeing as how we just created the selector from the very same terrain object we now supplying the selector back to. This is done because while the triangle selector we have created is one that is optimized for working with terrain meshes, Irrlicht does give you the opportunity to create your own custom triangle selector if you need to. In that case your own custom triangle selector, and not one created by the terrain object itself, would be supplied to the setTriangleSelector function.

At this point the terrain has a way to detect which triangles may be involved in a collision, but this alone won’t stop the camera from moving through it. For that we create a Collision Response Animator. This is done by calling the ISceneManager createCollisionResponseAnimator function, supplying both the triangle selector and the camera.

By default, the createCollisionResponseAnimator function will create an ellipsoid around the camera, which means that camera will collide with the terrain as if it occupied roughly the same space as a humanoid character.

We then assign the animator to the camera by calling the ISceneNode addAnimator function.

Finally, both the animator and the selector are dropped.

Now as you the camera you will see that it follows the terrain, even dropping down over cliffs (the createCollisionResponseAnimator function will add gravity to the animator by default).

This process of creating a triangle selector and then creating an animator to respond to collisions if the same for indoor worlds as well.



About Author

Leave A Reply