enter image description here

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.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse 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.

  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Sep 26 '14 at 13:37
  • \$\begingroup\$ 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? \$\endgroup\$ – arandomguy Sep 27 '14 at 8:07
  • \$\begingroup\$ 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. \$\endgroup\$ – lvictorino Sep 27 '14 at 8:18
  • \$\begingroup\$ I am a java programmer. What should i use? \$\endgroup\$ – arandomguy Sep 27 '14 at 11:43
  • \$\begingroup\$ 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. \$\endgroup\$ – lvictorino 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).

  • 1
    \$\begingroup\$ What, LaTeX does not work here? \$\endgroup\$ – Per Alexandersson Sep 26 '14 at 12:29
  • \$\begingroup\$ Because we use code here, not LaTeX. It's not enabled for this site as far as I know. \$\endgroup\$ – MichaelHouse Sep 26 '14 at 13:33
  • \$\begingroup\$ Just surprises me, this is essentially a math question, and LaTeX is the standard for typing mathematics... \$\endgroup\$ – Per Alexandersson Sep 26 '14 at 13:37
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Sep 26 '14 at 13:39
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Lars Viklund Sep 26 '14 at 14:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.