Jump to content
  • Advertisement
Sign in to follow this  
Enselic

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

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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.

Share this post


Link to post
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).

Share this post


Link to post
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);

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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).

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!