Get the closest point from an object to another one?

Hi,

I’d like to know what is the closest point from an object to another one. (see image)
I’ve tried several things, but none returns the expected result.

A. I’ve tried to find the closest vertex.
It works perfectly on little objects, but not on very big ones (see image) with few vertices, like a wall.
This is not a perfect solution.

B. I’ve tried to get the distance to the center of the object (very often used for LOD systems) but it’s not working at all with big objects.
The solution would be to break big objects into more little ones.
But I want to find another solution.

C. I’ve tried to send a ray to the center of the object and get the hit position.
The problem is :

  1. the center of the object is not always in the center of the mesh (see image)
  2. there is no way to touch the object if another one (with same properties or not) is between.
    The ray would be stopped and return unexpected results.

D. This is the closest point. This is the position that I want to get, and I don’t know how.

If you have other methods, I would be happy to read about it :slight_smile:

Attachments


Similar to what you did to the verticies, but instead you would have to iterate through the faces and calculate the distance between the planes.

Though I did learn how to do this at college, I’m far from remembering how to deal with planes in geometry. A quick googling found these guys explaining how to calculate the distance between two planes:


and

And on wikipedia you can read how euclidean planes are described:
http://en.wikipedia.org/wiki/Plane_%28geometry%29

So, good luck to translate those to a python script :slight_smile: Maybe you can find a python library to find the distance between two planes. I did a quick googling but didn’t find anything.

Thank you, I don’t remember doing it at school. Maybe I was sketching at that time -_-
At least, I feel that I am improving myself thinking hard and seriously about my projects :slight_smile:
But I’m not sure I’ll be able to do it that time.

Does anyone know how to translate a local position (vertex.XYZ) to a global position?

front view (global) (infos from Transformation panel)

center of my cube is [-2, 0, 8]
top left [-5,-3,11]
top right [-5,-3,11]
bottom left [-5,-3,5]
bottom right [1,-3,5]

front view (local) (infos from Transformation panel)

top left [-1,-1,1]
top right [1,-1,1]
bottom left [-1,-1,-1]
bottom right [1,-1,-1]

So, how can I get the global positions of my vertices, knowing the global position of the center?
Vertice’s local position are connected to the objetc’s size (like top left [-1,-1,1] for a little or a very big cube), so does the scale have to be taken in account?

Does anyone know how to translate a local position (vertex.XYZ) to a global position?
That’s easy, just sum one with the other. Imagine if it were only 1 dimension, you have an object at position 10 that has a point at position 5 from that object. So the global position of that point is 10 + 5 = 15.

You’ll just do the same thing for all 3 dimensions. To do that, either:

  • Sum each axis individually by doing:
    vertex_global[0] = vertex_local[0] + global_position[0] and repeat that for axis 1 and 2.

  • or import MathUtils and do it in one line with vectors:
    vertex_global = Vector(vertex_local) + Vector(global_position)

Scale doesn’t matter since the values you’re getting from the vertex positions were already modified by scaling. You’re using the final values after scaling were already taken into account.

I should have said that obviously I had done it (I’m not “that” bad in math :slight_smile: ) the first time, but as I wrote previously :

little or a very very big cube, top left corner has [-1,-1,1] local posiiton.
From that, I can understand that those values don’t use blender units.

So, adding -1 to the center of the object gives me the same result for a little cube or a big one.
And that’s not the result I want to get.
thank you for your help, and I hope I explained myself well enough that time.

A vertex at [-1,-1,1] would be in the same place globally if the object were at the world’s center with no rotation or scaling; to find a vertice’s global position in any other case you have to factor in if the object was scaled (if the object was scaled * 2, so the vertex was globally at [-2,-2,2] the vertex position would still return [-1,-1,1]) so just multiplying the positions by the object’s scaling should fix that, if the object was moved (here you can add the object position to the vertex positions) and if the object was rotated (this is a little beyond me, but I’d imagine there’s plenty of documentation on this around the internet- the best I can come up with is to find the amount of degrees the object has rotated, and rotate the vertices around the centerpoint the came amount. I feel like there should be a more efficient method though.)

In the case of your little and big cubes, did you apply scale, or are they both the same cube with different scaling? that would explain why they both return the same vertex position.

To account for rotation simply multiply the matrix by the local pos:

from Mathutils import Vector, Matrix

ob = ...
opos = ob.position # object position
vpos = ob.mesh.blah.blah.blah # vertex position
ori = ob.orientation # object orientation

# If the position needs to be scaled:
#scale = ob.scaling
#vpos[0] *= scale[0]
#vpos[1] *= scale[1]
#vpos[2] *= scale[2]

vec = Vector(vpos[0], vpos[1], vpos[2]) # make a Vector object
mat = Matrix(ori[0], ori[1], ori[2]) # Make a Matrix object

gpos = mat * vec # Account for rotation in the vertex's position
gpos[0] += opos[0] # Account for objects translation
gpos[1] += opos[1]
gpos[2] += opos[2]

I wouldn’t recommend doing this to find the closest point. Instead use the bounding box of the object, I believe the bounding box can be accessed from the mesh proxy so have a look in the docs. Then you can use the face method talked about above. The bounding box should give a pretty good representation of the object.

Thanks all for your help.
I’m still trying to understand how to calculate the distance between the planes.
The links that vibrunazo gave me are very helpful, though.

About vertex global coordinates,

In the case of your little and big cubes, did you apply scale, or are they both the same cube with different scaling? that would explain why they both return the same vertex position.

Just right-click, S key.

As a different method, assuming this is realtime, have an expanding circle from your player then get the distance to the first vertices to collide with your object. Simaler to socials searcher demo. I guess it wouldn’t work so well if you’re further away, or you would need more verts in your circle but it’s just an idea.