# A deterministic game loop

I want to make a game loop that is deterministic sort of like a physics engine but it's for game update and rendering.

I've done a lot of reading and I still was a bit confused.

The one that made the most sense was the final article about multiply delta time or not.

I want to have fixed time step so I will design my games to fit in either 30 or 60 fps. So if I target 60fps all logic needs to be done in 0.166ms on a pc but if I am making an app that runs on mobile 0.33ms.

With those ideas in mind how would I setup the game loop that's deterministic but also frees the renderer but I am unsure how to go about doing the rendering loop? Would I interpolate the position before rendering?

• What you'd want to read is how to do a basic game engine with two threads, one for the logic, and one for the rendering. Commented Jul 1, 2015 at 13:26
• what do you exactly mean by deterministic? Commented Jul 1, 2015 at 13:55
• A deterministic simulation is a simulation that runs the same way every time. In the usual game engine (non deterministic), you multiply movement by frame time deltas and also use random numbers that are different each run. A deterministic simulation makes it so that these and other "random" factors don't impact the simulation. This is useful for game replays, and also for networked games such as RTSs where there are a lot of units all following deterministic logic on all players machines, so you don't need to communicate their actions over the network. Only player actions get transmitted. Commented Jul 1, 2015 at 14:10
• Could you describe what you've tried? It would demonstrate your level of understanding and hopefully make this question more focused.
– Anko
Commented Jul 5, 2015 at 9:51

Your rendering and deterministic simulation don't have to run at the same frequency

If you fall behind, you can run the simulation multiple times between renders.

If you get ahead, you do multiple renders between simulations and use interpolation of the values in your renderer.

If you fall too far behind you can see that you might fall into a death spiral where you can't simulate fast enough to catch up to "real time". In this case, your best bet is to probably just let it fall behind.

If it's a networked game, this might be problematic but if it isn't, this is fine, and doesn't break the determinism of course (:

• This makes sense. I think that I will set a delta time with a fixed time step in a inner loop, a variable outer loop time step that can do non deterministic type things such as background animations, particle effects etc. The question now moves towards the rendering. What if I'd like to have two rendering modes. One that's locked to sync for mobile devices and one that can run free of platforms that are plugged into the wall or pc. How would I implement something like that? Commented Jul 1, 2015 at 16:21
• BTW if you intend on having the sim deterministic across many device types you are going to hit some issues with floating point math. You are probably going to want to use fixed point. Commented Jul 1, 2015 at 16:25
• There's a couple ways to handle what you want to do. Assuming you know how to code each behavior independently, one way is to have a bool setting that you can set to true or false to make it work one way or the other. This is useful because then you can make it lock/sync on pc for easier debugging. If however, you need to strip the extra code that the pc uses out for the mobile version, to decrease exe size or whatever else, using a #ifdef or a separate file per platform is the way to go. Commented Jul 1, 2015 at 16:28
• Can you provide more information about the fix point stuff you referred to earlier? The code is modular, for example I can do getRenderer which returns a pointer to the currently loaded renderer, software, OpenGL, directx so the coding part isn't that important. main code calls to void pointers which will do the right thing. The main point that I am having trouble with is the loop and the rendering because on mobile there's no need to render at 60fps but it could be a setting if someone wanted it while on the desktop it would default to 60 but could be turned down if needed. Commented Jul 1, 2015 at 17:49
• I'd ask a new question about deterministic simulations and fixed point vs floating point so you could get deeper details and a wider range of opinions. Commented Jul 1, 2015 at 18:06