Limiting game loop to exactly 60 tics per second (Android / Java)
Posted
by
user22241
on Game Development
See other posts from Game Development
or by user22241
Published on 2014-06-02T09:27:57Z
Indexed on
2014/06/02
9:54 UTC
Read the original article
Hit count: 393
So I'm having terrible problems with stuttering sprites. My rendering and logic takes less than a game tic (16.6667ms) However, although my game loop runs most of the time at 60 ticks per second, it sometimes goes up to 61 - when this happens, the sprites stutter.
Currently, my variables used are:
//Game updates per second
final int ticksPerSecond = 60;
//Amount of time each update should take
final int skipTicks = (1000 / ticksPerSecond);
This is my current game loop
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
//This method will run continuously
//You should call both 'render' and 'update' methods from here
//Set curTime initial value if '0'
//Set/Re-set loop back to 0 to start counting again
loops=0;
while(System.currentTimeMillis() > nextGameTick && loops < maxFrameskip){
SceneManager.getInstance().getCurrentScene().updateLogic();
//Time correction to compensate for the missing .6667ms when using int values
nextGameTick+=skipTicks;
timeCorrection += (1000d/ticksPerSecond) % 1;
nextGameTick+=timeCorrection;
timeCorrection %=1;
//Increase loops
loops++;
}
render();
}
I realise that my skipTicks is an int and therefore will come out as 16 rather that 16.6667 However, I tried changing it (and ticksPerSecond) to Longs but got the same problem).
I also tried to change the timer used to Nanotime and skiptics to 1000000000/ticksPerSecond, but everything just ran at about 300 ticks per seconds.
All I'm attempting to do is to limit my game loop to 60 - what is the best way to guarantee that my game updates never happen at more than 60 times a second?
Please note, I do realise that very very old devices might not be able to handle 60 although I really don't expect this to happen - I've tested it on the lowest device I have and it easily achieves 60 tics. So I'm not worried about a device not being able to handle the 60 ticks per second, but rather need to limit it - any help would be appreciated.
© Game Development or respective owner