java - Make projectiles fire a certain direction -


how make projectiles fire direction?

i'm making small game java class in play small red square face , have shoot oncoming shapes borders of frame survive long possible.

your character moves w, a, s, , d, , @ same time move target mouse. have made "slimeball" graphic shoot mouth wherever target upon mouse click. however, can't life of me figure out how implement this. no other questions find on here quite answered needed.

this have right timer make slimeballs move:

 actionlistener timeraction = new actionlistener(){     public void actionperformed(actionevent evt) {          if(evt.getsource() == moveslimeball){             slimex += slimexspeed;             slimey += slimeyspeed;             repaint();         }     } };  timer moveslimeball = new timer(10 , timeraction); 

i know incomplete, doesn't yet except move ball top left corner down toward right. have calculate angle between players mouth (where shoot from) , target is:

firingangle = (int)(math.atan((double)(xmouth - mousex) / (double)(ymouth - mousey)) / math.pi * (double)180); 

but giving me strange values, can't figure out how make proper 360 degree measure. i'm not sure begin use timer make move in right direction. i'm new this, i've been learning basics of java past few months have started on graphics. if appreciated! other variables have if helps:

    private int mousex;     private int mousey;     int slimex;     int slimey;     boolean isfiring = false;     int slimexspeed = 4;     int slimeyspeed = 4; 

and here's mousepressed method:

public void mousepressed(mouseevent e) {      if(mousex >= playerx - 12.5 && mousex <= (playerx + 87.5) && mousey >= playery - 12.5 && mousey <= playery + 87.5){         targetisvalid = false;         isfiring = false;         sound.invalidtargetsound.play();         repaint();     }else{         sound.shootslimesound.play();         targetisvalid = true;         isfiring = true;         repaint();     } } 

edit: fixed angle measure, measures correct value

firingangle = math.atan2(mousex - mouthx , mousey - mouthy);     firingangle -= math.pi/2.0;     firingangle = math.todegrees(firingangle);     if(firingangle < 0){         firingangle += 360;     } 

edit 2: fixed distance measure, finds hypotenuse of difference in original x , target x, original y , target y. have angle , distance , timer working flawlessly, can please putting them make move using information?

public void calchypot(){      diffx = playerx - mousex;     diffy = playery - mousey;     firingdistance = math.hypot(diffx, diffy);  } 


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -