Hi,
I have been making a clone of Asteroids in Java. You can see my progress here Play Asteroids
I am having some trouble with getting the flying saucers to shoot at the player.
I have done some research and found that by taking the players position away from the flying saucers position and using the Math.atan2 function on the result I should get the rotation in radians needed to point towards the player from the flying saucers position.
Here is that in code.
double x1 = saucer.x.get();
double y1 = -saucer.y.get();
double x2 = player.x.get();
double y2 = -player.y.get();
double x = x2 - x1;
double y = y2 - y1;
double angle = Math.atan2(y, x);
I tried this and it did not work. So I did some test calculations to see why.
[tr][td]Saucer Position[/td][td]Player Position[/td][td]Result[/td][/tr]
[tr][td](100, 100)[/td][td](100, 90)[/td][td]3.14[/td][/tr]
[tr][td](100, 100)[/td][td](110, 100)[/td][td]1.57[/td][/tr]
[tr][td](100, 100)[/td][td](100, 110)[/td][td]0[/td][/tr]
[tr][td](100, 100)[/td][td](90, 100)[/td][td]-1.57[/td][/tr]
This would rotate an object to point in the direction I wanted except the Y axis was upside down.
So if I multiplied the Y by -1 it comes out ok.
I then calculate the velocity for the bullet the saucer fires at the player as such
xVelocity += Math.sin(angle) * ACCELERATION;
yVelocity -= Math.cos(angle) * ACCELERATION;
I tried this in my game and the saucers do not shoot at the player.
Any tips on what to try next?
Thanks!
