Jump to content

  • Log In with Google      Sign In   
  • Create Account


Physics Engine Timing Suggestions?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Kurasu1415   Members   -  Reputation: 152

Like
0Likes
Like

Posted 03 October 2012 - 05:39 PM

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?

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 12275

Like
1Likes
Like

Posted 03 October 2012 - 06:55 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 Kurasu1415   Members   -  Reputation: 152

Like
0Likes
Like

Posted 04 October 2012 - 12:38 PM

@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.

#4 serumas   Members   -  Reputation: 710

Like
0Likes
Like

Posted 04 October 2012 - 01:34 PM

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, 04 October 2012 - 01:44 PM.


#5 Kurasu1415   Members   -  Reputation: 152

Like
0Likes
Like

Posted 04 October 2012 - 01:53 PM

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.

#6 amrazek111   Members   -  Reputation: 692

Like
0Likes
Like

Posted 04 October 2012 - 02:21 PM

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

#7 L. Spiro   Crossbones+   -  Reputation: 12275

Like
0Likes
Like

Posted 04 October 2012 - 02:32 PM

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, 04 October 2012 - 02:36 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 Kurasu1415   Members   -  Reputation: 152

Like
0Likes
Like

Posted 04 October 2012 - 02:54 PM

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?

#9 L. Spiro   Crossbones+   -  Reputation: 12275

Like
0Likes
Like

Posted 04 October 2012 - 03:01 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#10 Kurasu1415   Members   -  Reputation: 152

Like
0Likes
Like

Posted 04 October 2012 - 09:07 PM

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.

#11 Kurasu1415   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 October 2012 - 02:54 PM

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.

#12 L. Spiro   Crossbones+   -  Reputation: 12275

Like
0Likes
Like

Posted 06 October 2012 - 04:31 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#13 Krohm   Crossbones+   -  Reputation: 2961

Like
0Likes
Like

Posted 08 October 2012 - 12:56 AM

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, objectID 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, 08 October 2012 - 12:57 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS