New Step resetForces flag and Fiedler's "fix your timestep"

I'm trying to use the new 2World::Step resetForces flag (from r27) to implement a "pseudo-fixed" time step for box2d.

Here's what I've got so far:
Code:
-(void) tick: (ccTime) dt
{
   int32 velocityIterations = 8;
   int32 positionIterations = 1;

   static float accumulator = 0.0f;
   static float pdt = (1.0/60.0); //the fixed physics delta to use
   
   float dtToAddToAccumulator;
   dtToAddToAccumulator = dt;
   
   if (dtToAddToAccumulator > 0.25f)
      dtToAddToAccumulator = 0.25f;
   
   accumulator += dtToAddToAccumulator;
   while (accumulator>= pdt  )
   {
      world->Step(pdt, velocityIterations, positionIterations, false);
      
      accumulator -= pdt;
   }

   //snipped out the display update stuff…

}


It's working! - Although I'm probably doing stuff in a less-than-optimum fashion.

But I'm not sure how to implement the bold red part below excerpted from the famous "fix your time step" article:

Quote:
The final touch

So it seems that all is well. Our simulation is stable and framerate independent but there is still one last thing to do before it is perfect. Consider what will happen if the display framerate is 55fps and the physics is running at 60fps. Basically the time accumulator will have to alternate between taking one and two physics steps each display update to make sure that it keeps up. This irregular amount of physics steps taken per update causes a subtle but visually unpleasant stuttering of the physics simulation on the screen known as temporal aliasing.

Even worse, if you setup a slow motion mode (1/10th speed) your effective display framerate would be 600fps if your display is actually updating at 60fps. Assuming the physics is also running at 60fps the accumulator would only do one physics step every 10 display updates. Objects would animate in discrete jarring steps every 1/6th of a second. This looks pretty dodgy but the good news is that there is a way to make sure that everything runs smoothly all of the time.

The solution is to interpolate between the previous physics state and the current state based on how much time is left in the accumulator. This will add a latency of up to dt to your physics simulation, but the visual results are definitely worth it. Here is how to implement it:


Here's his example of how to interpolate between the states:

Code:
    float t = 0.0f;
    const float dt = 0.01f;

    float currentTime = time();
    float accumulator = 0.0f;

    State previous;
    State current;

    while (!quit)
    {
         float newTime = time();
         float deltaTime = newTime - currentTime;
         currentTime = newTime;

         accumulator += deltaTime;

         while (accumulator>=dt)
         {
              previousState = currentState;
              integrate(currentState, t, dt);
              t += dt;
              accumulator -= dt;
         }

         const float alpha = accumulator / dt;

         State state = currentState*alpha + previousState*(1.0f-alpha);     //<--- I'M STUCK HERE AAAAAHHHHHHHH!!!!!

         render(state);
    }


Might anyone know how would I go about changing my code to incorporate what's accomplished by the "I'M STUCK HERE" line above?
There's 2 ways I see of doing this.

1. Interpolation : Keep both the current and previous physic states in your objects. RenderPosition = (CurrentPosition - PreviousPosition * Alpha) + PreviousPosition. Do the same for angle. If you want to get fancy, you can use the velocities to get a better interpolation.

2. Extrapolation : You only need the current physic state. RenderPosition = CurrentPosition + Velocity * (FractionOfSecond) where FractionOfSecond is the fraction of second to extrapolate. Do the same for the angle.

1 is more precise, uses more memory and creates a 1 frame lag. 2 is less precise, uses less memory and may cause the objects to jump if they change direction abruptly. I went for 2 and with a small enough timestep, it's smooth.
You probably need the forces to be reset on the final sub-step. So pass in _true_ for the final sub-step.
Tiblanc wrote:
If you want to get fancy, you can use the velocities to get a better interpolation.


Could you explain this in greater detail?
I don't really know the math to make it work flawlessly, but this is the general principle.

Let's say you throw a ball up and step the world 1 second at a time(I know this is bad, but it makes for a better example). The ball reaches its apex at 0.5 sec. You have these 2 physic states :

0 sec : Position (0,0) - Velocity (0, 5)
1 sec : Position (0,0) - Velocity (0, -5)

When you interpolate to 0.5 sec, you won't want your ball to be at (0,0), but at its apex, let's say at (0, 2.5).

A curve can be derived from the 4 values to interpolate between them. I lack the math background to know its name, but that's the basic idea.
I get it, the curve is defined by control points on the curve and tangents at those points; I wonder if this is any better than linear interpolation? Specifically, if the simulator is using linear interpolation between states (for collision/etc) then it seems like using a different scheme to interpolate the graphics might actually be worse than plain linear interpolation.

The main problem for interpolating (extrapolating as well) would seem to be collision, which is a sudden/discontinous change, likely to cause artifacts for any interpolation/extrapolation scheme. Currently we're simply using the most recent simulator state (it's running at 100hz) but at some point we plan on adding smoothing/interpolation, and it worries me since there are many problems on paper..
I don't think it's better than linear interpolation. It's more precise, but nobody would notice.

Unless your game and physics run at low fps, this won't be a problem. You may end up with some overlapping in the case of speedy collisions, but this will last a frame at most. If the game runs at 30 or 60FPS, this will be so fast nobody will notice. It will take someone recording the game and analyzing it frame by frame to detect this kind of anomaly.

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