• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Enselic

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

Share this post


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

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).
0

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);
0

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
0

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

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Zyberant
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.

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 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).
Quote:
Original post by oggialli
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.

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 this post


Link to post
Share on other sites
Quote:
Original post by Zyberant
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.


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0