java - Firing bullet from gun's current position to mouse x & y position -


please i'm kinda lost in coding.

so i've created bullet class:

package javagame.states;  import org.newdawn.slick.color; import org.newdawn.slick.gamecontainer; import org.newdawn.slick.graphics; import org.newdawn.slick.image; import org.newdawn.slick.input; import org.newdawn.slick.slickexception; import org.newdawn.slick.geom.vector2f; import org.newdawn.slick.state.statebasedgame;  public class bullet {      private vector2f pos;     private vector2f speed;     private int lived = 0;      private boolean aktiv = true;      private static int max_lifetime = 2000;      public bullet (vector2f pos, vector2f speed){         this.pos = pos;         this.speed = speed;     }      public bullet(){         aktiv = false;     }      public void update(int t){          rotation++;          if(aktiv){             vector2f realspeed = speed.copy();             realspeed.scale((t/1000.0f));             pos.add(realspeed);              lived += t;             if(lived > max_lifetime) aktiv = false;         }     }      public void init(gamecontainer gc, statebasedgame sbg) throws slickexception{     }      public void render(gamecontainer gc, graphics g) throws slickexception {          if(aktiv){             g.setcolor(color.red);             g.filloval(pos.getx(), pos.gety(), 10, 10);         }     }      public boolean isaktiv(){         return aktiv;     } } 

then called bullet class here in robot class:

package javagame.states;  import java.util.iterator; import java.util.linkedlist;  import org.newdawn.slick.gamecontainer; import org.newdawn.slick.graphics; import org.newdawn.slick.input; import org.newdawn.slick.slickexception; import org.newdawn.slick.geom.vector2f; import org.newdawn.slick.state.basicgamestate; import org.newdawn.slick.state.statebasedgame;  public class robot2 extends basicgamestate{      private linkedlist<bullet> bullets;      @override     public void init(gamecontainer gc, statebasedgame sbg) throws slickexception {         bullets = new linkedlist<bullet>();     }      @override     public void render(gamecontainer gc, statebasedgame sbg, graphics g) throws slickexception {         for(bullet b : bullets){             b.render(gc, g);         }            }      @override     public void update(gamecontainer gc, statebasedgame sbg, int delta) throws slickexception {         iterator<bullet> = bullets.iterator();         while(i.hasnext()){             bullet b = i.next();             if(b.isaktiv()){                 b.update(delta);             }else{                 i.remove();             }         }          input input = gc.getinput();         int mousex = input.getmousex();         int mousey = input.getmousey();          int xdistance = (int) (robotx() + robotwidth() / 2 - mousex); //robotx robot image x position same roboty         int ydistance = (int) (roboty() + robotheight() / 2 - mousey);         double angletoturn = math.todegrees(math.atan2(ydistance, xdistance));              if(mousex != 0){             angletoturn += 270;             }else{             if(mousey != 0)             angletoturn += 180;             }             robot.setrotation((float) angletoturn); //robot image. didn't included code here shorten codes                  if(gc.getinput().ismousepressed(input.mouse_left_button)){             bullets.add(new bullet(new vector2f(sprites.getrobotx(), sprites.getroboty()), new vector2f(mousex, mousey)));         }     }      @override     public int getid() {         return states.robot;// has integer value     }  } 

the robot rotating according mouse's cursor movement when hit mouse's left button fire bullet, doesn't follow mouse's cursor location. i've got when run these codes , pressing left button of mouse. please check sample image.

http://s12.postimg.org/kwsr41p71/untitled_1.jpg

what want achieve correct direction of bullet according mouse cursor location.

any greately appreciated. thank much.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -