Explain DT

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Explain DT

Post 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.
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: Explain DT

Post 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)
viking
Prole
Posts: 6
Joined: Sun Apr 22, 2012 10:29 am

Re: Explain DT

Post by viking »

That's a great explanation timmeh42.
User avatar
Camewel
Prole
Posts: 20
Joined: Thu Apr 19, 2012 5:58 pm

Re: Explain DT

Post 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.
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Re: Explain DT

Post by Krizzu »

Thanks You guys! Great explanation :3
User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Explain DT

Post 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.
I LÖVE, therefore I am.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests