Skip to main content
gamedev.net
🔒 Locked

Untitled

Started by Marianne 3 replies 900+ views
Original Post
Marianne
Marianne
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)
daftasbrush
daftasbrush
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.
ajas95
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?

Zakwayda
Zakwayda
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;
Marianne
Marianne
thnaks to you all, it works just fine!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.