Currently I'm using a variable time step and when the FPS drops by even a small amount you can easily notice a slight lurch forward of the player. I'm making a platformer and I'm wondering whether I should use a fixed timestep instead, however I'm confused as to why my movement doesn't look smooth, because the various articles I've read all say that a variable timestep is meant to make movement look much smoother.
So my question: Is a fixed timestep the better option for a 2D platformer, and why would anyone even use a variable timestep if your sprite appears to jump forward every 2-3 seconds?
private static final int UPDATE_RATE = 90;
private static final long UPDATE_PERIOD = 1000000000L / UPDATE_RATE;
private void mainLoop() {
long beginTime, timeTaken, timeLeft;
while (state == State.PLAYING) {
beginTime = System.nanoTime();
update();
repaint();
timeTaken = System.nanoTime() - beginTime;
timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000L;
if (timeLeft < 10)
timeLeft = 10;
try {
Thread.sleep(timeLeft);
} catch (InterruptedException ex) { }
}
}
Delta time is calculated every time repaint is called.
long currentTime = System.nanoTime();
deltaTime = (currentTime - previousTick) / 1000000L;
previousTick = currentTime;
This is how I pass the delta time into the player class.
public void move(float deltaTime) {
if(left && !right)
xPos -= 250*(deltaTime/1000);
if(right && !left)
xPos += 250*(deltaTime/1000);
}