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.

  • \$\begingroup\$ 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? \$\endgroup\$ Jul 5, 2012 at 22:39
  • \$\begingroup\$ In "ray", mean "radius". \$\endgroup\$ Jul 5, 2012 at 22:58

1 Answer 1


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).

  • \$\begingroup\$ 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. \$\endgroup\$ Jul 5, 2012 at 22:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .