Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm really newbie on programming and I'm making some tests. I couldn't find nowhere on Internet how could I calculate the distance between two 3D objects' faces. Is there anyway?

Detailing, as an example, I have two 3D cubes. Each one has a vector3 position designating it's center on the 3D space and an orientation matrix. And each cube has a size (float width, float height and float length).

I could get a simple distance between them by calling Vector3.Distance(), but it doesn't consider its sizes, just the position. Then the distance would be between its centers.

Is there any way to calculate the distance between the faces? Thanks for any reply.

share|improve this question
    
I'm now trying to use a sphere created with ray equal to object's biggest dimension, for each object. And I would just have to remove the ray from the distance calculated from Vector3.Distance(). But seems to not work on large objects since spheres may get wrongly intersected. And maybe not the entire object may get inside the sphere. What would be the best way to calculate the distance? –  Arthur Gibraltar Jul 5 '12 at 22:39
    
In "ray", mean "radius". –  Arthur Gibraltar Jul 5 '12 at 22:58
add comment

1 Answer

up vote 3 down vote accepted

There are a variety of approaches you could take for this, depending on how fast and how accurate you need it to be, and how much time you're willing to spend on it.

If a very approximate answer will suffice, you could store a "radius" for each object and calculate the distance between two objects as the distance between their centers, minus both of their radius values.

A somewhat more accurate way would be to apply a separating axis test. You could use the vector between the centers as a trial axis for a faster version of this algorithm, or go all the way and try all the possible axes called for by the theorem, taking the minimum separation across all the trial axes.

A very accurate and fast way would be to use the GJK algorithm, but it is complicated and difficult to implement (though you can probably find some code for it around the Web).

share|improve this answer
    
Thank you, I'm gonna search and study a little about that to see if that fits. I'm still learning, but thank you, I had no idea where should I start from. –  Arthur Gibraltar Jul 5 '12 at 22:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.