I use a separate Entity component that maintains position and orientation for the current and previous simulation step, separate from the Box2D rigid body. The main loop multiplies the elapsed time since the previous frame by the simulation turn rate and adds that to a turn accumulator. While the turn accumulator is greater than one, the main loop performs a simulation step. Each simulation step performs a Box2D world update and updates each Entity with the new position and orientation of each rigid body. The rendering step takes place every frame and uses the residual turn accumulator to interpolate the position and orientation of each Entity. The downside to this approach is that rendering will "lag" the simulation state by half a simulation step on average, though you probably won't notice it for simulation steps 1/30th of a second or shorter.
Once you've separated out simulation and rendering, it doesn't matter where your frame time comes from. I use SDL_GetTicks() and subtract the tick count from the previous frame, so the main loop doesn't make any assumptions about frame rate. The simulation update doesn't care about the frame rate at all, as it runs only when the accumulator says enough time has elapsed. You can run with or without vertical sync, and you don't have to worry about low frame rates slowing down your simulation. You'll want to clamp the elapsed time to something sensible like 100 milliseconds, especially if you plan to step through your code in a debugger.
Yeah, on regular first person shooters I play, I cannot play with Vsync enabled because it introduces about a frame or two of lag, and it completely throws off my accuracy.
How does the whole Vsync thing work though? Since you have the physics decoupled from rendering, the physics behavior is no longer framerate dependent, but you would still need to know what the refresh rate is, right?
I still have no idea how to force vsync in opengl, I guess I should do some reading up before I keep posting mindless questions.
I guess it depends on what kind of game you're playing, and how the game's update loop works. Ideally, it would read player input, perform all simulation, render everything, and then swap buffers. The latency should be half a frame on average, but that's not always how it works. Triple-buffering, SLI/CrossFire alternate-frame rendering, and graphics drivers accumulating several frames worth of updates all improve throughput but make latency worse. Disabling vertical sync masks the problem by allowing the frame rate to float free of the refresh rate, and (say) three frames of latency at 200 frames per second is not nearly as bad as three frames of latency at 60 frames per second.
I just measure how much time has elapsed since the last frame, so it doesn't matter what the refresh rate is. With vsync off, the refresh rate on my game can easily reach thousands of frames per second. 🙂
If you're using SDL, just use SDL_GL_SetAttribute(SDL_GL_SWAPCONTROL, 1) to enable vertical sync.
Nice. I got a working white square moving diagonally across the screen, and the VSync setting works great even in window mode. Of course if the video driver is set to force it off it runs at like 10,000 fps. All I'll need to do is decouple the physics from the rendering and I've solved this issue I could never figure out....
I'm learning things. This is good.
Hey, I have a quick question about this. What kind of behavior do I expect when the physics takes longer than the fixed timestep to calculate?
Say I have it set to 1/100 second timestep, but I spawn 500 boxes and all of a sudden the simulation slows down to 40fps.
I'm not sure what kind of behavior this will cause because the accumulator will have, say 1/50 second on the first frame, it will do two physics steps which take it 1/20 seconds of real time, and the next time around, the accumulator will have 1/20 seconds plus some, which will cause it to do five physics steps, which will make it take 1/4 seconds to do... this quickly becomes really bad. I think at some point my accumulator limiter will need to kick in. But I really don't like this because it creates an insane framerate drop instantaneously... Will I need a separate timer to see how much of real time the physics step calculations are taking and then deal with them accordingly, when they dip below realtime speed?
edit: solved my other question 😆
If your physics is taking longer to calculate than it runs for, then you are going to have to sacrifice something to deal with it. Cutting the graphical framerate is ineffective, as if the physics is really loaded it takes more than a second to calculate 1 second of physics, and the amount of time spent drawing is irrelevant. Cutting the physics framerate is plausible (as in, increasing your timestep to 1/40 in the example), but dangerous as it can lead to instabilities, and won't actually speed things up so much what with CCD. So the final option is allowing slowdown, which is what the accumulator does. Other than these, I don't know what you mean by "deal with accordingly".
I'm not sure you'd see a large drop in anything the moment the accumulator is hit. The system will already be crawling in your example by the time you hit the second frame. The way I see it, you are running behind schedule, and the limiter merely says "don't bother trying to catch up, aim for this easier target instead". Perhaps you've got your limiter set to too high a value?
Well, I don't mind the actual slowdown but would like to keep the simulation 100% stable at all costs, so that means if the computer is slower it should still do the same output except at a slower real-time speed.
Well what the accumulator does is it adds the deltaTime (change in real time since the last render) to itself each render and divides that by the physics timestep and applies that many steps... Currently I have my accumulator set to not add more than 0.03 seconds if the deltaTime is more than 0.03 seconds.... I'm wondering if I should change this value to be equal to the physics dt because otherwise it seems like it will instantly limit the framerate to 0.03 sec per frame.
Anyhow i dont think this is a big deal because its one of those things that trial and error seems to be good at solving.
While my usage patterns aren't necessarily typical, I've found that Box2D uses only a few milliseconds per step even with hundreds of objects. A large number of interacting objects takes significantly longer, but you can generally arrange for that to be rare. In my case, it only happens when several dozen objects pile up in one location and start bumping into each other.
Clamping the elapsed time to one or two simulation steps will produce "slowdown" when things start taking too long, but that will at least prevent simulation time from spiraling out of control. (I set the threshold to 0.1s to prevent runaway simulation after sitting in the debugger for a while, but your limit is probably saner.)