2

Having two 3D cubes/cuboids, each defined by their different vertices (p1, p2, p3, p4, p5, p6, p7, p8), how can I find the minimum distance between the cubes?

I have been working on a simple Euclidean 3D distance algorithm with the min and max of each cube but it doesn't work for rotated cubes. Is there any overall method for this problem? In this picture there are two examples of the two cubes and possible positions where I want to calculate the minimum distance:

img

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 1
    You probably can use this post : https://math.stackexchange.com/questions/2133217/minimal-distance-to-a-cube-in-2d-and-3d-from-a-point-lying-outside – Wind56 May 17 '21 at 22:31
  • see [`line closest(convex_mesh m0,convex_mesh m1)`](https://stackoverflow.com/a/62257945/2521214) – Spektre May 18 '21 at 07:06

1 Answers1

0

I don't see a shortcut, so I'll answer based on brute-force case exhaustion. This won't be the fastest approach but a very robust one.

You're looking for the shortest connecting line from a point in cube A to a point in cube B. At both ends, this shortest line will surely not end inside the cube, but on a surface, edge or vertex of the cube (assuming the cubes don't overlap). So you get 9 cases:

  • vertex / vertex
  • vertex / edge
  • vertex / surface
  • edge / vertex
  • edge / edge
  • edge / surface
  • surface / vertex
  • surface / edge
  • surface / surface

We can omit the surface / surface case, as it's only possible with surfaces parallel to each other, and in that situation some other case, using an edge or vertex, will give the same distance.

For each of the cases, you can find online a formula for computing the distance of the (infinitely extended) geometric objects, and then you should check for staying inside the finite surfaces or edges.

Finally, you can return the minimum of the valid distances.

Of course, there can be lots of optimizations, excluding cases that cannot produce shortest distances, e.g. surfaces pointing away from the other cube, but that needs careful analysis.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
  • Unfortunately calculating the edges and surfaces equations, computing the distances between everything and then checking the limits is to costly in computing times, I have optimized to exclude the surfaces/vertex/lines opposed to each other, but still it takes too much time (I need to compute this for a near real-time application) – Liliana Antão May 19 '21 at 15:03