How to shoot bullets depending on how something is rotated in a game

edited December 2016 in How To...

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

    Missile(float xstart, float ystart, float xv, float yv)
    

    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:

    m = new Missile(100,100, 5,5);
    

    down 5 and right 5.

    m = new Missile(100,100, -7,0);
    

    left 7.

    Pick numbers based on which way the ship is facing.

Sign In or Register to comment.