Ok quick question: I have two methods that are called by my game loop: client tick which renders is called as often as possible, game tick which handles game logic is called at a set interval (30 gameticks per sec).
So mostly the process looks like the following:
client tick
client tick
client tick
game tick
client tick
client tick
client tick
but some times due to lag, I get the following:
client tick
game tick
game tick
game tick
client tick
client tick
client tick
The problem is that all of my update position code is in the game tick, so the position of a character will be updated several times without being rendered to the screen. The result is that the lag causes my charcater to skip across the screen. As far can understand, the game tick takes too long, resulting in calling of another game tick becasue of the timing. I realize that the update position code should not be in the game tick, so here is the question:
Where should I put my update position code? Not in the client tick since then it will happen too often. Should it stay in the game tick and some kind of loop has to be set up to check for lag?
How do you properly handle lag in your game loop?
Here is the code:
passedTime = 0;
previousTime = timer.getClockTicks();
while (true) //run until quit app
{
fTime = passedTime / TICKS_PER_GAMETICK;
//rendering
clientTick();
currentTime = timer.getClockTicks();
passedTime += currentTime - previousTime;
previousTime = currentTime;
while( passedTime > TICKS_PER_GAMETICK )
{
gameTick();
passedTime -= TICKS_PER_GAMETICK;
}
}