Gamedev In-depth Topics: Time-based vs. Frame-based Movement

KidsCanCode · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

Discusses time-based vs frame-based movement in game development

Full Transcript

welcome back to another video on in depth topics in game dev this is a series where we talk about certain subjects that students may find a little bit confusing the first time around or haven't quite found the right explanation for this time we're gonna be talking about time based versus frame based movement which is how you ensure that the animation that's happening in your game happens at a consistent speed even if you start to experience lag as always these videos will be coded in Python but this is a totally language-independent topic and whatever whatever language you're coding in or whatever game framework you're using this this lesson will apply just as well so in this video I want to talk a little bit about timing especially how it relates to movement so I've made a little program here where we have a sprite and the sprites gonna spawn on the left-hand edge of the screen and now I want to make this sprite move I want this sprite to just move to the right across the screen and I want it to take exactly 5 seconds to make that trip so how would I do that well if we look at a quick calculation here the screen is 600 pixels wide and I want the sprite to take 5 seconds so that means I wanted to move a hundred and 20 pixels every second but we don't just move 120 pixels wait a second and then move on to another hundred and twenty right we're moving we want to smooth animation so we want to move every frame and if we're running at 60 frames per second and we want to move 120 pixels every second that means we're gonna move two pixels every frame and if we move it exactly that speed it will take 5 seconds to cross the screen so that's easy enough to do we will give our sprite a velocity of two and we will just update it by moving the X by that velocity and that will make our sprite move so now our sprite is moving and it's taking five seconds to cross the screen and that's great although real quick I think for the purposes of our testing I'm gonna go ahead and make it wrap around the screen so that we can see it keep going so if the rectangle is right actually let's let it move all the way off let's say if the left is greater than width then right equals zero okay so so we have our sprite moving and it's moving at the speed we want because it's moving a certain amount every frame but the problem is what happens if our frame rate changes imagine we have a really busy game with lots of sprites on the screen lots of activity going on and we start to get some lag in fact let's imagine we got a lot of leg and our frame our frame rate dropped to 30 frames per second so that means we're still gonna be moving to pixels every frame but we're only moving 30 times per second and not 60 and now all of a sudden our speed is half of what it was we're taking ten seconds to cross the screen so if this was a bullet or it was a you know a rocket flying across the screen now suddenly it's traveling slower and it's taking longer to travel the same distance and this is not very good we want our objects in the world to travel at the speed we want and not be tied to the frame rate we want them to be independent of the frame rate and we can accomplish this by looking at how long the frame is taking now most game frameworks provide you some way of finding this out in the case of tie game when we do RFPs when we tick the clock to keep our timing of our loop to the FPS we wanted it returns us back a value and that value which we're gonna call DT or delta T which is what most game frameworks will call it is how much time the previous frame took all right if we're running at 60 frames per second that will be one sixtieth but if we start to get lag this number will change and in PI game this number is given to you in milliseconds so I'm gonna go ahead and just divide by a thousand so that we have it in seconds instead so now we have a delta time for how long the previous frame took and if we look back at our calculation that we did a minute ago we know that we wanted to travel at 120 pixels per second that's the speed we actually want our sprite to move at and notice this has nothing to do with frame it's just how many pixels to travel in one real second of time so that's what we want to set our speed to so when we update our sprite we're gonna pass it the DT from the game so it receives the DEET the delta time okay and Delta just stands for change if you haven't heard that term before Delta is the Greek symbol that looks like a little triangle and you just ants further change so this is the change in time since the last frame so we're gonna change our VX here and we're gonna make our VX be 120 right since that's what we want our speed to be and then we're gonna move at 120 times whatever the change in time was so again if this was if we're right now if we're going in at 60 frames per second then we're gonna see the same calculation we're gonna take that 120 and it's gonna be multiplied our mean sorry divided by or x 160th right it's gonna be multiplied by one sixtieth and get s2 so if we're running at 60 frames-per-second will still be moving to pixels every frame but as soon as this number changes then the number of pixels per frame is going to change and we won't have to recalculate it the computer is going to do it for us so right now if we set this back to 60 you'll see that we're taking five seconds to cross the screen same as we were before but now if we start experiencing lag and we drop to 30 frames per second our sprite still takes five seconds to cross the screen and in fact if something really horrible happened and we dropped to ten frames per second still we're gonna take five seconds to cross now obviously the animation isn't as smooth anymore but that's okay if you're running a tenth frame ten frames per second this is what's going to happen but at least our sprite isn't now taking what six times five is thirty seconds across the screen right which would be a ridiculously slow bullet if this was a bullet so now we've made our movement of our sprite independent of the framerate so that's all there is to time-based vs. frame based movement you just keep track of how long it's been since the last frame happened and again most game frame works in one way or another we'll give you that amount of time and you use that to figure out how many pixels you should move for that particular frame and then everything will stay consistent you just set your speeds in pixels per second and everything will remain consistent so I hope this video was helpful and that if you were struggling to understand time based versus frame based movement sometimes the documentation out there can be a little hard to get the first time through but you should in most cases you want to use this in your games that you making if you liked this video please press the like button below and help others find it and if you haven't already think about going over to our patreon page and contributing a few dollars a month that would really help in producing the videos and keeping this channel going the videos will always be free and it's not required but it would of course be greatly appreciated thanks and I'll see you in the next video you

Original Description

In this video we discuss how to ensure things in your game move at a consistent speed, even if there is a loss of frame rate (lag). "In-depth Topics" will explore and explain a little more deeply certain subjects that may be tricky for new programmers to understand, or that may not quite fit into the usual tutorial structure. Most will likely be math related, but they may also cover other topics that come up or are requested by viewers. Note: While the examples here will be using Python/Pygame, the actual material will apply more generally to all game development, regardless of what language you may be using. If you like these videos please consider supporting me on Patreon: https://www.patreon.com/kidscancode
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 54 of 60

1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

Related Reads

Up next
monday.com Automation: Fix Missing Emails in Seconds
Mastering monday
Watch →