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:
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:
Here's his example of how to interpolate between the states:
Might anyone know how would I go about changing my code to incorporate what's accomplished by the "I'M STUCK HERE" line above?
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…
}
{
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:
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);
}
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?