I have two bounding boxes defined by the following values:

m1.min.x;//the min x value of model 1
m1.min.y;// "   "  y   "    "   "   "
m1.min.z;// "   "  z   "    "   "   "
m1.max.x;// "  max x   "    "   "   "
m1.max.y;// "   "  y   "    "   "   "
m1.max.z;// "   "  z   "    "   "   "
m2.min.x;// "  min x   "    "   "   2
m2.min.y;// "   "  y   "    "   "   "
m2.min.z;// "   "  z   "    "   "   "
m2.max.x;// "  max x   "    "   "   "
m2.max.y;// "   "  y   "    "   "   "
m2.max.z;// "   "  z   "    "   "   "

I know that the form is gonna look something like this:

if (a&&
    b&&
    c&&
    //...
    )
{
    //there is a collision
}

I did this in 2d without a problem at all, but now I am in 3d and I am completely out of my element. Any point in the right direction would be helpful.

That is my problem, I have no idea where to start, I just can't figure out how to approach it. So far my idea has been to check the four corners and see if any of the corners is within the other box. I also thought about maybe checking for collision across two dimensions at once... I am just lost in the dealing with the third dimension part.

Does this work... it seems far too simple:

 if ((m1.min.x>m2.max.x)||(m2.min.x>m1.max.x)||
    (m1.min.y>m2.max.y)||(m2.min.y>m1.max.y)||
    (m1.min.z>m2.max.z)||(m2.min.z>m1.max.z))
        return true;//there was a collision
 else
        return false;//there was no collision

Edited 4 Years Ago by Labdabeta

I don't think that would work, try this:

bool x_in_range = (m1.min.x <= m2.max.x) && (m1.max.x >= m2.min.x);
bool y_in_range = (m1.min.y <= m2.max.y) && (m1.max.y >= m2.min.y);
bool z_in_range = (m1.min.z <= m2.max.z) && (m1.max.z >= m2.min.z);
if( x_in_range && y_in_range && z_in_range )
  return true;
else
  return false;

I did some boolean logic and came up with what seems like a faster solution (and simpler):

bool x_in_range=(m1.min.x<=m2.max.x)&&(m1.max.x>=m2.min.x);
//now using good old boolean logic:
bool x_not_in_range=(m1.min.x>m2.max.x)||(m1.max.x<m2.min.x);//later to be rearranged to use > on both sides
//this can be done to all of the values leading to this:
if (x_not_in_range||y_not_in_range||z_not_in_range)
    return false
//since all not_in_range's are composed entirely of || and the final concatenation is done entirely by use of || a statement can be made entirely out of ||
if ((m1.min.x>m2.max.x)||(m2.min.x>m1.max.x)||
(m1.min.y>m2.max.y)||(m2.min.y>m1.max.y)||
(m1.min.z>m2.max.z)||(m2.min.z>m1.max.z))
    return false;//no collision
else
    return true;//collision

I just want a double-check of my work at this point, but it seems like my original solution works, only backwards :P

This article has been dead for over six months. Start a new discussion instead.