Most game programmers need to do some kind of physics simulation in their games. And afaik most use the Euler integration method
position += velocity * dt;
velocity += acceleration * dt;
I have used this simple approach for a very long time myself. Recently I stumbled across an article saying that the Euler integration method sucks
and an integration method called 4th order Runge-Kutta (aka RK4) integration is much better
for non-trivial games.
I was interested in the difference in accurucy of these two methods so I have worked on an application to show the difference.
It is written in C#/Managed DirectX and is a simple app with the only purpose of integrating particles positions and velocities when affected by forces from attractor masses.
The test app can be downloaded from the Downloads section
of my site.
- GPL:ed source
- Clean source code
- Well commented code, easy to follow and modify
- Modular design, generic RK4 implementation
Here is a screenshot (more can be seen at the download page).
It shows blue particles (attractor masses) pulling red particles (Euler integrated particles) and green particles (RK4 integrated particles). At the start of the simulation all red and green dots were ordered in a grid moving with constant speed towards the attractor masses. As you can see, the Euler integrated particles has clearly slipped out of course, even though the timestep in this case is 1/60 seconds.
(click for larger picture)
Even though we use the RK4 integration, particles still "explode" when they come too close to an attractor. This is due to the fact that the particles have an acceleration of about 100000000 pixels per second2
very close to the attractors. (1/distance2
Hopefully, this application will spread the usefulness of RK4 to game programmers.
Gaffer's articles on physics programming in games
Mathematical version of RK4
If anyone have any questions or comments regarding the source or app, feel free to ask!
[Edited by - Enselic on March 12, 2006 4:50:06 AM]