Skip to content
EdgarReynaldo edited this page Nov 4, 2018 · 1 revision

Axis Aligned Bounding Box collision detection

Non moving bounding boxes

Assume you have a struct that stores lx,rx,ty,by,w,h. Call it struct BBox. The letters stand for left x, right x, top y, bottom y, width, and height.

Then a straight forward collision detection routine between two non moving rectangular bounding boxes is as follows :

bool Collide(BBox* b1 , BBox* b2) {
   return !( (b1->ty > b2->by) || (b1->by < b2->ty) || (b1->lx > b2->rx) || (b1->rx < b2->lx));

Of course we don't really want to know if they're colliding right now, we want to know if they're colliding in the future. That's easy. We expand our collision detection to check against future positions.

bool BBCollide(BBox* b1 , BBox* b2 , double dt) {
   BBox bOne = *b1;
   BBox bTwo = *b2;
   return Collide(&bOne , &bTwo);

While this detects collisions, it doesn't detect the exact time that the collision actually happened. For that you have to use intercept times and relative positions and velocities.

There are two ways to prevent missed collisions. One is sweep tests, and the other is intercept times. Sweep tests will give you an interval of time within a collision may have occurred, while an intercept test will tell you exactly when they collided.

Sweep tests

TODO : Write me

Intercept times

TODO : Write me