0

enter image description here

In my 2d java game, I need to move 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)? Any ideas would be very helpful.

arandomguy
  • 428
  • 5
  • 23
  • 3
    If you're attempting any kind of spatial game programming, it will be a painful experience unless you've learnt some basic geometry and trigonometry first. I suggest you pick up a decent maths textbook before going too much further. – Oliver Charlesworth Sep 26 '14 at 10:19
  • Check http://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors or http://stackoverflow.com/questions/6222671/angle-from-vector – Tom Sep 26 '14 at 10:21

1 Answers1

1

The equation of movment with constant speed is simple

positionX = startPositionX + speedX * time 
positionY = startPositionX + speedY * time 

where

speedX = speed * sin(angle)
speedY = speed * cos(angle)

But most of the game use incremental aproach

posX = posX + speedX / lengthOfTick
posY = posY + speedY / lengthOfTick
talex
  • 16,886
  • 2
  • 27
  • 60