How to move an object uniformly from one point to another at a fixed angle? [duplicate]

In my 2d java game, I need to move Point objects (as projectiles) from Enemy object to Player object in a straight line. I did some research and found out that I can use atan2(x, y) method of Math class to get the angle, but then how am i supposed to move the projectile in that particular angle(direction)? I checked this question: How do I calculate how an object will move from one point to another? , but did not quite understand. Also there is no vector math in java. Any ideas would be very helpful.

• Not understanding an answer isn't reason enough to post the same question again. If you're having trouble implementing the solution from the other question, ask specifically about the trouble you're having. For example, in this situation, it appears you're having trouble with vector math. That means your solution is to get a vector math library or write your own. Vector math is very important for games, and it's unlikely you'll be able to avoid it for long. See this stackoverflow question about it. Sep 26 '14 at 13:42

[EDIT: as I don't know what lib you're using, and as this question is not specific to Java, my answer is in pseudo-code.]

If I understand your question correctly, atan2 is not what you're looking for.

What you want to achieve is moving something (which has coordinates) from one point to another according to time. You just need to compute the vector between the player entity and the target.

What you need here is:

• the position (x,y) of the player (player_position).
• the position (x,y) of the target (target_position).
• the speed at which you want your bullet to travel (in unit per seconds) (bullet_speed).
• the time passed between two game updates (two frames) (deltaTime).

As you seem to have all of that, you seem ready to make that bullet move.

The vector from your player to the target, in pseudo code, is :

Vector2 diff_vector = player_position - target_position;


Then put your bullet at player_position and move it each frame alongside this vector you just computed using the deltaTime between frames. Something like:

new_bullet_position = bullet_position + diff_vector * deltaTime * bullet_speed;


Changing bullet position each frame accordingly to the diff_vector and the bullet_speed will make it move uniformly from player to target.

I hope it helps.

• You'd need to normalize the diff_vector to make this work (otherwise you'd get a lot more movement than expected!). And it's actually the same approach found in the highest voted answer to the question linked by OP. Sep 26 '14 at 13:37
• But then I have to use some game library for using vectors because I am making my game using java and its 2d api and there are no classes that deals with vector math. Is there no alternative? Is it not possible to do the calculations without vectors? Sep 27 '14 at 8:07
• Come on! :) The most complex computation in my answer is about multiplying vectors. Something very simple. If you're afraid of vectors I would suggest you to pause developping games and wait until you know more about vectors, because you'll have really hard times trying to make games without using vector computation. Sep 27 '14 at 8:18
• I am a java programmer. What should i use? Sep 27 '14 at 11:43
• You should use Wikipedia. Go to the page related to vectors in Mathematics. Take one hour and read it until everything is clear. Then go back to your code, whatever langage you use because it's really not related to the langage, and code the thing. I won't code it for you. You asked a question about something that is not related to Java programming but related to an higher concept which is very VERY important when it comes to game programming. I suggest you to try. Sep 27 '14 at 11:50

Suppose the projectiles curret position is $(x,z)$. Then, in each step, you let $x_{new} = x_{old} + t \cos(\theta)$ and $y_{new} = y_{old} + t \sin(\theta)$ where $\theta$ is the angle you found, and $t$ is some small number (depending on the number of milliseconds since last update, for example).

• What, LaTeX does not work here? Sep 26 '14 at 12:29
• Because we use code here, not LaTeX. It's not enabled for this site as far as I know. Sep 26 '14 at 13:33
• Just surprises me, this is essentially a math question, and LaTeX is the standard for typing mathematics... Sep 26 '14 at 13:37
• Yep, I'm sure people (myself included) would appreciate you posting in meta about it (for it to be turned on for the site), I can't see how it would hurt. Sep 26 '14 at 13:39
• @PerAlexandersson See an existing meta question on enabling LaTeX processing. Long story short, it's supposedly expensive and it's hard to get it enabled due to unsubstantiated performance concerns. I really miss it and it really discourages me from answering questions here. Sep 26 '14 at 14:45