Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

Any quick way to calculate min distance between 2 bounding boxes?

This topic is 2994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any quick way to calculate the minimum distance between 2 bounding boxes? It is very easy to find an article describing how to find the distance between a point and a bounding box on the net.. But not for 2 bounding boxes... Any ideas? Thanks Jack

Share this post


Link to post
Share on other sites
Advertisement
Are they axis aligned? If so it's really similar to the point case.

Just project the situation onto each axis. Then find the distance between their projections, sum the squares of those distances, and take the square root of that.

Share this post


Link to post
Share on other sites
Quote:
Original post by quasar3d
Are they axis aligned? If so it's really similar to the point case.

Just project the situation onto each axis. Then find the distance between their projections, sum the squares of those distances, and take the square root of that.


For simplicity sake, you can assume they are axis aligned...
Do you know of any online/downloadable articles from the net that
I can take a look at (for your method)?
Thanks
Jack

Share this post


Link to post
Share on other sites
Don't know of any, but here's the code of my function:


float Box::GetDist(const Box &b) const
{
float sqrDist = 0;
for(int i = 0;i < 3;i++)
{
if(b.max < min)
{
float d = b.max - min;
sqrDist += d * d;
}
else if(b.min > max)
{
float d = b.min - max;
sqrDist += d * d;
}
}
return sqrtf(sqrDist);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by quasar3d
Don't know of any, but here's the code of my function:


float Box::GetDist(const Box &b) const
{
float sqrDist = 0;
for(int i = 0;i < 3;i++)
{
if(b.max < min)
{
float d = b.max - min;
sqrDist += d * d;
}
else if(b.min > max)
{
float d = b.min - max;
sqrDist += d * d;
}
}
return sqrtf(sqrDist);
}




Maybe I am missing the obvious, but will this not calculate the minimal distance between the corner points of the two boxes?

Share this post


Link to post
Share on other sites
something will be added to sqrDist only when the projections onto the current axis do not overlap.

This means that when the min distance is the distance between two edges, nothing will be added to sqrDist for the axis parallel to it, and so it will in effect be the distance between two corners of a 2D projection of the situation.

similarly, if the min distance is between two faces, sqrDist is only added to for one of the axes.

Share this post


Link to post
Share on other sites
Quote:
Original post by quasar3d
something will be added to sqrDist only when the projections onto the current axis do not overlap.

This means that when the min distance is the distance between two edges, nothing will be added to sqrDist for the axis parallel to it, and so it will in effect be the distance between two corners of a 2D projection of the situation.

similarly, if the min distance is between two faces, sqrDist is only added to for one of the axes.


I see, indeed I missed the obvious. Been a while since I wrote such functionality :)

Share this post


Link to post
Share on other sites
Quote:
Original post by quasar3d
Don't know of any, but here's the code of my function:


float Box::GetDist(const Box &b) const
{
float sqrDist = 0;
for(int i = 0;i < 3;i++)
{
if(b.max < min)
{
float d = b.max - min;
sqrDist += d * d;
}
else if(b.min > max)
{
float d = b.min - max;
sqrDist += d * d;
}
}
return sqrtf(sqrDist);
}

this can be also nicely vectorized using sse with just 3 instructions (if squared distance is ok, which is the case in a lot of situations):
sub
max
dot

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!