# Moving in a straight line to the target

• 10-10-2002
Nutshell
Moving in a straight line to the target
Hi,

FOr example i have this situation:
Code:

```|                              |                            |                                  X | |    O | | |_____________________```
And i want "O" to move to "X". How can i do this in a straight line. Using pythagoras' theorem?

Thanks
• 10-10-2002
MrWizard
Subtracting O's position from X's will give you a vector. A vector is a direction and a displacement. That should probably help you.

EDIT: Fixed a typo!
• 10-10-2002
Nutshell
Nah. Can you please give me a formula or something, from which i can then work out the concept.
• 10-10-2002
MrWizard
Okay, some basic mathematics. We have a point O and a point X in 2D cartesian space. X - O = V ( a 2D vector ).

P = O + kV

where P is our new point , O is our original point, k is a scalar between 0 and 1 and V is our vector. You see, if we have 0 for k we get point O. If we use 1 for k we get the endpoint of the vector, or X. So, by incrementing k we will affectively move along this line towards X our destination. Hope this helped you. Let me know if I need to clarify.
• 10-10-2002
Nutshell
mmm..What about the x and y values of "O"? ALso in your first post you actually said to minus X off O, which is O - X, so i was confused ^.^, and i still is. How do you calculate the x and y values along the straight path towards the new point?
• 10-10-2002
MrWizard
oops! My first post is incorrect! My second one is mathematically sound, however. Let's assume that O is at (1, 1) and x is at (7,2). We do our computation.

P = O + kV ( V = X - O = (6,1) )

It's trivial to plug in 0 for k because we will get (1,1) the starting point of O. If we plug in 0.5 for example, we get.

P = (1,1) + (0.5)(7,2) =
P = (1,1) + (3.5,1) =
P = (4.5,2)

That's halfway to our destination!

An example with 0.1 as k ( remember k is between 0 and 1 )

P = (1,1) + (0.1)(7,2) =
P = (1,1) + (0.7,0.2) =
P = (1.7,1.2)

Now due to rounding you will lost accuracy of course but you get the idea.