Frame Rate Independent Movement Box2Das3

Hi guys,

I've had to start work on a realtime multiplayer box2d game. I know that the usual advice for someone starting on such a project would be to rethink and do something else - but in this case it's not crucial to have players perfectly synched (they wont collide with each other for example). However I need to make gameplay as smooth as possible and as such think it would be a good idea to implement Frame Rate Independent Movement (as discussed here: http://www.box2d.org/forum/viewtopic.php?f=3&t=2065).

I've tried to convert the C++ example there to as3 but haven't been able to get it working quite right as of yet - anybody else had a go with doing this?

Also any other advice for a project like this would be greatly appreciated..
Sure, this should be pretty easy to adapt to any language. What exactly are you having difficulty with.
Ok so far I have the following variables declared for my class:

Code:
      private var iPrevTime:int = 0;
      private var iAccumulator:Number = 0;


iPrevTime is initialised when box2D gets initialised:

Code:
      private function InitB2World()
         {
         iPrevTime = getTimer();
         ...
         }


And here's the code which updates Box2D
Code:
      protected function DoMapUpdate()
         {
         const millistep:int = 20;
         const timestep:Number = millistep/1000;
         const iterations:int = 15;
         const iterationsb:int = 10;
         var thisTime = getTimer();
         iAccumulator += thisTime-iPrevTime;
         iPrevTime = thisTime;
         
         while(iAccumulator>=millistep)
            {
            iWorld.Step(timestep,iterations,iterationsb);
            iAccumulator -= millistep;
            }
         }


The exact problem I'm having with it at the moment is that when using this - everything becomes very "jittery" unless I use a very small value for millistep (i.e. <10). This code isn't fired in an 'onEnterFrame' event but on a timer event, the timer is initialised by:

Code:
private var iTimer:Timer = new Timer(25);


Here's a quick video showing some 'jitteryness' - http://www.youtube.com/watch?v=tjUP8zvHPuE
The original article discusses this problem. He proposes interpolation, which is the best if you must have fixed time step.
Part of the problem might be you are stepping with 20 milliseconds, but having a timer interval of 25 milliseconds.

So, your accumulator will start at 25( the deltaMS ) and go down to 5 after the first step, so you'll do another full step. This will leave your accumulator at -15 now... The next frame you'll add a further 25 to that bringing it up to 10. Another full step will take place bringing the accumulator down to -10. Next frame it will be -5, then 0 and you'll do 2 steps again after that.

So basically you're adding a whole new step every 5 frames. If you want it to remain smooth, you'll have to do like Boris suggests and interpolate your render positions based on the accumulator and how far into the 'next' frame you are.( or previous frame in some of your cases.)
Thanks for the advice guys - I'll get this fixed and repost the working code as soon as I get it working incase anyone else ever looks for a solution..
Thanks again guys - I think I'm getting closer to getting it. Basically what I've taken from the article is that after running the box2D step - I'll set a value ('alpha' in the article - 'iInterpolation' in my code):

Code:
      protected function DoMapUpdate()
         {
         const millistep:int = 20;
         const timestep:Number = millistep/1000;
         const iterations:int = 15;
         const iterationsb:int = 10;
         var thisTime = getTimer();
         iAccumulator += thisTime-iPrevTime;
         iPrevTime = thisTime;
         while(iAccumulator>=millistep)
            {
            iWorld.Step(timestep,iterations,iterationsb);
            iAccumulator -= millistep;
            }
         iInterpolation = (iAccumulator/millistep);
         }


Currently this is set as a public static var in my class. All my display objects are derived from one class which has this to update it's current display on the map:

Code:
      protected function UpdateDisplayClip(aPosition:b2Vec2,aRotation:Number)
         {
         iDisplayClip.x = (aPosition.x*30);
         iDisplayClip.y = (aPosition.y*30);
         iDisplayClip.rotation = (aRotation);
         if(iLastDisplayPoint.x>0)
            {
            var diffX:Number = (iDisplayClip.x - iLastDisplayPoint.x);
            var diffY:Number = (iDisplayClip.y - iLastDisplayPoint.y);
            }
         iLastDisplayPoint.x = iDisplayClip.x;
         iLastDisplayPoint.y = iDisplayClip.y;
         if((diffX>0)||(diffY>0))
            {
            iDisplayClip.x += diffX*CMapBox2D.iInterpolation;
            iDisplayClip.y += diffY*CMapBox2D.iInterpolation;
            }
         }


The results of this is that the movement is much less 'jittery' (http://www.youtube.com/watch?v=NkBRplSb6rg) But it's not perfect so I was thinking maybe I've misunderstood somewhere / got something quite wrong.
There's several things wrong with that. You want.

newX = aPosition.x*30;
newY = aPositiony*30;
newAngle = aAngle;
sprite.x = (newX - oldX) * interpolation + oldX;
sprite.y = (newY - oldY) * interpolation + oldY;
sprite.angle = (newAngle - oldAngle) * interpolation + oldAngle;
oldX = newX;
oldY = newY;
oldAngle = newAngle;
Thanks for that code Boris, I now understand where I was going wrong previously and have implemented the new code. However it's now slightly more 'jittery' than ever which leads me to think that I've messed up in the 'DoMapUpdate' function:

Code:
      protected function DoMapUpdate()
         {
         const millistep:int = 20;
         const timestep:Number = millistep/1000;
         const iterations:int = 15;
         const iterationsb:int = 10;
         var thisTime = getTimer();
         iAccumulator += thisTime-iPrevTime;
         iPrevTime = thisTime;
         while(iAccumulator>=millistep)
            {
            iWorld.Step(timestep,iterations,iterationsb);
            iAccumulator -= millistep;
            }
         iInterpolation = (iAccumulator/millistep);
         }


Trying on a different tack - I found doing only this to try and achieve the same results very nearly gave the required results:

Code:
      var step:Number = (thisTime-iPrevTime)/1000;
         iWorld.Step(step,iterations,iterationsb);


Thanks so much again for your help and for the amazing physics engine.
Hmm, the accumlator code looks fine. I think the problem might be in how you are calculating your interpolation point.

You are dividing the accumulator by the millistep. This will give you a value between -1 and 0( you'll never hit -1 exactly since you are doing a >= for the accumulator but you get my point. ) What you really want is a value between 0 and 1. Try negating it in your calculation for the interpolation point.

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