4 ms·
The whole section on physics is particularly worth learning from carefully. Don't make your physics timestep depend on your rendering timestep. And if you wan
by JoshTriplett 10y ago
The whole section on physics is particularly worth learning from carefully. Don't make your physics timestep depend on your rendering timestep. And if you want to have any hope of reproducible results, don't make your physics have a variable timestep at all. Pick a reasonable quantum of time, run your physics engine on that fixed timestep, and render at whatever rate you want. If the graphics framerate is slower than the physics, repeat the physics step. If the graphics framerate runs faster than physics, just render the same frame repeatedly (or update things that don't interact with physics, like decorative elements).
- jstimpfle 10y agoFor a text scroller I once made this didn't work out well. A motion of constant velocity manifests worse jitters with a fixed timestep (if timestep * velocity > 1px). But scrolling text is a very special use case (and not physics) and probably nobody should want to scroll text at 60Hz/100dpi/arbitrary velocity...
- Strilanc 10y agoOne way to avoid that problem is to use interpolation between two frames computed by the physics engine.
- JoshTriplett 10y agoThat can work, but take care to avoid making the controls feel laggy in that case: if the player feels like their input doesn't immediately take effect, such as because the input gets processed on a physics timestep but the player character kept moving through interpolation, the controls can feel "floaty".
- cpeterso 10y ago> run your physics engine on that fixed timestep, and render at whatever rate you want. I think that works well for parallelizing game subsystems, too. Physics and the renderer can run at their own rates on different cores and then publish (read-only) global snapshots of their state for other subsystems to consume.
- JoshTriplett 10y agoTrue. And that's not too hard a version of parallelism to handle: never access the same data at the same time, just run physics on frame N to produce frame N+1 while rendering frame N, and don't publish frame N+1 until after physics finishes, at which point physics never writes to it again.
- Mithaldu 10y agoIn theory that sounds nice and all, however in practice this leads to jitter, since the delta between the time the physics were done, and the time the frame is shown, keeps changing. In the worst case it skews slowly over time, leading to very ugly frame skips when the counter "flows over", despite the system running at full framerate. In practice you must always have at least a smaller version of your physics engine, that your rendering loop needs to run before actually creating the frame, to do one of these two things: - interpolate between the last and the previous physics state, accepting a constant additional latency between input and result - extrapolate forward from the last physics state, accepting that what is displayed will occasionally be slightly wrong for a frame Both of these come with their own problems, which may be bad enough to completely invalidate their use depending on whatever game type they're being used for. So even if you decouple and parallelize your loops, you end up with 3 ways to handle the result, neither of them being without disadvantages. There are no silver bullets.
- ball_of_lint 10y agoWould there be any big issues to rounding the framerate to the nearest number evenly divisible by the physics rate on the fly? I'm meaning run it mostly like how he does but allow any framerate and eat the small changes in speed?
- jstimpfle 10y agoMonitor framerates are kinda "fixed". That is, there is no protocol to seamlessly change the framerate.
- forrestthewoods 10y agoRendering the same frame multiple times is terrible. Standard solution is to interpolate position between two known physics states. This means physics is one tick behind in the normal case. There's a clever approach I read recently. It's to trrat physics like fighting games test netcode. Extrapolate physics (usually a huge no no) and fix up in the unusual case that it's wrong. Now that 144Hz and even 165Hz monitors are common I expect to see more experimentation with this type of thing.
- taneq 10y agoI think we tend to see extrapolation as a no-no because we're thinking in terms of hundreds of milliseconds. If you know the physics of your system quite well and your interval is small enough and regular enough you can get a significant boost from extrapolation. More: https://developer.oculus.com/blog/the-latent-power-of-prediction/ https://developer.oculus.com/blog/the-latent-power-of-predic...
- JoshTriplett 10y ago> I think we tend to see extrapolation as a no-no because we're thinking in terms of hundreds of milliseconds. Exactly. Doing physics at 30Hz and rendering at 60Hz seems like a bad idea. Doing physics at 60Hz and rendering at 120Hz seems completely fine.
- Kiro 10y ago> Extrapolate physics (usually a huge no no) Why is it usually a huge no no?