# Get bullet movement direction?

I have two objects, A and B, at two different points. I want B to fire at A. B's shot moves so far x and y every frame. How do I get the angle it needs to travel at as two numbers, one representing how much X changes and how much Y changes per frame?

• This is basic vector math. Take a look here – Vaillancourt Feb 1 '16 at 16:33

You don´t need the angle. Just use:

x(v)= x(a)-x(b)

y(v)= y(a)-y(b)


Your new vector (x,y) (v) will have the ammount of movement in X and in Y axis needed to go from one point to another.

You can then calculate the ammount of distance per frame based on this vector.

You don't seem to need the angle, only the delta vector between the two positions.

vA = vector representing position of A

vB = vector representing position of B

vD = vA - vB // distance and direction to travel

vD / len(vD) = vDu // direction of travel

And you multiply vDu with your speed per frame, this will give you the distance per frame.

• Seems like we had the same idea at the same time :) xD – Mayuso Feb 1 '16 at 16:33
• @Mayuso yup :) It could probably have fitted as a comment, but there was not much else to say about it. – Vaillancourt Feb 1 '16 at 16:34

Well, as you mentioned only mathematics tag so I'm assuming that you have to do everything manually, without built-in vectors functionality.

So, I'm writing here on of the may methods to do so.

First find the Slope between two points at which bullet A and B do exist, by which we can calculate angle through which I'll determine the direction.

Considerations:

Slope = m

A's position = x1, y1

B's position = x2, y2

tan inverse = atan

angle = theta

First find m between both bullets as,

m = (y2 - y1)/(x2 - x1)

theta = atan(m)

Now using parametric equation of circle which will give you the direction, and you want B to fire at A

newX = Cos(theta) --(1)

newY = Sin(theta)

B's position = x2 + newX, y2 + newY

Repeat from (1)