IrrlichtWrapper Library

Collision


Introduction

A very important feature of almost every 3D application is knowing when objects collide with other objects in the virtual world, these collisions are used for many things like: -
  • Preventing a camera moving through walls and objects.
  • Entering special areas such as an elevator.
  • Knowing when you can pick up an object.
  • Activating a virtual control.
  • Detecting the point of impact of a projectile.
  • Accurately simulating falling objects.
Although Irrlicht is a 3D Graphics Engine rather than a Physics Engine its developers have wisely included a wide range of ways that you can test for colliding objects. And if that isn't enough you can always include a Physics Engine into your project.

Irrlicht performs its collision through an object known as a triangle selector, this is represented in the Irrlicht Wrapper with the irr_selector type. A triangle selector is essentially a simplified version of your model, it contains only the geometry either constructed from the surface of your model or from the bounding box of your model (a simple cube that defines the volume your model occupies). These triangle selectors can be joined together into groups to construct a simple representation of the surfaces in your scene that you wish to collide against.

In this paper we are going to examine methods for solving each of the problems we have listed above.

World Segmentation

Before collision is examined, it is essential to address segmentation of the world. In a large and complex environment there may be many hundreds even thousands of objects that you can collide with however only a few of these may actually be in your locallity, testing against every object in your scene will be wasteful and can reduce the performance of your game. This is where segmentation of your environment becomes vital.

In this very simple diagram the avatar (encircled) has a number of 'medical' objects that they can collect and two structures that they can collide with. This world has been divided into 15 areas and clearly the avatars 'area of influence' only overlaps four of those areas. When testing for the collection of medical objects there is only one object that needs to be checked. When preventing the camera moving through walls in the building neither of the potentially large structures needs to be checked potentially saving thousands of calculations.

Preventing a camera moving through walls and objects

The easiest way to stop your camera moving though objects in the world is with Irrlichts built in collision system. This tests for collision between a virtual sphere around your node and all of the polygons that you place into the collision system. It enables you to move the sphere around, even up and down steps and ladders.



The surfaces in a collision are stored in a special object called an IRR_SELECTOR this contains a simplified version of a model in your scene that is used purely for testing collisions.

You can create them simply by supplying the mesh and your node to a single call:

DIM as IRR_SELECTOR mycollision = IrrGetCollisionGroupFromMesh( roomMesh, roomNode )

Or when you are dealing with a complex mesh like a BSP game level you should use the call:

DIM as IRR_SELECTOR mycollision = IrrGetCollisionGroupFromComplexMesh( levelMesh, levelNode )

Once you have your collision object you need something that will test for the collisions, for this you can use the special collision animator. This tests for collisions between the sphere and your collision objects and is applied with a single call:

IrrAddCollisionAnimator( mycollision, myCamera, _
                         sizeX, sizeY, sizeZ, _
                         gravityX, gravityY, gravityZ, _
                         offsetX, offsetY, offsetZ )

If you make the size of the sphere too small you will find it difficult to walk up steps, if you make the size of the sphere too large you might get stuck in doorways. Experiment to find a good size for your scene.

Gravity can pull in any direction but it will probably be a negative value applied to the Y component.

The offset can be used to offset the position of the object from the center of the virtual sphere, so you can raise a camera to appear to be at eye level for example.

The animator can be applied to any object to provide collision animation with other objects in the scene, controlled vehicles for example. However you should remember that this is purely for simple collisions it is not a physics simulator.

Entering special areas

Often in many games and applications you will want to enter special areas: elevators, spring boards or simply areas that trigger other actions.
You can test for these conditions with a simple call to IrrIsPointInsideNode. As long as your nodes model encompasses the whole of the area you want to test against.

If you wish to test against a virtual or invisible area you can simply create a node perhaps even a box shaped test node and make it invisible by making a call to IrrSetNodeVisibility.

While your character is inside the elevator you might alter the position of your avatar with the same forces that you apply to the elevator.

Picking up objects

Picking up objects within a world is a simple task, you can simply judge if the distance between your character and the object, if the distance is short enough the object can be automatically collected or collected with a key press.

The function IrrGetDistanceBetweenNodes can be used to determine the distances between your camera and a collectable item for example.

Activating a virtual control: Triggers

Often within a game you will want to press a button, open a door or activate an object.. Usually this will be achieved by clicking on it on the screen or adjusting the camera so a crosshair is on the object before a button is pressed.


Detecting the impact of a projectile

Many games need to detect the impact of a projectile against other objects or the surface of a map, whether that is the strike of gunshot, a paint ball or a thrown object. Thanks to 'The Car' we now have a method that we can use to extract all of this important information in a single call.


Accurately simulating objects

Acurately simulating collisions and the motions of objects in those collisions is actually beyond the what both Irrlicht and the Wrapper were intended to do, however thanks to 'Siskinedge' we now have example integration and a useable interface for working with physics libraries like Newton and the Open Dynamics Engine.