I've implemented a deterministic, fixed timestep system from here: http://gafferongames.com/game-physics/fix-your-timestep/
Everything works fine, output is the same with each step but there's one problem - the animation is jerky while the character isn't moving.
Here's the code responsible for interpolation:
draw.x = previous.getX() * alpha + current.getX() * (1.0f - alpha);
draw.y = previous.getY() * alpha + current.getY() * (1.0f - alpha);
g2d.drawImage(image, (int)draw.x, (int) draw.y, null);
Here's how 'alpha' looks like:
0.29991353
0.35988057
0.41996205
0.41996205
0.4799291
0.4799291
0.53989613
0.5998632
0.5998632
0.65994465
0.7199117
0.97999954
0.039999668
0.099999785
0.1599999
0.21999958
0.2799997
0.33999982
0.39999995
0.4599996
0.51999974
Let's assume that player's initial position is x = 100 and he's not moving.
His interpolation gives values like 100.0000123123 (which when casted to int gives 100 - OK) but it also gives values like 99.99999998, which when casted to int gives 99 (which makes the jerk).
I don't know how to handle this, maybe just make a statement (if previous != current then do interpolation) and that's it?
Thanks very much
Edit Here's my gameloop:
float fps = 60;
float dt = 1 / fps;
float accumulator = 0;
Time time = new Time();
float frameStart = time.getSeconds(); - // returns seconds since initialization for ex. 1.32234, 6.43243
while(match_running) {
float currentTime = time.getSeconds();
accumulator += currentTime - frameStart;
frameStart = currentTime;
if (accumulator > 0.2f)
accumulator = 0.2f;
while (accumulator > dt) {
doPhysics(dt);
accumulator -= dt;
}
float alpha = accumulator / dt;
gamePanel.drawGame(alpha); // here's the interpolation
}