Doing gravity right Now you may be thinking: "baah, I know how to do gravity.." but there's a big flaw in the commonly used Euler's method to handle the gravity (or other forces). Even all the Quake games have this problem. Then what is it? If you have more frames per second in Quake, your player will run faster and jump higher. There are some places in Quake where you can't jump high enough if you don't have enough frames per second. Sounds odd, right? Most of the programmers do the gravity something like this: velocity = velocity + gravity*delta_time position = position + velocity*delta_time The algorithm above is ok but when delta_time changes or delta_time is too high, it causes many unwanted problems. Gravity adding should actually be done like this: velocity = velocity + gravity*delta_time/2 position = position + velocity*delta_time velocity = velocity + gravity*delta_time/2 That's it.. but you need proof, right?
Results in practice Here is a "jumping" parabola with different delta times, using the bad algorithm. The picture on the right corresponds almost exactly to Quake. As you can see, you can't jump in Quake if you have less than 3 fps. (Illustration showing changes in the arc of a jump depending on framerate) As delta time (dt) gets higher, the jumping curve gets lower. Now here are results with the new algorithm: (Illustration showing the arc of a jump staying the same) Quite nice, don't you think? Accelerating physics are no longer approximation! Remember that you should do all the accelerating forces like described above, not just gravity.
Integrating to the same results The proving of the formula can also be done using integration (of course, because you are calculating areas..) After integrating you'll get this formula: pos = pos + 1/2*acc*dt^2 + vel*dt vel = vel + acc*dt Note that pos = position, vel = velocity, acc = acceleration and dt = delta_time And optimizing that formula leads to this: temp = acc*dt pos = pos + dt*(vel + temp/2) vel = vel + temp This equals to the second formula given at the beginning of this document.
I dug up this very helpful old article on the internet archive that I think explains the problem and solution extremely clearly using the way Quake handles gravity as an illustration
web.archive.org/web/20160719...