I'm making a game of life clone, (you know, just for fun) and I'm having a little problem with the update (tick) method.
Here is the the update and render loop:
while (running) {
double nsTick = 1000000000 / amountOfTicks;
long now = System.nanoTime();
deltaRender += (now - lastTime) / nsRender;
deltaTick += (now - lastTime) / nsTick;
lastTime = now;
while (deltaRender >= 1){
render();
frames++;
deltaRender--;
if (tickRunning) {
while(deltaTick >= 1) {
tick();
ticks++;
deltaTick--;
}
}
if (System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("Ticks: " + ticks);
frames = 0;
ticks = 0;
}
}
}
stop();
As you can see I have separated the render and tick functions because tick's rate is controlled by the user in real time.
The problem is that, when the game starts, (or is unpaused), tick has an unstable update rate for a few seconds (around 10) and then adjust to the right one. Can that be fixed?
The game currently runs on a single thread.