re-record frame dependence section
continuous-integration/drone/push Build is passing Details

main
Evan Hemsley 2020-07-21 13:57:41 -07:00
parent 67a241a7d1
commit 52665f4b1a
3 changed files with 28 additions and 7 deletions

View File

@ -8,27 +8,33 @@ Let's do a little experiment. In the PongFEGame constructor, add the following l
```cs
...
graphics.SynchronizeWithVerticalRetrace = false;
...
Window.AllowUserResizing = true;
IsMouseVisible = true;
IsFixedTimeStep = false;
...
```
This means that the game will now run with an *unlocked timestep*. In other words, the game will update as fast as the computer allows us to. By default, FNA tries to make the game update at a rate of 60 times per second.
This means that the game will now run with an *unlocked timestep*. In other words, the game will update as fast as the computer allows us to. By default, FNA tries to make the game update at a rate of 60 times per second, and synchronize with the monitor's refresh rate.
Now what happens if we run the game?
![oh dear](/images/oh_dear.gif)
Oh dear. That doesn't seem right at all.
Oh dear. That doesn't seem right at all. The paddle is moving way too fast. But our speed value is only 10. What's going on?
The paddle is moving way too fast. But our speed value is only 10. What's going on?
Remember when I mentioned *frame-dependence* and *delta-time* earlier? This is a classic example of frame-dependence. What's our framerate right now?
Remember when I mentioned *frame-dependence* and *delta-time* earlier? This is a classic example of frame-dependence. Notice that the FPS, or frames-per-second, of the game is around 500 in the above recording. Our motion message when we press the "up" key on the keyboard tells the paddle to move 10 units.
```cs
System.Console.WriteLine(1 / gameTime.ElapsedGameTime.TotalSeconds);
```
That means every frame we have the Down key held down, the paddle is moving 10 units. Which means, as things stand right now, the paddle is moving about 5000 units per second. And if the framerate changes for some reason, the paddle will move slower or quicker. This means the actual progress of the simulation will be completely different on slower or faster computers.
When I run the game, my console shows that the game is fluctuating at around 19000 frames per second. Our motion message when we press the down arrow key on the keyboard tells the paddle to move 10 units.
That means every frame we have the Down key held down, the paddle is moving 10 units. Which means, as things stand right now, the paddle is moving about 190,000 units per second. And if the framerate changes for some reason, the paddle will move slower or quicker. This means the actual progress of the simulation will be completely different on slower or faster computers.
Even if you use fixed timestep, what if you want to lock the timestep to 30 later? Or 120? The entire behavior of your game will change.
@ -52,3 +58,18 @@ Let's go back to the line of the InputEngine where we send the MotionMessage.
Our simulation is now *frame-independent*, which is what we desire. The paddle is definitely moving too slowly now, but that's something we can fix with a bit of value tweaking.
It is very important that you take care to multiply things that change over time by the delta-time value, or you risk elements of your game becoming frame-dependent.
Before we move on, let's reset the values we changed in **PongFEGame**.
```cs
```cs
...
graphics.SynchronizeWithVerticalRetrace = true;
...
IsFixedTimeStep = true;
...
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB