I have been looking around online for a while now for methods of integration for a physics engine I am trying to code for fun (gotta love the nerdiness there :P). I have found Euler’s method, RK4, and Verlet (as well as the time corrected version). I have also been trying to come up with some of my own methods. I was wondering if you knew of any others that you found intuitive or helpful. Thanks.
EDIT: Thanks for all of your help so far. As for clarification: perhaps I do mean numeric integration. Surprisingly, in all my research, I have not found so much as the technical NAME for what I am trying to do! Perhaps describing my specific problem will make my question more clear. Lets say I want to simulate a ball moving through a circular (or spherical once I implement 3d) gravitational field. This ball will encounter force vectors which can be used to calculate a corresponding acceleration vector for the point the ball is at on that specific tick. From your physics class, you know that velocity = acceleration * time but my problem is that the ball is technically on that point for only an instant, represented mathematically in calculus by dt. Obviously, I cannot use an infinitesimally small number in C++ so I must approximate the solution using methods of instantaneous integration (a term I heard in some reading but I could be completely wrong about) or what you think is called numerical integration (you are probably right so I changed the title).
Here is my (successful) attempt at implementing the Euler method of numerical integration:
//For console output. Note: I know I could just put "using namespace std;" but I hate doing that.
#include <iostream>
using std::cout;
using std::system;
using std::endl;
//Program entry
int main (void)
{
//Variable decleration;
double time = 0;
double position = 0;
double velocity = 0;
double acceleration = 2;
double dt = 0.000001; //Here is the "instantanious" change in time I was talking about.
double count = 0; //I use count to make sure I am only displaying the data at whole numbers.
//Each irritation of this loop is one tick
while (true)
{
//This next bit is a simplified form of Euler's method. It is what I want to "upgrade"
velocity += acceleration * dt;
position += velocity * dt;
if (count == 1/dt) //"count == 1/dt" will only return true if time is a whole number.
{
//Simple output to console
cout << "Time: " << time << endl;
cout << "Position: " << position << endl;
cout << "------------------" << endl;
system ("pause");
count = 0; //To reset the counter.
}
//Update the counters "count" and "time"
count++;
time += dt;
}
return 1; //Program exit
}
Because the acceleration is constant and this differential is actually solvable (why I am using it to test, the solution is position = time ^ 2, this is fairly accurate but if you make it a bit more complicated by, for example, making the acceleration change over time, the algorithm loses accuracy extremely rapidly. Again, thanks!
You have a second order differential equation (ODE) x”=f(x,x’,t). x can be a vector and x’ and x” are the first and the second derivative with respect to the time. In your case x is the position, x’ is the velocity and x” is the acceleration. Usually one transforms this second order ODE into a first ODE by introducing X=x,Y=x’ and you obtain
X’=Y
Y’=f(X,Y)
Then you can use the classical schemes for solving ODEs like Runge-Kutta, Dormand-Prince, Adams-Bashforth, …
Many of these methods are implemented in odeint which is quite easy to use.