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.

I was working on my game, adding stuff to it, when I noticed my ticks were in the low 50's when I had set it to do a constant 60. This is my run method.

public void run() {
    init();
    long lastTime = System.nanoTime();
    final double amountOfTicks = 60.0;
    double ns = 1000000000.0 / amountOfTicks;
    double delta = 0;
    int updates = 0;
    int frames = 0;
    long timer = System.currentTimeMillis();

    while (running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if (delta >= 1) {
            gameUpdate();
            updates++;
            delta--;
        }

        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        gameRender();
        frames++;

        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            frame.setTitle(title + "   |   " + "Frames Per Second: "
                    + frames);
            System.out.println(updates + " Ticks, " + frames + " Frames");
            updates = 0;
            frames = 0;
        }
    }
    stopGame();
}

I've tried removing all the code I added before this occurrence happened and it didn't help. Suggestions would be great!

share|improve this question
add comment

2 Answers

up vote 13 down vote accepted

Delete the Thread.sleep() call (assuming you're on a desktop machine and have power to spare!).

In general you never sleep in your game loop (except on Android, where there appears to be no other choice). The bad thing about Thread.sleep is it is unpredictable and may cause your game to give up exec time for longer than 16ms, which is the maximum time a frame can take to complete a frame at 60 fps.

If there is too much time at the end of your loop, you usually can let the GPU VSYNC keep you at 60 FPS. If VSYNC is off, then you busy wait, basically in the form of a loop like:

while( getTimeDeltaThisFrame() <= 16 ms )  ;  // keep "spinning" until
                                              // 16ms have passed.

Note that busy waiting on a portable device is going to burn a lot of battery, and you may have to allow a sleep call and step down to a lower frame rate there.

share|improve this answer
    
Thank you for informing me of this I appreciate it. –  Evan Nudd Apr 26 '13 at 0:37
1  
Allow me to append: For desktop programs. On a portable device, you will be burning a lot more battery, so you'd worry about the 100% usage thing. –  bobobobo May 9 '13 at 2:00
add comment

Make sure never to have the Thread.sleep(). It takes time for a render to complete... That's why the FPS is lower than expected.

share|improve this answer
6  
Thread.sleep is unpredictable. Calling Thread.sleep is like going to sleep really late and setting your alarm clock really early. You might wake up in time, but you also might "hit snooze" many times and wake up later than you wanted. The O/S may not wake up your game 2ms later. It may be 30 ms later. Once you call Thread.sleep, you lose control of the CPU and relinquish it to the O/S. For that reason you cannot call Thread.sleep in a game loop. –  bobobobo Apr 25 '13 at 20:10
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.