1
\$\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\$
3
  • \$\begingroup\$ What should be observed in the gif, and what is the expected behaviour? \$\endgroup\$
    – Vaillancourt
    Commented Sep 30, 2016 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
    Commented Sep 30, 2016 at 18:05
  • 2
    \$\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
    Commented Mar 7, 2017 at 4:44

1 Answer 1

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\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .