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
FIXED_TIME_STEP
); what you're describing is temporal aliasing. You need to increase sample frequency in order to minimize it. Interpolation helps with this, but it's quite ineffective in the grander scheme of things. I'm sure you've realized in rendering in general that interpolation doesn't fix a lack of precision. That only works in the movies, where you can "enhance" an image hundreds of times ;) At some point you actually need to increase the sample rate (resolution) of the actual data to get anything meaningful happening. – Andon M. Coleman Aug 15 '15 at 21:50