Jump to content
  • Advertisement
Sign in to follow this  

quick math question. ...

This topic is 4774 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

The position of the closest point P2 is there where a line from P to P2 is perpendicular (90 degree angle) to line AB.
So knowing the position of the points and the angle, it's a matter of basic trigonometry.

Share this post

Link to post
Share on other sites
The line is given by the equation:

x = A + t * (B - A) / |B - A|

The dot product '.' can be used to project a vector onto another:

u.v = |u|*|v|*cos(alpha)
|v|*cos(alpha) = u.v / |u|

So use it in the line equation:

u = B - A
v = P - A
t = u.v / |u|

And you get

x = A + (B-A) * (B-A).(P-A) / |B-A|²

Share this post

Link to post
Share on other sites
Here's another method. Convert the line segment AB into origin/direction form, like this:

Vector O = A;
Vector D = B-A;

Now find the parametric projection of P onto AB:

float t = Dot(P-O, D)/Dot(D, D);

If you want the closest point on the segment rather than on the infinite line, clamp t to [0, 1]:

if (t < 0.0f) t = 0.0f; // Or return O
if (t > 1.0f) t = 1.0f; // Or return O+D

Now use t to find the point on AB closest to P:

return O+tD;

[Edit: same as what nmi said.]

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!