Let say I have, enter image description here

Point A(X1,Y1) moving in the direction V(W, H). I need to make sure that it reaches B(X2,Y2). I think I need to subtract some value in Point A(X1, X2). But not know what? I also sure that the direction is correct.


You can get the displacement from A to B by subtraction. I'm not sure if your direction V is normalized or not, but if you normalize the direction and normalize the displacement vector, A will reach B if those vectors are equal.

Vector2 displacement = B - A;

if(displacement == V)
    // A will reach B if you get in here

Note: I'm rusty on my XNA so the specific functions you may need to call may be a little off, but the concept should work.

Post-clarification Edit:

float distance = 10.0f;  //Change this value to whatever distance you want
Point A = B - V * distance;
|improve this answer|||||
  • \$\begingroup\$ I need to make sure that it must reached there. What I can do with Point A. \$\endgroup\$ – user960567 Apr 16 '12 at 4:46
  • \$\begingroup\$ What is it, specifically, that you need to calculate? Do you mean that you have A and B and need to calculate V? \$\endgroup\$ – chaosTechnician Apr 16 '12 at 4:48
  • \$\begingroup\$ Your XNA is perfectly fine, but since vectors store floating point values you'd have to do an almost-exactly-equal check (because of ye olde classic difference of one millionth). \$\endgroup\$ – doppelgreener Apr 16 '12 at 4:49
  • \$\begingroup\$ No, I need to make sure that Point B is reached. I need to do something with Point A. I cannot change Vector V. I cannot change Point B. \$\endgroup\$ – user960567 Apr 16 '12 at 4:50
  • \$\begingroup\$ So... you want to move point A in direction V (toward B)? Have you tried A += V? \$\endgroup\$ – chaosTechnician Apr 16 '12 at 4:52

If I understand correctly, you want a point close to A such that moving from that point in the direction v will eventually reach B.

The point closest to A is the projection of A on the line (B,v). It is computed as follows:

newA = B - dot(B - A, V) / dot(V, V) * V;


|improve this answer|||||
  • \$\begingroup\$ Is dot means dot product? If no then can you please explain dot method in language agnostic way. \$\endgroup\$ – user960567 Apr 17 '12 at 15:33
  • \$\begingroup\$ @user960567 yes, dot() is meant as the dot product. \$\endgroup\$ – sam hocevar Apr 17 '12 at 15:37
  • \$\begingroup\$ Can you please explain how you found this formula? \$\endgroup\$ – user960567 Apr 17 '12 at 18:23
  • \$\begingroup\$ @user960567 it's pretty straightforward from the projection of vector AB on the line (B,v) \$\endgroup\$ – sam hocevar Apr 17 '12 at 19:03
  • \$\begingroup\$ Also, why you are subtracting from Point B. \$\endgroup\$ – user960567 Apr 18 '12 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.