Jump to content
  • Advertisement
Sign in to follow this  
Marianne

Untitled

This topic is 4411 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
Advertisement
Take a look here
Distance Point to Line
There's explanation above, but basically they find the shortest distance from point to line, by first finding the point on the line (Pb) closest to the point P...
Then they calculate the distance from P to Pb... you just need Pb.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!