0
\$\begingroup\$

At it's core what is delta time? In every equation to make a player jump I see that it is multiplied by delta time. Is this a constant value or a changing one? How does this value help? I don't understand why it's needed for velocity, acceleration and jumping.

\$\endgroup\$
2
\$\begingroup\$

Delta time is the time elapsed since the previous frame.

It can be a constant value but is not required to be; typically it will be dependent on factors including, but not limited to:

  • how fast your hardware is,
  • whether or not you have vsync enabled,
  • what your monitor's refresh rate is,
  • whether your program is framerate limited or not,
  • etc.

It's important because otherwise movement would be done at a rate that is framerate-dependent: if your program was running at a faster framerate then movement would likewise be faster.

\$\endgroup\$
0
\$\begingroup\$

Great answer by 'Le Comte du Merde-fou'.

Small practical pseudo-code example:

game(){
 previousTime = systemTime.now() // initial time

 //gameloop
 while(){

  currentTime = systemTime.now() // current time
  dt = currentTime - previousTime // difference between current and previous time
  previousTime = currentTime //save the current time to be used to calculate dt for the next cycle

  update(dt)
  //render etc...
 } 
}
\$\endgroup\$
3
  • \$\begingroup\$ The thing is, the framework I'm using lets you set the fps in the gameloop and then it only runs the loop that many times per second. So if i had an update() function in the loop, and i had set the framerate to 60fps, it would be run exactly 60 times per second. So if that's the case, then would I even need to calculate dt and pass it in as a parameter? \$\endgroup\$ – Ivan Lendl Sep 8 '17 at 4:14
  • \$\begingroup\$ @Ivan-Lendl In that case dt would always be (1/60). But depending on the hardware the game is run on, frames might be finished faster or slower than this time. If they are finished faster, one solution is to delay the next update until the correct amount of time has passed. If it is run slower one solution is to skip frames. It is possible to ensure that we never run at >60 fps but it is not possible to ensure that we never run at <60 fps since it will depend on your code and the hardware it runs on \$\endgroup\$ – George Hanna Sep 8 '17 at 7:36
  • \$\begingroup\$ @Ivan-Lendl You could set dt as a parameter with a constant value or better yet not even use it at all if you know that the game will always run at 60fps. The game might behave differently on hardware that isnt able to run it at 60 fps though \$\endgroup\$ – George Hanna Sep 8 '17 at 7:43

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.