# Box2d Fixed Timestep and Interpolation

I am using libGDX and I have problems implementing the box2d fixed timestep with interpolation.

This is my code:

private void updateWorld() {

accumulator += Gdx.graphics.getDeltaTime();

while (accumulator>= step) { // The step is 1/10

copyCurrentPosition();

world.step(step, 8, 3);
accumulator -= step;
}

interpolate(accumulator / step);
}

private void copyCurrentPosition() {
prevPosition = new Vector2(player.body.getPosition().x, player.body.getPosition().y);
}

private void interpolate(float alpha) {
player.body.setTransform(player.body.getPosition().x * alpha + prevPosition.x * (1.0f - alpha), player.body.getPosition().y * alpha + prevPosition.y * (1.0f - alpha), 0);
}


• What should be observed in the gif, and what is the expected behaviour? – Vaillancourt Sep 30 '16 at 18:03
• I recorded it, it shows my problem. I hope I can get more help if the problem is visualy illustrated. – Sharpirate Sep 30 '16 at 18:05
• It looks like your interpolation function isn't just positioning the visual of the item, it's actually moving the physics body back in time to the interpolated moment. This means your rendering step alters your physics, precisely the opposite of what you want from a fixed timestep system. What you want to do instead is to move only the visual representation of the object in your interpolation function, while allowing the fixed update step to retain exclusive control of the physics body's movement. – DMGregory Mar 7 '17 at 4:44

That's a pretty large time step (0.1s), which almost certainly spans multiple render frames. The LibGDX docs recommend either 1/45f or 1/300f for that argument. I've personally been using 1/300f and it works fine.
The docs also recommend 6 and 2 (vs your 8 and 3) for the other arguments, but these are close enough together it's probably not that critical.