# RK4, How to apply non-constant forces

## Recommended Posts

CombatWombat    673
I have RK4 setup and working for my simulation. It's just a bunch of boxes floating in space at the moment.

Some of the forces I must apply to these boxes can be constant from t->t+dt. IE: Thrusters that push the blocks around, etc.
However, I foresee that some forces will have varying value across duration of the time step. I *could* just accumulate all forces into a value and treat them as though they were constant, however this seems to throw away the entire purpose of using RK4, since the idea (i thought) was to sample curvature in the derivatives (force, in this case) in a few places.

So, my question then, is what are some ways to structure a simulation such that I can pass forces that may be varying for whatever reason into my integrator? Is it even worth it to do this?

Some things I have thought of (but not thought out very well...)
1) Create a "Force" object, which knows how to calculate the value of itself given a _state_ object, a time, and a time step. However this does not seem enough, since it seems like such an object might need to be passed the state of the entire simulation and not just the state of the object on which it acts. IE: if that force depends upon the location of another body (gravitational pull, maybe).

I had hoped typing this out would help me think of more ideas. So far, not so good...

##### Share on other sites
alvaro    21263
RK4 doesn't impose any conditions on how your forces vary over time. If you set up your simulation as an initial value problem, RK4 just works. So I guess I don't understand what it is you are having trouble with. What's the simplest scenario you don't know how to handle?

##### Share on other sites
CombatWombat    673
My understanding was that RK4 would not be much more stable than simple Euler if I did not allow the integrator some means to sample the time varying force at multiple points during the timestep.

As an example: suppose I had gravitational forces between rigid bodies.
The gravitational forces vary with position of the objects, and thus will not be constant over dt. If I simply sum my forces before the integrator is called, it will not be aware of this and there will be error at the end of the integration step, possibly accumulating as time goes on.
Similarly if there exist spring/damper forces between rigid bodies, these vary with time and can be notoriously unstable.

In [url="http://gafferongames.com/game-physics/integration-basics/"]GafferOnGames: Integration Basics[/url] he states [quote]What you write here of course is completely simulation dependent, but it is crucial that you structure your simulation in such a way that it can calculate the acceleration or force derivatives completely from inside this method given the current state and time, otherwise your simulation cannot work with the RK4 integrator.[/quote]

This is fine for a small demo with one or two things happening, but I do not see a practical way to approach this for a larger open-ended simulation with perhaps one hundred objects which may be created/destroyed at any time.

So what I am after, is a discussion of some architectural ways to structure the physics/forces to make this possible.
Do I pass the entire world state into the integration, integrate part 1 of RK4 for THE WHOLE WORLD STATE...then part 2 for the entire world state...etc...This seems nice as positional dependent forces can be recalculated at each step.
Do I pass individual object states in and integrate them one at a time, looping until all objects are taken care of? This seems more common in the samples I've seen, but seems more awkward.

Maybe this belongs in general game programming since its a structural problem?

Thanks for the response.

##### Share on other sites
alvaro    21263
[quote name='CombatWombat' timestamp='1308006498' post='4822966']
Do I pass the entire world state into the integration, integrate part 1 of RK4 for THE WHOLE WORLD STATE...then part 2 for the entire world state...etc...This seems nice as positional dependent forces can be recalculated at each step.[/quote]
Philosophically, that is the correct way to go about it.

[quote]Do I pass individual object states in and integrate them one at a time, looping until all objects are taken care of? This seems more common in the samples I've seen, but seems more awkward.[/quote]
If you can divide your scene into individual objects that mostly don't interact with each other (e.g., on a flight simulator), you can integrate them one at a time, and on the rare occasion when they interact with each other (two planes collide) just fudge a force that is not technically correct but will look just fine.

Now the disclaimer: I haven't implemented any Physics simulations beyond some simple toys, so perhaps there is some industry standard on how to do this and I just don't know about it.

If I were in your situation, I would study the APIs and documentation of some Physics libraries. You can probably figure out how they are dealing with these things from the interface they present to the programmer.