Chapter 10. Collision detection

Collision detection is one of the most important parts of the game. Essentially, any game is a set of rules for the behavior/interaction of objects in the event of a collision. For example, a shell and a tank: when they meet, the shell disappears, the tank explodes, and so on.

So how can you tell whether two objects are colliding or not? The first thing that comes to mind is to measure the center-to-center distance between them. If it is less than the sum of the radii of the objects, then this is a collision. A good solution for billiard balls, but not for other shapes. Come to think of it, maybe check bounding boxes overlap? This may work, but like the first approach, it is very approximate. For example:

As in this example, the bounding boxes overlap, but the objects do not. A more reasonable solution (in my opinion) is to measure the distance between the longest axises of the objects.

If less than the sum of objects' radii , then collision for certain.

Implementation - on  GitHub link: https://github.com/bkantemir/_wg_410

In following demo cars are placed randomly and many of them are overlapping. Program checks if they collide and pulls them apart:

How it works:

  1. When loading model ModelLoader::loadModelStandard(), in the end, it calls function buildGabaritesFromDrawJobs() and stores initial dimensions in SceneSubj::gabaritesOnLoad
  2. In the main cycle, in TheApp::drawFrame() for each object it calls Gabarites::fillGabarites(), which calculates longest axis position, size and orientation.
  3. And finally, we call SceneSubj::checkCollisions(), which compares them, and in case of collision calls corresponding reaction.

Everything is pretty straight forward, though implementation sometimes doesn't look simple, particularly distance calculation between line segments.


Leave a Reply

Your email address will not be published. Required fields are marked *