Understanding framerate independence and deltatime

preview_player
Показать описание
A video about using deltatime to create a game that runs at any framerate. I will use pygame to implement the logic but the theory applies everywhere.

(You also get lots of perks)

Social stuff:
Рекомендации по теме
Комментарии
Автор

in my honest opinions this channel is so underated this channel deserves 1 mil subs

sbinti
Автор

Attention at 4:50.
The example there is about "fixed time", and does not count difference between frames.
This could led to confusion if you implemented it in your code, expected that everything multiplied by that just "move correctly" (since it will not).
To correct, you need to take your system's "now" time, subtracting "last_draw_time" from it. The result is the right value of the delta time that you can multiply.
This value will have to (maybe) be elevated to some power of 10, just to reduce precision. But it works as expected.

bersi
Автор

It's worth mentioning that this video doesn't show how smoothly the rectangle moves (probably because of YouTube's video compression, I don't know). But it's definitely worth following along in this example to see how to achieve smooth motion! Before watching this, I was always a bit frustrated by the occasional jerkiness of object motion in Pygame--no matter what frame rate I used. This fixes the problem! If you care about smooth motion in Pygame, learn this technique!

kenhaley
Автор

This is such an elegant explanation. Just wanted to add, that I had fun adding my scaling factor to the speed as well. My game is using a standard 320x180 pixel art canvas then everything is scaled up 4x or 6x (or less for fun). The scaling was only applying to the image sizes but now I am adding scaling to the speeds. As I understand it, using dt also simplifies the choice of speed as a value basically translates to pixels per second. Therefore, I have to correct it with my 4x or 6x scaling. (Don't want 100 pixels per second on a 320 screen AND a 1920 screen). Very satisfying

michaelwallace
Автор

BEST video about delta time in games i have never followed a tutorial that actually made me understand this THANK YOU!

tails_the_god
Автор

I struggled so much with the floating point issue and my position being 0 because of it.
Thank you!!

wngZ_
Автор

Really well explained. I like the mix of theory and practical demonstrations.

seekeroftheball
Автор

Thanks YouTube recommendations for this.. I've tried to make this already since August 2021 and didn't know how to solve it. Thanks you, man, you are really cool

lnt
Автор

This is exactly what I was looking for. Almost ALL tutorials handling movement and jumping using frame dependant code and it's always bothered me. They do not even touch on the topic of accurate movement. Today was watching a tutorial on jumping and they set jump_height = 15 (steps aka frames) then were monitoring a variable called jump timer counting frames and once you hit 15 you'd be at max height...

But just like you explained if you ran this game at 600 fps... it would happen at light speed and you'd only jump a fraction of the height you'd jump at 30 fps.

This is a such an important part of a game's development and I do not see anyone talking about this. I'm glad I found this video and am not going crazy lol.

Nyghtprowler
Автор

5:09 wouldn't the formula be movement*target framerate*deltatime to prevent lag?

DylanBeaudry
Автор

Very good explanation. Thanks for making the video.

יואבט
Автор

Thank you for the great videos! I ended up purchasing your course on Udemy and loved it.

RH_Guitar
Автор

*Review*: ok, so by multiplying the desired speed by Δt each frame refresh, this enables you to scale the speed traveled each frame according to the hardware fps.

so. Each frame refresh the motion would actually be different with this formula. *but* since there are more frames being output at 120fps vs 60fps, over 1 second the lower motion per frame should normalize to a constant value

Retrofire-
Автор

Another solution or addition:
Have two update methods.

One for logic and the other for drawing. The drawing loop should run at computer speed while the logic loop should run at a fixed interval ideally 30-60 times a second

davidsyengo
Автор

It's a bit easier to do that now in Pygame-CE thanks to frects, which are rects that use floats instead of integers, so you don't need that extra variable for the position.

yanlucca
Автор

Generally a very good video and helps a lot to grasp this really important matter, but you erroneously built the table from 5:50:
0) 1/30=0.033, not 0.33 etc.
1) units are wrong. On the left site you've got 1/s*pixel*s=pixel, whereas on the right site you've got pixel/s.
2) The variable you calculate in the table is just covered distance i.e. Δx, not Δx/Δt.
3) You indeed want to have the same Δx/Δt regardless of FPS, but to have it constant
4) You need to have various Δx and Δx/frame. For instance, since
Δx/Δt=Δx/frames*frames/Δt=Δx/frames*FPS,
if you've got FPS=10 and you want Δx/Δt=100 you must have Δx/frames=10 and Δx=10 in every frame, but if you've got FPS=30 and want Δx/Δt=100, you must have Δx/frames=100/30 and Δx=100/30 in every frame. Thus Δx/frames must differ.

plrc
Автор

Happy I found your channel, learning a lot here :)

ellsonmendesYT
Автор

For the Pygame fps, have you tried using time.perf_counter() instead of time.time()?

brunorcabral
Автор

Excellent video as always. Keep it up!

yourdad
Автор

Thanks bro, just taught me all of the basics :D

paxxous