Page 1 of 1

Explain DT

Posted: Tue Apr 24, 2012 7:16 pm
by Krizzu
Hello

I need someone to explain in the easiest way for what delta time (dt) is for.

I see some codes like:

Code: Select all

frame=dt*30
it's give us 0,3 (because dt is 0.01) so we couldnt just use next variable like

Code: Select all

framee=0,3
?

Thanks for Help guys.

Re: Explain DT

Posted: Tue Apr 24, 2012 7:30 pm
by timmeh42
Delta Time is the time it takes for the computer to go through all the processing/rendering for a single frame. It is dynamically updated, so it can fluctuate depending on what level of processing the last frame required.

Its use in the situation is to regulate the speed at which the game runs. Without factoring in delta time, the faster one's computer is, the faster the game will seem to run (and the opposite for slower computers). For example, in a game you might want a player to move at a speed of 50 units per second, and you had an average framerate of 30 fps, you could say:

Code: Select all

player_x = player_x + 50/30
this would move the player forward by 50/30 units per frame, and at 30 fps this becomes 50 units per second. However, what happens if the fps is 60? In this case, the player is still moved by 50/30 units per frame, but the frame rate is doubled, and so the player seems to move at 100 units per second instead.

To solve this, we use delta time instead of the constant 30 (or more accurately, 1/30, since we multiply by the delta time).
This gets us:

Code: Select all

player_x = player_x + 50*dt
If the frame rate is 30 fps, we get a delta time of 1/30=0.033, and the player moves at 50*0.033=1.65 units per frame. If the frame rate were instead 75 fps, we get a delta time of 1/75=0.0133, and the player moves at 50*0.0133=0.66 units per frame. Of course, the dt is a lot more accurate than what I use here, so the player should move at a constant rate no matter what the frame rate is.

(Would others correct me if I'm wrong please)

Re: Explain DT

Posted: Tue Apr 24, 2012 7:36 pm
by viking
That's a great explanation timmeh42.

Re: Explain DT

Posted: Tue Apr 24, 2012 7:45 pm
by Camewel
dt is used in mechanics, and it stands for Delta Time (delta means "the change in", so 'delta time' just means 'the change in time').
For anyone who does mechanics, they will know that dx/dt = v (x being the distance, and v being the velocity). You can use the velocity and the change in time to work out the change in distance, as dx = vdt, or the change in distance = velocity times the change in time. Such, you get things like x = x + v*dt, which simply calculates the change in x in this frame and adds it onto the current value.
That's the mathematical explanation, and yes, it's more complicated.

Re: Explain DT

Posted: Tue Apr 24, 2012 7:52 pm
by Krizzu
Thanks You guys! Great explanation :3

Re: Explain DT

Posted: Thu Apr 26, 2012 12:07 am
by Puzzlem00n
Oh, wow, I recognize that "frame = dt * 30" as my own! I'm sorry for the confusion, it's just something I do because I come from a background in actionscript, where the increments are a lot smaller, so multiplying dt helps me think better in my tiny little number land! I wish I had been around to answer this.