Sunday 10 June 2012

Axis Aligned Bounding Boxes and Collisions

(Warning: this article may confuse and disorient you if you are not mathematically inclined)
If you've ever tried to make a physics based game or even a simple platformer, then you've no doubt encountered axis aligned bounding boxes (aabb). An aabb is as the name implies a box that is permanently aligned to all axis. (Wikipedia gives a rather lacking description of an Aabb)
Yay pretty diagrams
aabbs are so convenient for video games and macro physics because a) they can be defined by just two points (max and min) and b) because you can do some super fast collision detection on them.
2D aabb defined by two points
Simply put the algorithm for checking collisions against two aabbs (we'll call them C and D) is:

if Cmax is greater than Dmin AND Cmin is less than Dmax then they are colliding.


Sounds simple enough. I should also note that when I write "is greater (or less) than" I mean for all vector components (each or its coordinates).
Amazing as it may sound to an outsider this simple technique can also be used in 3D. The aabb's are still just defined by two points and you can still make the same checks for collision.

In fact, you can even use the same technique in 1D. You can think of an infinite line and then pick out two line segments (C and D) each line segment will have a point further to the left (its min) and a point further to the right (its max).
Now for the awesome part, aabbs and aabb collision checks also work in 4D. In fact they can work in 5D, 6D and 10582D as well! But uh, trying to visualise two tesseracts coming into contact with one another...
colliding ---------------------------- not colliding?

Indeed the maths works out fine but it is all a bit ambiguous to look at. So the maths is easy. Now I just need a way to convey this information to a general audience.