Fixed timestep game loop graphics stutter

Hi All,

I'm developing a physics-based game to be able to target different platforms using OpenFL (the successor of NME) and the Box2D port I use is in Haxe: https://github.com/jgranick/box2d,
however I the issue I have is probably generic enough to be recognized by users of other ports.

I'm trying to implement the fixed-timestep loop decoupled from rendering as outlined here http://gafferongames.com/game-physics/fix-your-timestep/ and even though I seem to be doing everything by the book the render still produces a noticeable stutter.

Attached is an SWF that should illustrate it: http://www.openfl.org/download_file/747/14508/

The main part looks like this:

Code: Select all

function enterFrameHandler(ev:Event):Void {
      
     var timeStep:Float = 1/60;      
     var currentTime:Int = Lib.getTimer();      
     var frameTime:Float = (currentTime - prevTime) / 1000;      
      
     if ( frameTime > 0.25 )
     frameTime = 0.25;            
      
     prevTime = currentTime;      
     timeAccumulator += frameTime;      
     while(timeAccumulator >= timeStep)
     {   
          updatePhysics(timeStep);
          timeAccumulator -= timeStep;
     }      
   
     var alpha:Float = timeAccumulator / timeStep;      
     updateGraphics(alpha);               
}


And this is where I update graphics, interpolating position (some less relevant code related to my implementation removed) :

Code: Select all

function updateGraphics(alpha:Float):Void
{
      
      for (b in gameBodies)
      {
         var posX:Float = b.getPosition().x  * PHYSICS_SCALE;
         var posY:Float = b.getPosition().y  * PHYSICS_SCALE;         
            
         var posXIntp:Float = userData.prevPosX * (1 - alpha) + posX * alpha;
         var posYIntp:Float = userData.prevPosY * (1 - alpha) + posY * alpha;   

//More code here...


If anyone's interested to see the full source code, here's a buildable (requires Haxe, OpenFL with Box2D and Spritesheet haxelibs) test project which I tried to make as simple as possible to get my point across:

https://github.com/olegr1/openfl-box2d-tilesheet-test

Thanks for looking into it!
Looks like you are doing everything right. I could barely notice any stutter myself, but if you say so... 😀
What values are you typically getting for the 'frameTime' variable?
It's actually much less noticeable in the browser-based player than the standalone one. My Android phone is somewhere in between. Also, if there's even a slight fluctuation in frame rate (e.g. due to multiple collisions in my actual proof of concept) it causes the graphic to jump what seems like tens of pixels at a time.

Maybe that's how it's supposed to work, I just thought there's a way to reliably make it look smooth.
Now I'm reading about other platforms and everyone making a physics-based game is struggling to make a fixed-step simulation to not stutter.

Here's a trace of the frameTime variable from a few seconds into the simulation:

Code: Select all

Main.hx:174: 0.026
Main.hx:174: 0.026
Main.hx:174: 0.026
Main.hx:174: 0.026
Main.hx:174: 0.026
Main.hx:174: 0.028
Main.hx:174: 0.026
Main.hx:174: 0.035
Main.hx:174: 0.017
Main.hx:174: 0.025
Main.hx:174: 0.027
Main.hx:174: 0.026
Main.hx:174: 0.027
Main.hx:174: 0.027
Main.hx:174: 0.026
Main.hx:174: 0.028
Main.hx:174: 0.023
Main.hx:174: 0.013
Main.hx:174: 0.013
Main.hx:174: 0.014

Original thread: https://box2d.org/forum/viewtopic.php?t=9519