简体   繁体   中英

How can I correctly calculate the time delta?

I'm trying to create a game with an independent frame rate, in which myObject is moving to the right at one unit per millisecond. However, I don't know how to calculate deltaTime in this code:

var currentTime = 0;
var lastTime = 0;
var deltaTime = 0;

while( play ) {

    // Retrieve the current time
    currentTime = Time.now();            
    deltaTime = currentTime - lastTime;  
    lastTime = currentTime;

    // Move myObject at the rate of one unit per millisecond
    myObject.x += 1 * deltaTime;       
}

Let's say the first frame took 30 ms, so deltaTime should be 30 but it was 0 because we only know the time at the start of the frame not at the end of the frame. Then, in the second frame it took 40 ms, so deltaTime is 30 and thus myObject.x is 30. However, the elapsed time is 70 ms (30ms in 1st frame + 40ms in 2nd frame ) so myObject.x is supposed to be 70, not 30.

I'm not simulating physics, I'm just trying to move myObject relative to the elapsed time (not the frame).

How do I calculate deltaTime correctly?

I know that some game engine people use chunk of time or tick, so they're animating ahead of time. Also, I've already read Glenn Fiedler's article on fixing your timestep and many other ones, but I'm still confused.

Try this:

float LOW_LIMIT = 0.0167f;          // Keep At/Below 60fps
float HIGH_LIMIT = 0.1f;            // Keep At/Above 10fps

float lastTime = Time.now();

while( play ) {

   float currentTime = Time.now();
   float deltaTime = ( currentTime - lastTime ) / 1000.0f;
   if ( deltaTime < LOW_LIMIT )
      deltaTime = LOW_LIMIT;
   else if ( deltaTime > HIGH_LIMIT )
      deltaTime = HIGH_LIMIT;

   lastTime = currentTime;

   myObject.x += 1000 * deltaTime;     // equivalent to one unit per ms (ie. 1000 per s)
}

There is alot wrong with this, but it makes it easier to illustrate the basic concept.

First, notice that you need to initialize lastTime with some value BEFORE the loop starts. You can use a lower value (ie Time.now() - 33) so that the first frame yields the desired delta, or just use it as I did (you will see that we limit it in the loop).

Next you get the current time at the start of each froame, use it to calculate the time elapsed since the last loop (which will be zero on the first run of this exaple). Then I like to convert it to seconds because it makes much more sense to work in "per second" than "per milisecond" - but feel free to remove the / 1000.0f part to keep it in ms.

Then you need to limit the deltaTime to some usable range (for the example I used 10-60fps, but you can change this as needed). This simply prevents the loop from running too fast or too slow. The HIGH_LIMIT is especially important because it will prevent very large delta values which can cause chaos in a game loop (better to be somewhat inaccurate than have the code break down) - LOW_LIMIT prevents zero (or very small) time steps, which can be equally problematic (especially for physics).

Finally, once you've calculated the new deltaTime for this frame, you save the current time for use during the next frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM