# Measuring closest distance

• February 11th, 09:14 PM
Measuring closest distance
Hi.

Need some help with a small 3d-project of mine. I'm trying to find out a way
to determine the shortest distance between two models in the 3d-world. I
cannot use the worldpositions, because that would give me the distance between
the centerpoints of the objects.

I've tried to figure out a way to compare distances between all the vertices
of the objects, but it seems that you can't get the world position of vertices?

Any ideas would be appreciated!

• February 12th, 12:58 AM
hondo3000
Re: Measuring closest distance
depending on how your models look like this way could work:

send a ray from modelA to modelB, store the #isectposition. Send a ray from
modelB to modelA, store again the #isectposition.

Now you would have two points that can be used to determine the distance. If
you use directormx2004 you can tell the ray the models that should be checked.
In this example the ray from that is send from modelA should only check for
modelB and vice versa. In this way other models can be between those two models.

• February 12th, 10:08 AM
duckets
Re: Measuring closest distance
I've tried to figure out a way to compare distances between all the vertices
of the objects, but it seems that you can't get the world position of
vertices?[/q]

As well as the raycasting technique that hondo3000 suggested, you can in fact
get the world position of the vertices by following this process:

Create a temporary 'group' object.
Make the group object a child of your model.
Set the group's transform position to the vertex position.
(The group object will now be at the same position as the vertex)
use getWorldTransform() to get the world transform of the group object. This
is the vertex world position.

hope this helps!

- Ben

• February 13th, 12:23 AM
openspark
Re: Measuring closest distance
If your models have a large number of vertices, then calculating the distance
between each vertex of one model to each vertex of the other will require a
high number of calculations. For example: if both models have 32 vertices, you
will need 1024 calculations If they both have 64 vertices, you will need 4096
calculations.

Are your models concave? Do they have a regular form? Do you need to know
the shortest separation precisely, or would an accurate estimate be good
enough? Do you need to know which points are the closest, or is the distance
that separates them enough? Is speed of calculation an important issue? Can
you give us more details of the context in which this calculation takes place?

Depending on your answers to these questions, there may be ways to create a
custom solution which would work faster than a generic brute-force solution.

• February 13th, 01:36 PM
Re: Measuring closest distance
Thank you very much Ben, your solution did the trick.

The project I'm working on is a small "room-planner". The mesuring function
discussed here is a way for the user to get information about the distance to
another object. The function calculates the shortest distance, and then it
creates a "line" between the two points.

The solution posted by Ben did everything I wanted at that point. Though,
later on, I've recived suggestions to reCalculate the distance on each frame,
as the user moves the objects around. It is, of course, a pretty heavy
operation for the CPU to calculate these, for example, 1024 distances every
1/30 second. I've tested it and it works, but the framerate decreases way to
much, as one could guess.

Therefore, openspark, I'm very interested in what ideas you might have and I