In my game-loop, I am using fixed time step for physics and interpolation for rendering as suggested on Gaffer on Games | Fix Your Timestep!
However, when the framerate is varying between 30-60fps during the game, the game looks jumpy. For example, balls suddenly look accelerated when the frame rate increases from 35 to 45 suddenly.
Is there a way to make the game look smooth while framerate is varying?
Here is my game loop:
protected void update(float deltaTime) {
//do some pre stuff
deltaTimeAccumulator +=deltaTime; //deltaTimeAccumulator is a class-member holding the accumulated frame time
while(deltaTimeAccumulator>FIXED_TIME_STEP) {
world.step(FIXED_TIME_STEP, 6, 2); //perform physics simulation
deltaTimeAccumulator-=FIXED_TIME_STEP;
}
// world.step(deltaTime, 6, 2);
destroyBodiesScheduledForRemoval();
render(deltaTimeAccumulator /FIXED_TIME_STEP); //interpolate according to the remaining time
}
Here is the part related to the interpolation (related inner works of render() method) :
this.prevPosition = this.position; //get previously simulated position
this.position = body.getPosition(); //get currently simulated position
//interpolate
Vector2 renderedPosition = new Vector2();
if (prevPosition != null) {//&& !isFloatApproximatelyEquals(this.prevPosition.x, this.position.x) && !isFloatApproximatelyEquals(this.prevPosition.y, this.position.y)) {
renderedPosition.x = this.position.x * interpolationAlpha + this.prevPosition.x * (1 - interpolationAlpha);
renderedPosition.y = this.position.y * interpolationAlpha + this.prevPosition.y * (1 - interpolationAlpha);
} else {
renderedPosition = position;
}
//Draw the object at renderedPosition