Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
I thing this could help you: gameprogrammingpatterns.com/game-loop.html –  Ricard Pérez del Campo Jun 2 at 14:16
    
Thanks @RicardPérezdelCampo, I hadn't seen this article before. However it doesn't help as it just goes over what is covered in all the other articles I've read on Game Loops (deWITTERS, Secret Room, Against the grain, Fix Your TimeStep!) :-( –  user22241 Jun 2 at 20:39
    
i'm very confused - you use a while-loop and measure timestamps to control your ticks? have you considered another approach? maybe you seperate your animation within an rendering Thread that ticks all 60 seconds? –  Martin Frank Sep 18 at 9:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.