I had a problem where my AI moved extreme at the first frame and then normal after that. I then figured out it was my delta. It's about 0.016 seconds (60 fps), but the first frame was about 19000 seconds, which is obviously impossible. Does anybody know what might be happening? Also the delta later on likes to oscillate from 0.01 to 0.03.

    long time = Sys.getTime() * 1000 / Sys.getTimerResolution();
    float delta = (time - lastFrame) / 1000f;
    lastFrame = time;
    return delta;

That's the delta code.

EDIT: Wow, you guys really like to take the fun out of composing a question with all your editing.

  • 1
    \$\begingroup\$ What is lastFrame on your first frame? if Sys.getTime() normally returns some large value and lastFrame is initialized to 0 then you'll have a large delta. Usually you need to ensure lastFrame is initialized to the current time just before the start of your game loop. \$\endgroup\$ Oct 24 '13 at 17:43

The oscillation is a common thing. Likely from CPU scheduling and other processes on the system.

The first frame being very long is an error in your code. Consider what lastFrame is set to when the application starts (likely 0), so for the first frame you're just setting deltaTime to the time/1000. You can add a check that if lastFrame is 0, the deltaTime is also 0. Or you can set it to a reasonable value (like the current time), when the application starts. Or you can delay anything that uses deltaTime by at least one frame, something like a delayed start.

  • \$\begingroup\$ OK, thanks. I realised that the first frame actually took longer each time I ran the program, which means that Sys.getTime() doesn't start at zero upon initialization of the application, contrary to what I believed. Does anybody know how actually that method works (it's a LWJGL method)? \$\endgroup\$
    – Kipras
    Oct 24 '13 at 17:47
  • \$\begingroup\$ You can check the documentation here: lwjgl.org/javadoc/org/lwjgl/Sys.html \$\endgroup\$
    – MichaelHouse
    Oct 24 '13 at 18:20
  • 1
    \$\begingroup\$ As Byte56 says, you can initialize LastFrame to be equal to time, so deltaTime comes out as zero. Be careful, though, because many systems (e.g. physics) break down when dt = 0 (e.g. dividing by zero). In addition, you should consider adding a clamp to the calculated value of dt e.g. 1ms to 1s. That way, even if you get a glitch or something goes wrong, your physics system won't explode. \$\endgroup\$
    – MooseBoys
    Oct 24 '13 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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