# Framerate control and physics engines?

I am reading this article on how to control the framerate and physics calculations.

But in the game I am writing, I use a third party physics library and the only thing I do to update my physics is call a single function (runSimulation()).

Since I cannot do things like "interpolation" in the above tutorial, how can I make my game runnable in different hardware/at different framerates?

• Are you currently having any problems so far, and do you think the 3rd party physics simulator you are using isn't using all those techniques? (As far as possible) – Roy T. May 25 '12 at 9:26
• Actually in the physics engine source code ,it doesn't use any time variable for calculations – Ashika Umanga Umagiliya May 25 '12 at 9:51
• In that case it might be better to look for another (better?) physics library. Or maybe ask the developers there what they think. Or maybe they expect you to call the runSimulation step exactly every x-miliseconds. – Roy T. May 25 '12 at 10:52

Looks like the SteeringBehaviors library is determining the simulation time by calling System.currentTimeMillis(), so (unless you modify the library) your only option is to call runSimulation() at exactly the right time.

Consider spawning 2 threads:

1. Execute Thread 1 every time the display needs to be updated (e.g., by syncing to the display's refresh interval)
2. Execute Thread 2 every time you want to update the simulation. Make sure this thread executes at the same rate on different platforms (e.g., by setting it up to fire using a fixed-rate timer).

(And, of course, be sure that any data structures shared between those two threads (the locations of the particles, for example) are protected from thread collisions (e.g., using locks).)

That way, if the rendering thread stalls, or if the rendering speed differs between hardware devices, your simulation thread will continue to update the simulation at a constant rate.

• Using a separated thread is a good answer, but may be too overwhelm . – Gustavo Maciel May 26 '12 at 4:15
• Overwhelming in what sense? – smokris May 26 '12 at 4:20
• Almost all non-trivial physics simulations run in their own, fixed time rate threads just like @smokris describes above. – Patrick Hughes May 26 '12 at 5:19
• I don't think the OP is treating a non-trivial physics simulation. It's not overwhelming in sense of performance, but implementation. Getting threads very sync, and out of dead locks is not a trivial task. – Gustavo Maciel May 26 '12 at 15:56
• This is a pretty straightforward use of threads though. OP would just need to share the particle-location data structure between the two threads, which would just require a single lock. No risk of deadlock. – smokris May 26 '12 at 19:23