Physics Engine Timing Suggestions?

Started by
11 comments, last by Krohm 11 years, 6 months ago
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?
Advertisement
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

@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.
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.
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.
The gaffer article is quite good and not locked to a particular physics solution. Which part are you having trouble understanding?

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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?
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

This topic is closed to new replies.

Advertisement