# OpenGL and C++ 2D game vehicle physics

I found this online with regards to basic 2D vector physics: acceleration, orientation and friction. I was wondering if someone would be able to explain the flowchart and how it would be translated into code. Also explain why having the displacement as time-based is better than frame-based?

"The simplest way I know of to move an image across the screen over time is by using a static velocity and apply it to the the position of an image every time-step:

pos += vel * dt;

dt should be calculated every frame loop and passed to this equation, this way your displacement will be time-based instead of frame-based. To calculate dt you can use the following flowchart:"

Also explain why having the displacement as time-based is better than frame-based?

Depending on the complexety of your scenery the frame rate might vary. Lets say you set a constant frame velocity of 0.01. After 100 frames you have moved 1. If your framerate is 100fps you move that distance in 1 second. If it is just 50fps, it takes 2 seconds. I think you see the problem why you need to calculate your velocity depending on the time.

What the flowchart wants to tell you is the following:

At the beginning of each loop iteration, get a current timestamp (have a look into the STL chrono lib documentation). Then calculate delta_t by substracting the previous timestamp from the new one. Then you set the old timestamp to the new one, cause this value should be used during the next iteration of your main loop to calculate the next delta_t. You won’t need the value in this frame anymore. Finally update all your time dependent movements with the calculated delta_t.