8
\$\begingroup\$

Gafferon Games has a great article on RK4 Integration for building physics simulations which can be found here: Integration Basics

Personally my mathematics and physics knowledge could use improvement. I feel comfortable within the realm of vector mathematics, trig, some stats (I've had to use linear line regression formulas for software, etc. ), and basically most things high school level to first year college.

Now to the question, I've read this article, downloaded the associated source and debugged line by line to try and get an understanding of what is happening and still feel like I'm clearly not getting what I'm looking at. I've searched the internet trying to find the "For Dummies" versions, frankly I learn a little differently and staring at formulas all day with the emphasis on memorization isn't going to cut it as I need to understand what is happening so I can be flexible applying it.

So here's what I think I understand so far, but I am hoping someone else can clarify or completely correct me. The RK4 uses a Euler step, then bases that to move forward in time to calculate several more essentially Euler steps(?) and determines using a weighted sum what the best position and velocity is for the next frame?

Furthermore that acceleration method (converted into AS3):

private function acceleration(state:State, time:Number):Number
{
    const k:int = 10;
    const b:int = 1;
    return - k*state.x - b*state.v;
}

takes a constant mass(10) and force(1)? and returns some weird calculation I have no idea why...-mass * position - force * velocity? what?

Then for my last bit of confusion, in the evaluate methods which look like (AS3):

private function evaluateD(initial:State, time:Number, dtime:Number, d:Derivative):Derivative
{
    var state:State = new State();
    state.x = initial.x + d.dx*dtime;
    state.v = initial.v + d.dv*dtime;
    var output:Derivative = new Derivative();
    output.dx = state.v;
    output.dv = acceleration(state, time+dtime);
    return output;
}

We store a new state with the time step, then set a derivative to return...I sort of understand this as it's used in the approximation process, but what is this!:

output.dx = state.v;
output.dv = acceleration(state, time+dtime); 

// ok I get we are getting the new velocity since v = a * t, obviously I   
// don't what acceleration() is returning though. 

We set the derivative output change in position to the states new velocity? Huh?

Lastly this test simulation runs doing this:

var state:State = new State();
state.x = 100;
state.v = 0;

t = 0;
dt = 0.1;

while (Math.abs(state.x)>0.001 || Math.abs(state.v)>0.001)
{
    trace(state.x, state.v);
    integrate(state, t, dt);
    t += dt;
}

So we are setting a new state with a positional value of 100, and a velocity of 0? What is the point of this simulation if we have no velocity...

Anyway, needless to say I'm pretty confused and have drifted off planet Earth. Hoping someone out there can clarify this for me.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If games is what you're after, RK4 is an overkill and, due to its cost vs. stability ratio, not the best choice either for those sudden and sharp changes in your acceleration. If you're looking for a discussion on which type of integrators are worth choosing from, as well as how to build a rather simplistic simulator, I can recommend a technical report on exactly these problems: arxiv.org/pdf/1311.5018v1.pdf \$\endgroup\$
    – teodron
    Commented Feb 2, 2014 at 12:51
  • 1
    \$\begingroup\$ Interesting, I'll grab some coffee and read that over! Personally, I'm interested in understanding as much as I can from basic to advanced simulation. I've made quite a few very basic ones currently, but this is purely a quest for knowledge so I can further my flexibility as a developer. Thanks for the reference, greatly appreciate it! \$\endgroup\$ Commented Feb 2, 2014 at 14:47

1 Answer 1

7
\$\begingroup\$

RK4 is an example of a numerical integrator. Euler integration is a similar concept, but it is much less precise. Numerical integration is not exact, but much better for a computer to handle in a real-time situation such as a game. The reason that you use RK4 instead of Euler is that RK4 takes into account the integration of the second and third derivatives (acceleration and jerk), and thus fits the analytical solution much better.

RK4 is essentially a Taylor series expansion of the differential equation that defines acceleration with respect to displacement and velocity. This allows you to integrate forces that depend on these quantities, such as in constraints and even universal gravitation. Taylor series expansions are useful in programming as computers can evaluate them very efficiently.

The acceleration function used is an example is a simple damped spring system, not gravitational. k is Hooke's spring constant, and b is used to damp the system (remove energy). For pretty much all spring-related constraints in your engine, you will want to damp them as numerical errors can cause a huge accumulation of energy, causing the simulation to explode. If you were using Euler integration, this would be much worse.

With regards to acceleration(), a more complete physics engine will calculate both linear and angular accelerations based on forces (torques). The forces to sum could include gravity (constant or based on Universal Gravitation), buoyancy, and springs (most constraints can be modeled using stiff springs).

Your third question is easy to answer. In kinematics, there are three basic quantities: displacement (position), velocity, and acceleration. Acceleration is the derivative of velocity, which is the derivative of displacement (both with respect to time). A derivative is just the rate at which something changes. Derivative.dx means "the derivative of State.x".

Because the simulation assumes that the spring is anchored at the origin and has a rest length of zero, a displacement of 100 means that the particle will start to oscillate. The test simulation ends when the particle is not moving and is close to the origin.

\$\endgroup\$
4
  • \$\begingroup\$ Another description is that you can think of RK4 as a taylor expansion to the differential equation. \$\endgroup\$
    – RandyGaul
    Commented Feb 2, 2014 at 0:15
  • \$\begingroup\$ This is a great answer and really exposed some holes in my physics knowledge. Can you provide some references to a good starting place for understanding what a damped spring system is, torques, etc? I believe I'm definitely lacking in these more advanced mechanics. Your answer really clarifies a lot for me, especially as I just recently started into Taylor polynomials. \$\endgroup\$ Commented Feb 2, 2014 at 0:43
  • \$\begingroup\$ I'd try here: khanacademy.org/science/physics \$\endgroup\$
    – jmegaffin
    Commented Feb 2, 2014 at 0:45
  • \$\begingroup\$ Perfect, khanacademy was where I was thinking of. \$\endgroup\$ Commented Feb 2, 2014 at 3:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .