Best way to find the closest point from object line to cámera

I have the cámera in a static point and a Object Line in the scene. Simply, I want the way to find what is the point from Object Line that is closest to the cámara.

I have to follow all the segments of the Object Line, create a Line3 and testing with “closestPointToPoint”, or are other efficent or direct method?

Thanks for your comments

Using closestPointToPoint looks like a good solution. Just do not forget:

  • set clampToLine, because you actually work with segments, not lines
  • if the object has scale, rotation or translation, use world coordinate of vertices

A possible, but minor, increase of performance is if you directly calculate the distance from the segments (i.e. do not make intermediate Line3 objects). You can get the required math from the source of Line3. It is just a few lines of code.

I’m not sure abaut clampToLine…
In my strategy, I would take each consecutive vertex of the Object Line and create the Line3 in world coordinates to get each clousestPoint. Review the points that i get and find the more clousest of the all of them. But I’m not sure why I should to use the paramether “clampToLine” in this context. I’ll try an algoritm and see what occurs with it.

It is always better to try and see which option works for your case.
Line3 attempts to model infinitely long abstract lines. My assumption is that you want finite lines (i.e. segments). I might be wrong, of course.

3 Likes

Ah ok! You’re right. That is the point, ofcourse. Thanks! Pavel