RK4 vs Euler integration (for physics in games) demo, GPL:ed C#/MDX app

Started by
10 comments, last by oggialli 18 years, 1 month ago
Most game programmers need to do some kind of physics simulation in their games. And afaik most use the Euler integration method, i.e.
  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. Features:
  • 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. Screenshot (click for larger picture) Known bugs: 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, remember?). Hopefully, this application will spread the usefulness of RK4 to game programmers. References: 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]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
Nice demo.

The explosion thing with RK4 is at least partially the slingshot effect that is also used to accelerate satellites.

Maybe I will add Dopri5 or Dop853, translating it to C# will be quite a lot of work though.

[Edited by - Trap on March 11, 2006 8:15:25 AM]
Your RK4-implementation is buggy, when Acceleration depends on absolute time your RK4-code does something wrong. The bug obviously doesn't manifest in your program as you always pass 0.0f as absolute time and your Acceleration is independent of absolute time.

Gaffers tutorial has the same bug.
I should have commented that 0.0f.

The thing is that the I wanted the RK4 implementation to be generic, so in case someones wants to use it for time dependant acceleration (i.e. in this case the mass of attractor masses changes over time) they can do so.

In this simulation however, the force that the attractor have on the pointmasses is independant of time, so therefore you can send any time you want.

So basicly my f in y'=f(t,y) looks like f(t,y) = k/y2 (i.e. independant of t).
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
The c-values in your RK4-implementation are all 0, RK4 should have c0=0,c1=0.5,c2=0.5,c3=1

eg:
Vector2 k4Acc = f(t, pos + dt * k3Vel, k4Vel);
should be Vector2 k4Acc = f(t+dt, pos + dt * k3Vel, k4Vel);
Ahaa, gotcha! Thanks for spotting it!

Before I update the .zip, you mean like this?
Vector2 k1Vel = vel;Vector2 k1Acc = f(t, pos, vel);Vector2 k2Vel = vel + 0.5f * dt * k1Acc;Vector2 k2Acc = f(t + 0.5f * dt, pos + 0.5f * dt * k1Vel, k2Vel);			Vector2 k3Vel = vel + 0.5f * dt * k2Acc;Vector2 k3Acc = f(t + 0.5f * dt, pos + 0.5f * dt * k2Vel, k3Vel);			Vector2 k4Vel = vel + dt * k3Acc;Vector2 k4Acc = f(t + dt, pos + dt * k3Vel, k4Vel);
EDIT: .zip updated
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Yeah.
I had an overlook of wikipedias explanation of the RK4. I have this question. Does the RK4 method also work with changing forces? Like if you attractor is moving.
did read this entire thread but RK4 doesnt matter unless your have things like springs (oscillations) or very large time steps. Otherwise it is just a waste of computational time. Most computer games do not need them IMO because you do not use springs (useful for a driving suspension for example).
Yeah, basic linear movement and rotation won't need RK4 over Euler at all. Also, for things needing more precision than Euler, there are other alternatives - Verlet, for example.
Olli Salli

This topic is closed to new replies.

Advertisement