Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8337693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:22:23+00:00 2026-06-09T04:22:23+00:00

I am building a game with a good emphasis on physics. Therefore I need

  • 0

I am building a game with a good emphasis on physics. Therefore I need the game to run on a very specific interval. Current code:

public double period = .02; //this is the run interval in seconds

//main gameLoop
public void gameLoop(){
    long startTime;
    long sleep;

    while(running){
        startTime = System.nanoTime();

        Graphics2D g = s.getGraphics();
        operateEntities(g);
        g.dispose();
        s.update();
        //figure out how long it must sleep to take .02s altogether
        sleep = ((int)(period*1000) - (System.nanoTime() - startTime)*100000);
        try{
            if(sleep > 0){
                Thread.sleep(sleep);
            }else{
                System.err.println("Warning: program runtime exceeded period");
            }
        }catch(Exception ex){}

        gameTime += period;
    }
}

This is not working as expected. Currently the main thread is executing without sleeping at all, and the “Warning: program runtime exceeded period” warning is firing.

Previously I was using System.currentTimeMillis(), but it was not accurate enough for my purposes, so I switched to System.nanoTime()

Increasing the period actually serves to speed up the program, while reducing it slows it down.

Is there a simple logic faw? is my understanding of System.nanoTime() off? or is there a better way to run the methods operateEntities, dispose, and update on a specific interval?

EDIT: for the record, the program does not take more than .02s to complete. It has been tested

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:22:25+00:00Added an answer on June 9, 2026 at 4:22 am

    Breaking down your code there are a number of problems:

    //Multiplies by 100,000 rather than divides.
    sleep = ((int)(period*1000) - (System.nanoTime() - startTime)*100000);
    //Note that sleep here is a very small number minus a very large number: probably negative.
    
    try{
      if(sleep > 0){//If positive, sleep
        Thread.sleep(sleep);
      } else{//throws an error in all other cases.
        System.err.println("Warning: program runtime exceeded period");
      }
    }catch(Exception ex){}//Empty exception handling poorly handles the thread.sleep() Exception requirement.
    

    This code will always error out unless you make your period value much larger. However, even beyond that your approach will not likely yield the result you want: accurate timing. What your core loop is:

    • Calculate the physics for 0.02 seconds.
    • Go to sleep.
    • Check what time it is.
    • If a particular period (0.02 seconds) has passed, continue, otherwise sleep again.
    • Repeat.

    With a small enough time slice this will be accurate. However, threads don’t work like that. You cannot guarantee when the thread will wake up. It could be never. It could be in three seconds. It could be instantly. Chances are you’re going to overshoot whatever your time period is, and you will effectively never hit it dead on.

    Instead of relying on a specific incremental period, you need to scale all your physics by the period of time that has actually passed, rather than rely on a specific period of time passing consistently every time.

    • Go to sleep.
    • Find out how much time has passed.
    • Calculate physics for that time period.
    • Repeat.

    You still want a small time slice to sleep, but this way you eliminate the error introduced by the thread scheduler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a real time game, mostly chat based, and I need to have
I'm building a game and I'm looking for a good way to implement multiplayer.
I'm current building a game in as3; the proplem I have right now is
I'm building simple game and I need to set game speed for each level.
I am building a game application where I need to play animation videos and
I'm new to this, i'm building a game where users need to log in
I'm building a small video game. I'm trying to do good oop/design. I have
I'm building a game with nodejs 0.6.18 expressjs 2.5.8 and mongoose 2.6.7. I'm trying
I'm building a game level editing app as part of a university project. In
I'm building a game of which the interface is one of the first items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.