title: Java shoot towards mouse
body: <p>I have a top down 2d game where you walk around shooting bad guys. I want to be able to shoot towards the mouse, no mater what direction it is but I have absolutely no idea how to do this.</p>
  
  <p>Here is my <code>bullet</code> class:</p>
  
  <pre><code>public class bullet {
  
  public double x, y,dy,dx,mx,my;
  public int dir;
  
  public Rectangle r = new Rectangle((int) x, (int) y, 5, 5);
  
  public bullet(double x, double y) {
      this.x = x+10;
      this.y = y+10;
      this.mx = Comp.mx;
      this.my = Comp.my;
      r = new Rectangle((int) x, (int) y, 5, 5);
      if (x &lt; mx+play.camx) {
          dx = 1;
      }
      if (x &gt; mx+play.camx) {
          dx = -1;
      }
      if (y &lt; my+play.camy) {
          dy = 1;
      }
      if (y &gt; my+play.camy) {
          dy = -1;
      }
  }
  
  public void tick() {
      x+=dx;
      y+=dy;
  
      r = new Rectangle((int) x - play.camx, (int) y - play.camy, 5, 5);
  }
  
  public void render(Graphics g) {
      g.setColor(Color.black);
      g.fillRect((int) x - play.camx, (int) y - play.camy, 5, 5);
  }
  }
  </code></pre>