Axis Aligned Bounding Boxes (AABBs for short) are very easy to make. They are basically non-rotated boxes.
They can be made by having a max point and a min point or by having a center point and width/height/depth. For this example, I will use an AABB created with two points (min and max).
The collision check between two AABBs is very simple and fast, since it has many early outs.
The bad thing about AABBs is that they can’t be rotated. Once they are rotated, they stop being AABBs since they are not aligned to the X,Y and Z axes anymore. For objects that rotate, a better option would be to use spheres, capsules or even OBBs (oriented bounding boxes).
To check if two AABBs are colliding, we just need to check that the first AABB’s max point is greater than the second one’s min point and that the first one’s min point is less than the second one’s max point.
Of course, the order of which AABB is the first and which one is the second doesn’t matter.
Since this check contains a lot of “&&” comparisons, if the first check is false, it won’t even bother checking the rest, so there’s a big possibility of early outs and optimization.
Here’s some sample C++ code using the DirectX Vector class.
bool AABBtoAABB(const TAABB& tBox1, const TAABB& tBox2)
//Check if Box1's max is greater than Box2's min and Box1's min is less than Box2's max
return(tBox1.m_vecMax.x > tBox2.m_vecMin.x &&
tBox1.m_vecMin.x < tBox2.m_vecMax.x &&
tBox1.m_vecMax.y > tBox2.m_vecMin.y &&
tBox1.m_vecMin.y < tBox2.m_vecMax.y &&
tBox1.m_vecMax.z > tBox2.m_vecMin.z &&
tBox1.m_vecMin.z < tBox2.m_vecMax.z);
//If not, it will return false