# Finding vectors with two points

We're are trying to get the direction of a projectile but we can't find out how

For example:

[1,1] will go SE

[1,-1] will go NE

[-1,-1] will go NW

and [-1,1] will go SW

we need an equation of some sort that will take the player pos and the mouse pos and find which direction the projectile needs to go.

Here is where we are plugging in the vectors:

def update(self):

self.rect.x += self.vector[0]
self.rect.y += self.vector[1]


Then we are blitting the projectile at the rects coords.

• Is this strictly tile-based? Must the projectile travel at angles or only in strictly axis-aligned directions? – Sean Middleditch Jun 27 '13 at 0:59
• i needs to be able to travel in any direction so if you click in front of the player it will travel forward if you click in back it will travel backwards etc – Serial Jun 27 '13 at 1:01
• Possible duplicate: gamedev.stackexchange.com/questions/49290/… – MichaelHouse Jun 27 '13 at 1:26
• No, 360 degree mobility not compass directions! – Serial Jun 27 '13 at 1:28
• If you take your endpoint minus the starting point you'll get a vector from start to end point. Is this what you're looking for? I'm not sure I understood correctly. – RandyGaul Jun 27 '13 at 1:29

To compute a direction you just need subtraction, and ideally normalisation.

self.vector = (mouse.position - player.position).normalize()


Note that your loop update should take the velocity and time between two frames into account:

self.rect.x += speed * timestep * self.vector[0]
self.rect.y += speed * timestep * self.vector[1]

• we got it all to work but thanks for the good answer we may use it to shorten the code :) – Serial Jul 27 '13 at 22:08

A vector is a position in space relative to other vectors, and by using Pythagoras and some Trigonometry, we can determine the vectors direction and distance in relation to a target vector.

For the projectile, you are going to need to determine the length of its flight path from the start to the end vector and the angle to which you must launch that projectile.

To find the magnitude(direction): Say we have two vectors in R2(2D Cartesian coordinate system): P(3,4) Q(6,8) we must subtract each of the corresponding X and Y terms, then subtract those two elements, square them, then take the root of the whole equation, this is illustrated as follows:

sqrt((6-3)^2-(8-4)^2) which equates to roughly: 2.6458 (Euler angles)

Now we have the distance between the two vectors, it is time to generate the angle:

Tan((8-4)/(6-8)) Which roughly equates to: 1.285

So now rotate your projectile to 1.285 degrees and then offset the projectile for a distance of 2.6458.

Albeit not a complete solution, you can do this check each time the target moves and update the destination of the projectile, you will however, need to find a suitable method to transition between angles, otherwise the result may look choppy.