Collision detection is the heartbeat of game logic. Essentially, any game is just a set of rules defining how objects behave when they interact. Think of a shell and a tank: when they meet, the shell is destroyed, the tank explodes, and the game state changes.
But how do we mathematically determine if two objects have actually met?
The Search for Precision
The first approach that usually comes to mind is measuring the center-to-center distance. if the distance is less than the sum of the objects’ radii, you have a collision. This is a perfect solution for billiard balls, but it fails for almost any other shape.
What about Bounding Box overlap? This is a standard industry shortcut, but it remains very approximate. Consider this scenario:

As the example shows, the boxes overlap, but the objects do not. So we need something better. My preferred solution is to measure the distance between the principal axes (the longest spans) of the objects. It’s called Axis-Aligned Bounding Boxes (AABB).

If the distance between these segments is less than the sum of the objects’ radii, we have a definitive collision.

Implementation & Logic
You can find the full source code on: https://github.com/bkantemir/_wg_410
In the demo below, cars are placed randomly, causing many to overlap. The program detects these collisions and physically “resolves” them by pulling the objects apart:
The Workflow:
- When loading model ModelLoader::loadModelStandard(), in the end, it calls function buildGabaritesFromDrawJobs() and stores initial dimensions in SceneSubj::gabaritesOnLoad
- In the main cycle, in TheApp::drawFrame() for each object it calls Gabarites::fillGabarites(), which calculates longest axis position, size and orientation.
- 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.