0
\$\begingroup\$

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);
}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ What should be observed in the gif, and what is the expected behaviour? \$\endgroup\$ – Alexandre Vaillancourt Sep 30 '16 at 18:03
  • \$\begingroup\$ I recorded it, it shows my problem. I hope I can get more help if the problem is visualy illustrated. \$\endgroup\$ – Sharpirate Sep 30 '16 at 18:05
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Mar 7 '17 at 4:44
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.