(Delta time) Getting 60 updates per second in java

I have seen this code several times.

long lastTime = System.nanoTime();
final double ticks = 60D;
double ns = 1000000000 / ticks;    
double delta = 0;

      

The above code takes the System Time and stores it in lastTime

. 60 ticks should match the number of times to update per second.

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

      

It takes now

and subtracts lastTime

, then converts it to nanoseconds / 60. Is there any guarantee that a time difference between now

and lastTime

before nano of more than 60 will cause the delta to be greater than or equal to 1.60 times per second? I can't figure out why it tick();

would work about 60 times per second. From my calculation, every time the loop runs, the delta increases by 0.0025 or so.

+3


source to share


3 answers


Not knowing which in tick()

, I can't be sure, but I would guess that it also uses 60D

to try to sleep for about the required amount of time? So: no, there is no guarantee that there is this code to fix.

He says: "If he tick()

slept less than a tick, then do nothing, if he slept on a tick or more, mark once."



Presumably if the ticking goes through enough (e.g. 80ns, then 80ns, which means the 1st cycle increases the tick and the second increases it by 2), then eventually there will be another cycle that only has a delta 40ns even out.

+1


source


I have commented out the code a bit to show more clearly what is going on.

//Get the system time
long lastTime = System.nanoTime();
//Specify how many seconds there are in a minute as a double
//store as a double cause 60 sec in nanosec is big and store as final so it can't be changed
final double ticks = 60D;
//Set definition of how many ticks per 1000000000 ns or 1 sec
double ns = 1000000000 / ticks;    
double delta = 0;

while(running){
//Update the time
    long now = System.nanoTime();
//calculate change in time since last known time
    delta += (now - lastTime) / ns;
//update last known time    
    lastTime = now;
    //continue while delta is less than or equal to 1
    if(delta >= 1){
//Go through one tick        
        tick();
//decrement delta
        delta--;
    }

      



Now I'm sure that what this does, but I can't tell for sure without seeing that tick () is

+2


source


It guarantees that the tick () method will only execute 60 times per second. It is called delta timeout and is referenced throughout the gaming community. The reason for delta sync is to make games run at the same speed across all platforms. Things like physics and animation inside games should run at the same speed regardless of the system speed. If you have a slow system and the game does not include delta sync, physics will run slower than if the game was running on a fast system.

How it works

  • figure out how many times you want to update per second, in your case it is 60 times per second.

  • find the resolution of languages ​​embedded in a temporary function. Your code is using Java built into the System.nanotime () function, which returns a long value for how many nanoseconds have been started by the system.

  • there are 1,000,000,000 nanoseconds per second, which means that the difference in time for a tick () call is 1,000,000,000/60. This is approximately 16666666 nanoseconds. This means that whenever tick () is called, the program will wait 16666666 nanoseconds before calling tick () again.

  • you need to find the time between the current frame and the last frame in nanoseconds. This value divided by timestep (16666666) will give you a decimal percentage of how much time has passed. If you add this decimal percentage to the delta variable when the delta variable> = 1, 1 / 60th of a second has passed, which means the program can now call tick ().

  • finally you minus 1 from the delta variable. The reason why you are not setting the delta variable to 0 is because there may have been a higher time interval than 1 and the delta variable will be higher than 1, which means that you will need to account for this on the next pass and call tick ()

+2


source







All Articles