Results 1 to 2 of 2
  1. #1
    Jacob_ is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default What's wrong with this game loop?

    Java Code:
                while(true)
                {
                    double elapsed = (int) (System.currentTimeMillis() - lastUpdateTime)/1000f;
                    w.fps = 1/elapsed;
                    lastUpdateTime = System.currentTimeMillis();
                    w.step();
                    processInput();
                    processTasks();
                    gameView.getGamePanel().repaint();
                    if(System.currentTimeMillis() - lastStatsUpdateTime > 500)
                    {
                        lastStatsUpdateTime = System.currentTimeMillis();
                        w.updateStats();
                    }
                    try {
                        nextStepTime += frameTimeMs;
                        int sleepTime = (int) (nextStepTime - System.currentTimeMillis());
                        if(sleepTime > 0)
                            Thread.sleep(sleepTime);
                    } catch (InterruptedException ex) {
                         ex.printStackTrace();
                    }
                }
    This is supposed to limit the FPS to 60. However, from frame to frame the calculated FPS varies wildly between 30, 60, and 0... but only those 3 numbers, or something very close. I've tried removing everything except the drawing code and it still does the same thing.

    Does anyone see the problem? This has been bothering me for months, and I think it's the cause of some slight physics instability.

  2. #2
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    Not exactly sure what you are doing in this loop. Other than the process the input and tasks, And not sure why you want to limit the FPS.
    FPS can be calculated using milliseconds. (1000 / FPS) = MPF. To go from MPF (milliseconds per frame) to FPS.
    You just do basic algebra to solve for FPS. FPS = (1000 / MPF)
    Not sure if this helps but since no one has posted on this in months i thought id try
    Last edited by rdjava; 06-02-2011 at 01:32 AM.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. Guessing Game with for loop
    By ccart62 in forum New To Java
    Replies: 3
    Last Post: 11-22-2010, 01:55 AM
  3. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  4. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 08:51 PM
  5. [help] game loop
    By Fuzzier in forum Java Applets
    Replies: 3
    Last Post: 10-31-2009, 09:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •