Hello I have game loop where I can change framerate. But when I change framerate to other, delta time not working fine. For example when framerate is lower everything is faster.
Here is game loop:
double delta = 0.0;
private double framerate = 60.0;
private double frameTime = 1.0/framerate;
public void run() {
runFlag = true;
int frames = 0;
double frameCounter = 0;
double lastTime = (double)System.nanoTime()/(double)1000000000L;
double unprocessedTime = 0;
while(runFlag) {
boolean render = false;
double startTime = (double)System.nanoTime()/(double)1000000000L;
double passedTime = startTime - lastTime;
lastTime = startTime;
unprocessedTime += passedTime;
frameCounter += passedTime;
while(unprocessedTime > frameTime) {
render = true;
unprocessedTime -= frameTime;
tick();
delta = frameTime;
if(frameCounter >= 1.0) {
this.frames = frames;
Display.setTitle(" FPS: "+frames+" DELTA: "+(float)delta);
frames = 0;
frameCounter = 0;
}
}
if(render) {
render();
frames++;
} else {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Display.update();
}
destroy();
}
What is wrong with this game loop ?