I’m messing around with libgdx and box2d and I’m trying to implement sidescroller movement with fixed timestep logic according to the famous Fix Your Timestep! tutorial/guide. However, logic updating seems to be twitchy and I’m not exactly sure why.
Here’s the render method where the issue lies:
public void render() {
handleCameraInput();
handleTestBodyInputImpulse();
//handleTestBodyInputVelocity();
accumulator += Math.min(Gdx.graphics.getDeltaTime(), STEP);
int logicStepCount = 0;
while (accumulator >= STEP) {
world.step(STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
accumulator -= STEP;
++logicStepCount;
}
Gdx.graphics.setTitle("FPS: " + Gdx.graphics.getFramesPerSecond() + ", Logic steps: " + logicStepCount + ", Test body vel: " + testBody.getLinearVelocity());
cam.update(); // update box2d world camera
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render(world, cam.combined); // renders box2d bodies
}
From what I’ve noticed the logic while loop responsible for updating the physics sometimes doesn’t execute (counter goes from 1 to 0 for one frame) and that seems to be causing the twitching. This is weird to me since delta time added to the accumulator is clamped and since the FPS is constant 60 on my machine I’d assume that the loop should always execute once. Am I doing something wrong? Is this behavior normal and is it something I am supposed to solve during rendering with interpolation?
Complete minimal example can be found here on Gist and can be run on any LibGdx project with Box2d dependency.