title: Android -- smooth canvas translational scrolling  based on the movement of an asynchronous object
body: <p>I have an android game where a ball moves based on the accelerometer. The ball moves at a variable speed. The game has levels much larger than a smartphone screen, and so I translate the canvas as the ball nears the edge of the screen to accomodate the large levels. The scrolling works, the trouble is smoothly scrolling based on the ball. </p>
  
  <p>Here's an attempt that shifts based on the change of position of the ball between updates:
  <pre><code>
  // RIGHT SHIFT
  shiftFactor = Math.abs(xCoor - prevXCoor);
  if (ball.x + translateX &amp;gt rightX * 0.8
   &amp;&amp; rightX &amp;lt levelWidth + padding) {
      translateX -= shiftFactor;
      leftX += shiftFactor;
      rightX += shiftFactor;
  } 
  </pre></code>
  It looks jittery.               </p>
  
  <p>Here's an attempt that shifts based on some constant. </p>
  
  <pre><code>
  int shiftFactor=2;
  
  //SHIFT SCREEN RIGHT
  if (ball.x + translateX &gt RightPerimeter * 0.8 && rightPerimeter &lt levelWidth) {
    translateX -= shiftFactor;
    viewPortLeft += shiftFactor;
    viewPortRight += shiftFactor;
  }
  </code>
  </pre>
  
  <p>For lower shiftFactor values, it smoothly scrolls, but the ball can easily 'outrun' the scrolling. For larger shiftFactors, the scrolling gets incrementally choppier. </p>
  
  <p>Any suggestions? Thanks.</p>