My port of the rigid body simulator from “Physics for Game Developers” (boat / plane / vehicle in fluid) starts to oscillate when a certain value gets very close to zero (steady state) and blows up. A smaller dt helps a little - I assume the Euler integrator to be the problem. So I’d like to try a different integrator. Unfortunately, the book does not show other implementations. “RK4” seems to be popular. There are some general implementations but to my embarrassment I lack the math and physics skills to apply them to the 3D simulation. So I need help. ![]()
I’ve found Integration Basics | Gaffer On Games that shows how to implement the algorithm for scalars (1D) and a spring-damper system example.
I had hope that I’d be able to implement the linear motion to get started (vectors only).
However, a key function looks like this:
float acceleration( const State & state, double t )
{
const float k = 15.0f;
const float b = 0.1f;
return -k * state.x - b * state.v;
}
Note it uses only the state (x:position, v:velocity) to determine the acceleration, but not t.
The calling function passed t+dt from its own parameters but does not explain where t originates. I assume it’s just there for completeness’s sake and would most likely be zero, since the simulation uses relative time dt.
In my simulation, acceleration is determined by constant mass and some forces (in addition to thrust) that depend on orientation and speed (drag, lift).
I don’t understand how I could calculate this for future steps, while I’m in the middle of calculating it for the next step.
I currently simply use the acceleration vector obtained previously, but I’m pretty sure that’s wrong.
Here is the JSFiddle:
I thought my initial attempt at the linear motion RK4 was somewhat working so I could post it, but it’s not doing anything plausible. Once it will, I’ll provide a fiddle.
Thinking ahead, the next step would be to implement it for the angular motion (inertia Matrix, rotation Quaternion) - I have no idea how
.
Any help appreciated!