-4
\$\begingroup\$

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?

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

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

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)

\$\endgroup\$

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.