Simulating Movement with Calculus
I have been creating a physics engine and am working on the movement mechanisms. I am making this short article to solidify what I have learned so far.
Physics simulations work by making multiple small predictions based on the laws of physics. These predictions are calculated using a mathematical technique called integration. This is a part of integral calculus where we try to calculate the area under a function curve. This can be done by calculating the area of the rectangles between time intervals. This is done multiple times for all time intervals under the function curve, and they are added together to get an approximation of the area under the curve. As you can imagine, the smaller the time intervals, the closer the approximation.
Now that we know what integration is, how does it correspond to simulating movement? To move an object we need three things: the acceleration, velocity, and position of that object. Acceleration is the rate of change for the velocity. By integrating it over time, we calculate the object’s velocity. The velocity is the rate of change for the position. By integrating it over time we get the object’s position. This process enables us to simulate movement. This is a type of integration called Euler Integration and it is a popular integration technique for simulating movement.
In the screenshot above you can see that we are using a variable called deltaTime. This represents the time interval of the calculation. The smaller the interval the more accurate the approximation.
As mentioned, this is the Euler integration technique. This is not the only integration technique we could use. Euler is one of the simplest and most popular integration techniques for game physics. It is accurate enough for most game applications. Another technique we could use is the Verlet integration technique. This technique provides better accuracy than the Euler technique and also uses less memory when simulating a large number of objects. The last technique I will bring up is the Runge-Kutta integration technique. This technique accumulates errors on the order of the fourth derivative, making it much more accurate than the Euler technique.