# Physics Engine Timing Suggestions?

This topic is 1932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, I am working on my own physics engine for a game that requires precise controls. The physics is handled in it's own thread separate from the rendering. My issue is making sure that my simulations run at the same speed on all systems. I have looked around and found a few posts relating to this, but as they use Box2D, some things aren't explained. I am particularly interested in the Semi-fixed timestep.

Related Posts :
http://gafferongames.com/game-physics/fix-your-timestep/
http://blog.allanbishop.com/box-2d-2-1a-tutorial-part-10-fixed-time-step/

Now, my system essentially works this way as of current. This is a pretty simplified explanation, but I have a list of Physical Objects, and a Queue of Pairs(or tuples) that have an objectID and a Vector (The mathematical vector). Each Pair represents a force. Then I have my collision handling code. (As I said, horribly oversimplified)

How have you guys solved this problem? Could you offer any clarification on what is explained in these posts?

##### Share on other sites
I don’t know what you mean by “semi-fixed”, because those articles are related to a purely fixed time-step, and a purely fixed time-step is necessary for a stable physics simulation.

My article on the subject might clarify some things: http://lspiroengine.com/?p=378

You basically have to divide your loop into 2 halves. One does logic and one does rendering. Although they are handled inside the same function, they operate at different intervals. My post explains how it works.

L. Spiro

##### Share on other sites
@L. Spiro Thanks for the links. I read through your post and I am still a bit confused. The interpolation part is where I get a little thrown off. How do we ensure that physics will update every 33.3 ms? From what I understand, looking at your graph we SHOULD be at 48.0 seconds, but we actually interpolate in between 0.0 and 33.3 in order to get our position at an exact 33.3 seconds of lag. If that is correct, then my only question is, how do we know that physics will run every 33.3 seconds? Also, when it comes time to render, who does the interpolation, the renderer, or the physics engine? I ask because my system is multi-threaded.

##### Share on other sites
In Spirou example you chop time at 33.333 ms, but your update goes not exactly at this time , intervals can be
28 36 33 34 29 38 ... but average will be = 33.333 ms and usualy there is no need to pass parameter to update function Update( 33333ULL ) if its your own engine;
To render smoothly you need calc interpolation variable alpha=0.0..1.0 to know how far you are to next update.
So engine gives alpha, renderer draws according to this alpha. Edited by serumas

##### Share on other sites
I don't understand how that could work though. I thought the point of this was to make sure that physics ticks at an exact interval.

##### Share on other sites
The gaffer article is quite good and not locked to a particular physics solution. Which part are you having trouble understanding?

##### Share on other sites

I don't understand how that could work though. I thought the point of this was to make sure that physics ticks at an exact interval.

It is not possible to fix something to an exact interval.

You are misunderstanding that the updates have to be spaced at every 33.33 actual real-life milliseconds.
In fact, inside the simulation, if you made 2 updates of 33.33 milliseconds each, but in 1 case they are done immediately back-to-back, 0.00001 milliseconds apart, and in another case they are done closer to real-world time at exactly 33.33 milliseconds apart, the simulation result is the same.
In either case, you just told the simulation to update 2 times, at 33.33 milliseconds each. How much actual time was between those updates does not matter to the simulation.

With that understood, that means you now understand that you can update any number of times within a single game loop. Meaning if the simulation falls behind real-world time you can catch it up by updating multiple times instead of just updating once by a longer amount of time.
Likewise, if too little time has passed since the last update (as in, fewer than 33.33 milliseconds) then you can update 0 times. Just move on.

The concept should become simple. Updates are always done at a fixed amount of time, and then in order to keep the simulation as close to real-time as possible, each frame you check to see how many times you should perform an update. If you are running at 60 FPS, the every second frame will update 0 times, and every other frame will update 1 time.
If you suddenly drop to 10 FPS, every frame you will update 3 times. This catches the simulation up to the current real-world time without allowing it to go beyond the current real-world time.

Also, when it comes time to render, who does the interpolation, the renderer, or the physics engine? I ask because my system is multi-threaded.

The renderer. Physics won’t updated at an interval fast enough to provide interpolations meant for the renderer. Since they are only meant for the renderer, they should be updated by the renderer.

L. Spiro Edited by L. Spiro

##### Share on other sites
Ok, I see what you are saying. So basically I set each Physical step to simulate a fixed amount of in-game time, which in this case is 33.3 ms. and I run as many of them per frame that is necessary to make sure it is caught up to current time. Then I take the difference between real-time and the next step-time, and use that difference to interpolate between the last 2 simulated times, to get the final rendered time? Does that make sense?

##### Share on other sites
What the renderer needs is not a time but a fractional amount from 0 to 1 representing the amount of interpolation to be applied between the object’s last and current transformations.

Also, if you are running these on different threads just for the sake of keeping them on different threads, you should put them back on the same thread. You will encounter both synchronization issues and performance issues otherwise.
This isn’t how multi-threaded rendering is supposed to work; this is a different type of decoupling.

L. Spiro

##### Share on other sites
Alright, taking it out of the thread is no problem. It really shouldn't be necessary anyway considering this is a 2D game using Shaders for all of my rendering, so rendering should be quick. So, I could essentially multiply my change in X by the percentage, and my change in Y by the percentage and that should be the translation that I actually apply to my geometry.

##### Share on other sites
I have one last simple question, and I believe I know the answer. I assume the safest way to handle passing the data from Physics to Rendering would be all at once in a structure. For example, have a PhysicsFrame class that holds the ObjectID, its transformation, and the Matrix of every changed object in the physics frame. Then pass that to the Renderer, and have it apply the percentage of interpolation between the new matrix and the old matrix per object. Does that make sense?

Thanks again for all the effort you guys have put into helping me.

##### Share on other sites
That would be superfluous.

The physics engine has nothing to do with rendering and should not prepare any information for the renderer. Any information passed between physics and rendering is intermediate. In other words the fact that there is a current and previous transform for interpolation is only because a layer was injected into the system specifically to bind physics and rendering together. Otherwise they would and should be entirely completely unrelated systems.

The physics system prepares only date related to physics, which in this case means the current transform of each object.
An intermediate layer stores the current transform into to the “lastTransform” for an object on each physical update.
And finally the renderer is run each frame using the above transforms with a time value to interpolate between them. The time value and the interpolation process are repeated for each frame. No information from the physics is prepared except for what the physics itself does: The current transform.

L. Spiro

##### Share on other sites

I have one last simple question, and I believe I know the answer. I assume the safest way to handle passing the data from Physics to Rendering would be all at once in a structure. For example, have a PhysicsFrame class that holds the ObjectID, its transformation, and the Matrix of every changed object in the physics frame. Then pass that to the Renderer, and have it apply the percentage of interpolation between the new matrix and the old matrix per object. Does that make sense?
I'm afraid not.
First, [font=courier new,courier,monospace]objectID [/font]or ids in general are typically unnecessary. We have far better tools which are pointers. Personally, I'm against most uses of IDs.
For Bullet, object transform is just poured out to the external code by using a proper interface. That is, if it's good enough for Bullet, I guess it will be good enough for you as well. Edited by Krohm