Sign in to follow this  

Untitled

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i think i'll annoy you one more time with my newbie questions :D let's assume i have a point and a line in 3D space:
        /
 o     /
      /
     /
    /
   /
  /
and i connect the point to the line with the shortest path possible (perpendicular, even though on the picture it's not ;))
        /
 o     /
  \   /
   \ /
    /
   /
  /
what i'd like to know is the coord of the point on the line that the perpendicular touches / i think a picture will be clearer:
        /
 o     /
  \   /
   \ /
    o <-- i want to find this point
   /
  /

how can i do that? i could do it in 2D but in 3D i don't know... thanks!! (i don't need code, pseudo-code or an explaination will do)

Share this post


Link to post
Share on other sites
Use the dot product. It's used to determine the projection of one vector onto another.

say your line is connected between points A and B, and you want the closest point to P on that line.


vec rayDir = (B - A).normalize();
vec toP = (P - A);

float dotProduct = rayDir.dot( toP );

vec closestPt = A + dotProduct * rayDir;


Does that make sense?

Share this post


Link to post
Share on other sites
Quote:
Original post by ajas95
Use the dot product. It's used to determine the projection of one vector onto another.

say your line is connected between points A and B, and you want the closest point to P on that line.


vec rayDir = (B - A).normalize();
vec toP = (P - A);

float dotProduct = rayDir.dot( toP );

vec closestPt = A + dotProduct * rayDir;


Does that make sense?
This is right, but the square root can be avoided. Try:
vec rayDir = B - A;
vec toP = P - A;

float t = rayDir.dot(toP) / rayDir.dot(rayDir);

vec closestPt = A + t * rayDir;

Share this post


Link to post
Share on other sites

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this