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

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

## 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 on other sites
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 on other sites
Quote:
 Original post by quasar3dAre 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...
I can take a look at (for your method)?
Thanks
Jack

##### 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 on other sites
Quote:
 Original post by quasar3dDon'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 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 on other sites
Quote:
 Original post by quasar3dsomething 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 on other sites
Quote:
 Original post by quasar3dDon'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

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007532
• ### Who's Online (See full list)

There are no registered users currently online

×