title: Moving a square from a starting point to the position of a mouse click at a fixed speed
body: <p>I'm trying to move a square from it's original position to the coordinates of my mouse when I click. The code I have somewhat works, but the square does not go directly to the mouse click. It goes diagonally a bit off, then it goes to the mouse click, as shown in the picture. I tried to make it a straight movement path but I could not think of a way to do so. I'm pretty sure the error is something to do with the 1st method below.</p>
  
  <pre><code>           ________X ← the mouse click
            /        
           /   ↑
          / ←  = movement path
         /
        /
  _____/
  |   | ← character
  |___|
  </code></pre>
  
  <p>Here are the 3 methods involved so far
  (pls dont criticize my code too much)</p>
  
  <pre><code> //int x and int y are the positions of the mouse, champion = the character
  
  public static Champion calculateChampionMovement(int x, int y, Champion champion) {
  
      if (x != champion.x || y != champion.y) {
  
           //x and y dist = the distance between the character and the cursor
          int xDist = x - champion.x;
          int yDist = y - champion.y;
  
           //the angle
          plrAngle = Math.atan2(yDist, xDist) * 180 / Math.PI;
  
           //the speed of the character on the x and y axis
           //(character diagonally moves at the speed of "champion.speed")
          plrXSpeed = champion.speed * Math.cos(plrAngle * Math.PI / 180);
          plrYSpeed = champion.speed * Math.sin(plrAngle * Math.PI / 180);
  
           //calls the method below that actually moves the character
          playerMain.champion = (Champion) Entity.moveChampions(x, y, champion, champion.speed, plrXSpeed, plrYSpeed);
  
          champion.moving = true;
      }
      return playerMain.champion;
  }
  </code></pre>
  
  <p>And the 2nd one...</p>
  
  <pre><code> //called by the method above
  public static Entity moveChampions(int x, int y, Champion champion, float speed, double xSpeed, double ySpeed) {
  
       //if the distance between the character on the x and y axis is not
       //exactly divisible by "speed", then this helps the character stop.
      if (Math.abs(x - champion.x) &lt;= speed) {
          champion.x = x;
      }
      if (Math.abs(y - champion.y) &lt;= speed) {
          champion.y = y;
      }
       //stops the character
      if (x == champion.x &amp;&amp; y == champion.y) {
          champion.moving = false;
      }
       //moves the character
      if (champion.moving) {
          champion.x += xSpeed;
          champion.y += ySpeed;
      }
      return champion;
  }
  </code></pre>
  
  <p>The last method calls "calculateChampionMovement" and "moveChampions", and it moves the character while "moving" is true</p>
  
  <pre><code>public static void buttonTest() {
      if (RIGHTCLICK == true) {
  
           //mouse x and y positions
          cursorClickX = (int) (mapX + MOUSE_X);
          cursorClickY = (int) (mapY + MOUSE_Y);
  
           //first method (setup the x and y speed)
          playerMain.champion = PlayerMain.testMainChampionMove(cursorClickX, cursorClickY, playerMain.champion);
  
       // if character is already moving
      } else if (playerMain.champion.moving == true) {
           //move the character
          playerMain.champion = (Champion) Entity.moveChampions(cursorClickX, cursorClickY, playerMain.champion, champAsdf.speed, plrXSpeed, plrYSpeed);
      }
  }
  </code></pre>
  
  <p>hi riot games pls dont sue me im too young anyways</p>