title: Smooth movement in Java2D top down game
body: <p>I am sure that others have had the same problem but after a significant amount of googling, I was unable to find anything completely relevant.</p>
  
  <p>I am working on a 2D top-down game using Java2D. I have separate rendering and physics threads.
  Movement is mouse-based, and calculations take the following form:</p>
  
  <pre><code>    isMoving = false;
      // Sets initial distance moved to 0
      playersMoveX = 0;
      playersMoveY = 0;
  
      // If player clicks, determines the distance to click
      if (MainGame.movement)
      {
          distanceX = MouseClass.changeInX;
          distanceY = MouseClass.changeInY;
  
          moveMarker();
      }
  
      // Prevents divide by zero error, and prevents variables from becoming negative
      if (distanceX &lt; 0)
          distanceX = 0;
      if (distanceY &lt; 0)
          distanceY = 0;
      if (distanceX == 0 &amp;&amp; distanceY == 0)
          return;
  
      // Sets the speed that the player moves (player moves a distance of moveFactor each cycle)
      if (Player.sprint)
          moveFactor = 2;
      else
          moveFactor = 1;
  
      double sumDistance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
      double deltaX = (moveFactor * distanceX / sumDistance) * dT; 
      double deltaY = (moveFactor * distanceY / sumDistance) * dT;
      // Determines distance moved in x and y directions by using the ration of distanceX to distanceY
      if (MouseClass.mousePositionX &gt; GUI.fullScreenWidth / 2 - 1 &amp;&amp; preventCollision(0, npcs, buildings, objects))
      {
          playersMoveX += deltaX;
      }
      else if (preventCollision(1, npcs, buildings, objects))
      {
          playersMoveX -= deltaX;
      }
      if (MouseClass.mousePositionY &gt; GUI.fullScreenHeight / 2 + 5 &amp;&amp; preventCollision(2, npcs, buildings, objects))
      {
          playersMoveY += deltaY;
      }
      else if (preventCollision(3, npcs, buildings, objects))
      {
          playersMoveY -= deltaY;
      }
      // Accounts for movement in the distance to the click
      distanceX -= deltaX;
      distanceY -= deltaY;
      if (deltaX != 0 || deltaY != 0)
          isMoving = true;
  </code></pre>
  
  <p>Then, I subtract playersMoveX from every object's x, and playersMoveY from every object's y.
  I tried adjusting the rate of physics calculations, going above 80 'frames' per second when it only renders at 60, no change.</p>
  
  <p><strong>THE PROBLEM</strong></p>
  
  <p>Objects bounce around when the player moves. The Player is centered in the screen, so everything else moves in relation to it. Since I am using doubles, the way I handle rounding is always rounding down with Math.floor()</p>