Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
displacement.Normalize();
V.Normalize();

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:

V.Normalize();
float distance = 10.0f;  //Change this value to whatever distance you want
Point A = B - V * distance;
share|improve this answer
    
I need to make sure that it must reached there. What I can do with Point A. –  user960567 Apr 16 '12 at 4:46
    
What is it, specifically, that you need to calculate? Do you mean that you have A and B and need to calculate V? –  chaosTechnician Apr 16 '12 at 4:48
    
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). –  Jonathan Hobbs Apr 16 '12 at 4:49
    
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. –  user960567 Apr 16 '12 at 4:50
    
So... you want to move point A in direction V (toward B)? Have you tried A += V? –  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;

projection

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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