4 Replies Latest reply on Feb 13, 2007 5:36 AM by magnus_andersson

# 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!
• ###### 1. 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.

• ###### 2. Re: Measuring closest distance
quote:

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?

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

• ###### 3. 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.
• ###### 4. 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 will try to answer your questions.

1. No convave models
2. The form changes from model to model
3. A good estimate would probably be enough
4. I need to know the points, in order to draw a line between them
5. Yes, speed is an important issue (if I shall use exitFrame to update, else it is not)