Game Development Reference
In-Depth Information
while game is running
realDeltaTime = time since last frame
gameDeltaTime = realDeltaTime * gameTimeFactor
// Process inputs
...
update game world with gameDeltaTime
// Render outputs
...
loop
Although it may seem like a great idea to allow the simulation to run at whatever
frameratethesystem allows, inpractice therecanbeseveral issueswiththis.Most
notably, any game that has even basic physics (such as a platformer with jumping)
will have wildly different behavior based on the frame rate. This is because of the
way numeric integration works (which we'll discuss further in Chapter 7 , “ Phys-
ics ”), and can lead to oddities such as characters jumping higher at lower frame
rates. Furthermore, any game that supports online multiplayer likely will also not
function properly with variable simulation frame rates.
Though there are more complex solutions to this problem, the simplest solution
is to implement frame limiting , which forces the game loop to wait until a target
delta time has elapsed. For example, if the target frame rate is 30 FPS and only
30ms has elapsed when all the logic for a frame has completed, the loop will wait
an additional ~3.3ms before starting its next iteration. This type of game loop is
demonstrated in Listing 1.3 . Even with a frame-limiting approach, keep in mind
that it still is imperative that all game logic remains a function of delta time.
Listing 1.3 Game Loop with Frame Limiting
Click here to view code image
// 33.3ms for 30 FPS
targetFrameTime = 33.3f
while game is running
realDeltaTime = time since last frame
gameDeltaTime = realDeltaTime * gameTimeFactor
Search WWH ::




Custom Search