1
\$\begingroup\$

Was thinking this morning, I 99.9% of the time, only see a main game loop, looking like this, just as much so where the delta is passed into update():

while running
getDeltaTime()
...
f(deltatime)
---

Now the question I was thinking to myself was what if between getDeltaTime() and f(deltaTime), the code in the middle took 1 second (large value to be easy). That means f(deltaTime) would have moved whatever object less distance than it was supposed to. This would of course be solved by grabbing the delta time directly before it is used.

Should this be a factor to be concerned with in any case?

\$\endgroup\$
1
\$\begingroup\$

deltatime is the time between two consecutive calls to getDeltaTime(). If a particular frame takes very long, that will show in the next iteration, and the model will be updated accordingly.

That being said, you're right: you should call f(..) straight away. Other code after it. Delays introduced by that extra code, as well as the time it takes to update the model itself, will show in the delta of the next frame.

\$\endgroup\$
1
  • \$\begingroup\$ That makes sense, not something I have considered before, but I will keep immediate calling in mind. \$\endgroup\$ – Rick Taemen May 30 '13 at 9:56
1
\$\begingroup\$

Yes, it is a factor. Some engines take this into account in some cases. For example AndEngine and its LimitedFPSEngine:

@Override
public void onUpdate(final long pNanosecondsElapsed) throws InterruptedException {
    final long preferredFrameLengthNanoseconds = this.mPreferredFrameLengthNanoseconds;
    final long deltaFrameLengthNanoseconds = preferredFrameLengthNanoseconds - pNanosecondsElapsed;

    if (deltaFrameLengthNanoseconds <= 0) {
        super.onUpdate(pNanosecondsElapsed);
    } else {
        final int sleepTimeMilliseconds = (int) (deltaFrameLengthNanoseconds / TimeConstants.NANOSECONDS_PER_MILLISECOND);

        Thread.sleep(sleepTimeMilliseconds);
        super.onUpdate(pNanosecondsElapsed + deltaFrameLengthNanoseconds);
    }
}
\$\endgroup\$

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.