Quantum Caverns Devlog

The Importance of Delta Time (Part 3)

Graph Manipulation

Before we can address our bug, we must first understand what each axis of the graph represents in the context of our code. If this was your average textbook physics problem then the y axis would probably represent distance in meters and the x axis would represent time in seconds. In the context of our code, the y axis still measures distance and the x axis still measures time, but the units are not meters and seconds respectively. Instead we are working with pixels as a unit of measurement and frames as a unit of time.

Now that we have the correct units, we can begin to analyze the graph. The maximum value of the position function occurs at the point (10, 500), and the function crosses zero for the second time at (20, 0). Therefore, the player will jump 500 pixels into the air in 10 frames, and then fall another 500 pixels to complete the jump in a total of 20 frames. If the game was running at 60 fps then 20 frames would amount to roughly 0.333 seconds. If the game was running at 30 fps then 20 frames would amount to roughly 0.667 seconds. In other words, the faster the frame rate the faster the jump is completed.

Before we continue, let’s just visualize this.

(The slider on the bottom represents the frame rate. Also don’t forget that 1 / frame_rate is how to calculate delta time).

As the frame rate increases the maximum value of the position function decreases. In other words, the higher the frame rate the lower the player jumps. This explains the player’s behavior in the very first two GIFs in Part 1.

In Part 1 I explained that simply multiplying horizontal movement by delta time creates consistent movement across different frame rates. This works because there is no acceleration in the x direction. Our jump code does have acceleration, gravity in this case, so using delta time gets a little tricky.