Followers 0

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

## 11 posts in this topic

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. (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]
0

##### Share on other sites
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]
0

##### Share on other sites
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.
0

##### Share on other sites
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).
0

##### Share on other sites
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);
0

##### Share on other sites
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
0

Yeah.
0

##### Share on other sites
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.
0

##### Share on other sites
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).
0

##### Share on other sites
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.
0

##### Share on other sites
Quote:
 Original post by ZyberantI 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.

I haven't tried this, but what you could in principal is to let f(t,y) depend on t in such a way that the attractor moves. I have no experience in building a full scale physics engine, so I have no good idea of how this would be implemented.

Quote:
 Original post by Anonymous Posterdid 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).
Quote:
 Original post by oggialliYeah, 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.

Good points. For linear movement RK4 is overkill. For one level more complex physics simulations though, Euler is not good enough. I wanted to provide a customizable demo so people could quickly get a feel for the difference in accurcy between these two methods.
0

##### Share on other sites
Quote:
 Original post by ZyberantI 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.

Yes. However, it's not perfect, but A LOT better than Euler, at the very least. The whole point of higher order numerical integration methods is to follow the curvature (change) in the integrated function too. And even higher-order methods also track the change of the change of the change of the change... mathematically speaking, higher order derivatives of the function in question.
0

## Create an account

Register a new account