title: Linear movement stutter
body: <p>I have created simple, frame independent, variable time step, linear movement in <code>Direct3D9</code> using <code>ID3DXSprite</code>. Most users cant notice it, but on some (including mine) computers it happens often and sometimes it stutters a lot. </p>
  
  <ul>
  <li><p>Stuttering occurs with <code>VSync</code> enabled and disabled. </p></li>
  <li><p>I figured out that same happens in <code>OpenGL</code> renderer. </p></li>
  <li><p>Its not floating point problem. </p></li>
  <li><p>Seems like problem only exist in <code>AERO Transparent Glass</code> windowed mode (fine or at least much less noticeable in fullscreen, borderless full screen window or with aero disabled), even worse when window lost focus.</p></li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p><strike>Frame delta time doesnt leave bounds 16 .. 17 ms even when stuttering occurs.</strike></p>
  
  <p>Seems like my frame delta time measurement log code was bugged. I fixed it now.</p>
  
  <ul>
  <li>Normally with VSync enabled frame renders 17ms, but sometimes (probably when sutttering happens) it jumps to 25-30ms. </li>
  </ul>
  
  <p>(I dump log only once at application exit, not while running, rendering, so its does not affect performance)</p>
  
  <pre><code>    device-&gt;Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255, 255, 255, 255), 0, 0);
  
      device-&gt;BeginScene();
  
      sprite-&gt;Begin(D3DXSPRITE_ALPHABLEND);
  
      QueryPerformanceCounter(&amp;counter);
  
      float time = counter.QuadPart / (float) frequency.QuadPart;
  
      float deltaTime = time - currentTime;
  
      currentTime = time;
  
      position.x += velocity * deltaTime;
  
      if (position.x &gt; 640)
          velocity = -250;
      else if (position.x &lt; 0)
          velocity = 250;
  
      position.x = (int) position.x;
  
      sprite-&gt;Draw(texture, 0, 0, &amp;position, D3DCOLOR_ARGB(255, 255, 255, 255));
  
      sprite-&gt;End();
  
      device-&gt;EndScene();
  
      device-&gt;Present(0, 0, 0, 0);
  </code></pre>
  
  <p>Fixed timer thanks to Eduard Wirch and Ben Voigt (although it doesnt fix initial problem)</p>
  
  <pre><code>float time()
  {
      static LARGE_INTEGER start = {0};
      static LARGE_INTEGER frequency;
  
      if (start.QuadPart == 0)
      {
          QueryPerformanceFrequency(&amp;frequency);
          QueryPerformanceCounter(&amp;start);
      }
  
      LARGE_INTEGER counter;
  
      QueryPerformanceCounter(&amp;counter);
  
      return (float) ((counter.QuadPart - start.QuadPart) / (double) frequency.QuadPart);
  }
  </code></pre>
  
  <p><strong>EDIT #2:</strong> </p>
  
  <p>So far I have tried three update methods:</p>
  
  <p>1) Variable time step</p>
  
  <pre><code>    x += velocity * deltaTime;
  </code></pre>
  
  <p>2) Fixed time step</p>
  
  <pre><code>    x += 4;
  </code></pre>
  
  <p>3) Fixed time step + Interpolation</p>
  
  <pre><code>    accumulator += deltaTime;
  
      float updateTime = 0.001f;
  
      while (accumulator &gt; updateTime)
      {
          previousX = x;
  
          x += velocity * updateTime;
  
          accumulator -= updateTime;
      }
  
      float alpha = accumulator / updateTime;
  
      float interpolatedX = x * alpha + previousX * (1 - alpha);
  </code></pre>
  
  <p>All methods work pretty much same, fixed time step looks better, but it's not quite an option to depend on frame rate and it doesn't solve problem completely (still jumps (stutters) from time to time rarely).</p>
  
  <p><em>So far disabling <code>AERO Transparent Glass</code> or going full screen is only significant positive change.</em></p>
  
  <p>I am using <code>NVIDIA</code> latest drivers <code>GeForce 332.21 Driver</code> and <code>Windows 7 x64 Ultimate</code>.</p>