We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I'm making the classic Arcade game Asteroids, and i've got movement all down and shooting a missile. The problem is, I dont know how to shoot a missile in the direction that I'm pointing, and since the ship is always rotating, the rotation is always changing. How would I do this?
Thanks in advance,
Anacan
heres my code at the moment:
http://hastebin.com/eciwilulom.java - thats the main class
http://hastebin.com/ulayoworur.java - thats the missile class im using
Answers
basic trigonometry.
if something is moving in a direction given by angle a at a given speed s then it moves s * cos(a) in the x direction and s * sin(a) in the y direction with evey step.
in your case
xv and yv should reflect the above when you instantiate Missile.
you are currently calling it with
m = new Missile(x,y,5,5);
so the missile will just move diagonally, regardless of where the ship is pointing.it's similar to how you are calculating x and y just before that.
and please don't start duplicate threads. there are three of these now, all very similar.
So, for velocity arguments xv, yv:
down 5 and right 5.
left 7.
Pick numbers based on which way the ship is facing.