Games Development Practices The Game Loop and Timing - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Games Development Practices The Game Loop and Timing

Description:

If tick = 0.02s, then NumUpdates = 4.5. Use result to scale values used in ... Using Ticks 2: Fixed Style. Divide the latest frame time by update tick length ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 14
Provided by: lauren80
Category:

less

Transcript and Presenter's Notes

Title: Games Development Practices The Game Loop and Timing


1
Games Development PracticesThe Game Loop and
Timing
  • CO2301 Games Development 1
  • Week 3

2
Basic Game Program Structure
  • Basic program structure used in TL-Engine
  • include ltgame engine librariesgt
  • Initialise game engine
  • ...
  • Load game media
  • Initialise game scene
  • while (game is playing)
  • Render the game scene
  • Move/Animate/Update game scene
  • ...
  • Shut down game engine
  • End program

Game Setup
Game Loop
3
Limitations of this Structure
  • Too simplistic, assumes
  • Game is set-up only once, i.e. only one level
  • No front-end - nothing before game set-up
  • Program ends when game-loop ends
  • Cant go to next level, or back to the front-end
  • We need a more complex structure
  • The exact structure needed depends on the style
    of the game progression

4
Better Game Structure
  • Better generic game structure
  • Initialise engine
  • Front-end setup
  • Front-end loop (until Start Game or Quit)
  • Front-end shutdown
  • if (Quit) Shut-down engine and end program
  • Set starting level
  • Level setup
  • Game loop (until Next Level or Game Over)
  • Level shutdown
  • while Next Level, loop to Level setup
  • Loop to Front-end setup

5
Basic Game Loop
  • Game is displayed like a film or animation
  • Sequence of static images (frames), displayed
    very quickly
  • Objects change position slightly from frame to
    frame
  • We dont perceive separate images, instead we see
    an animated scene
  • Basic game loop performs in this manner
  • Render static image of the scene
  • Move the objects a small amount
  • Repeat process as long as the game is running

6
Limitations of Basic Game Loop
  • No timing in a basic loop
  • Render and update scene as fast as possible
  • Loop frequency dependent on computer speed
  • Scene updates will occur more frequently on a
    fast machine, less so on a slow one
  • Using statements like MoveX(0.1) will cause
    models to move at different speeds on different
    computers.
  • Rendering frequency will also vary - lesser
    problem
  • How can we make our scene updates constant?

7
Game Loop Timing
  • Need to time the duration of each game loop
  • Get accurate measure of the time on each
    iteration of the game loop (each frame)
  • Subtract the previous time from the current time
  • ..to give us the duration since the last frame
  • This is the frame time
  • Use the frame time to determine how much to
    move/rotate models in the current scene update
  • N.B. The frame rate 1 / frame time
  • And frame time 1 / frame rate

8
Update Ticks
  • Decide on a fixed rate at which we would ideally
    like to update the scene
  • E.g. we would like the game scene updated at
    50fps
  • This is a scene update every 1/50 0.02s
  • Call this the scene update tick
  • We will use timing algorithms based on this tick
  • Can have different ticks for other game aspects
  • E.g. an AI component with a tick of 2fps, a
    slower tick as it uses a complex algorithm

9
Using Ticks 1 Variable Style
  • Each time we reach the scene update
  • Divide the latest frame time by the update tick
    length
  • To get the number of updates needed (floating
    pt)
  • Frame Time (time since last update) 0.09s
  • If tick 0.02s, then NumUpdates 4.5
  • Use result to scale values used in scene update
  • E.g. MoveX( CarSpeed NumUpdates )
  • The constant CarSpeed will now be a speed in
    world units per tick - regardless of machine speed

10
Using Ticks 1 Variable Style
  • Some drawbacks to this method
  • Cannot scale matrices in this way
  • Will see importance of matrices shortly
  • Can scale angles and quaternions though
  • On a network game, different speed machines will
    perform different calculations
  • With potentially different results

11
Using Ticks 2 Fixed Style
  • Divide the latest frame time by update tick
    length
  • But get the whole number of updates needed
  • Frame Time 0.09s
  • If tick 0.02s, then WholeUpdates 4
  • Run scene update several times in a loop once
    for each whole update needed
  • Scene update code does not need to be altered
  • E.g. MoveX(CarSpeed), speed is still measured in
    world units per tick
  • Carry remaining time to add to next frame time
  • Here carry 0.01s to add to next frame time

12
Using Ticks 2 Fixed Style
  • Drawbacks of this method
  • Not as smooth as a variable update method
  • Cant run game in slow motion(!)
  • Many ticks on slow machine slows it down more
  • E.g. a machine takes 0.2s per frame
  • it will call scene update 0.2 / 0.02 10 times a
    frame
  • Sometimes no ticks at all between scene updates
    on a fast machine
  • E.g. a machine takes 0.01s per frame
  • Doesnt need to update scene so what to do?

13
Choosing a Tick Method
  • Variable style is more flexible where practical
  • Some game aspects cant be scaled easily
  • AI, physics engines etc. can cause difficulties
  • Or scaling may add an unacceptable overhead
  • Drawbacks of fixed method dont always occur
  • Particularly on consoles - machine speed is
    constant
  • Many games use both methods for different game
    aspects
Write a Comment
User Comments (0)
About PowerShow.com