4

What update rate should I run my fixed-rate game logic at?

I've used 60 updates per second in the past, but that's hard because it's not an even number of updates per second (16.666666). My current games uses 100, but that seems like overkill for most things.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Tod
  • 4,618
  • 4
  • 32
  • 23
  • 5
    I think that's a typo...how can 60 updates per second be 16.666 updates per second? Perhaps you mean "60 updates per second is 16.666 milliseconds per update" – davr Sep 17 '08 at 22:00

9 Answers9

10

None of the above. For the smoothest gameplay possible, your game should be time-based, not frame-locked. Frame-locking works for simple games where you can tweak the logic and lock down the framerate. It doesn't do so well with modern 3D titles where the framerate jumps all over the board and the screen may not be VSynced.

All you need to do is figure out how fast an object should be going (i.e. virtual units per second), compute the amount of time since the last frame, scale the number of virtual units to match the amount of time that has passed, then add those values to your object's position. Voila! Time-based movement.

64BitBob
  • 3,110
  • 1
  • 17
  • 23
  • 4
    This often doesn't work well with (eg) physics. Many games (such as Half Life 2 for one) use a fixed timestep game-loop, and will run the game-logic multiple times in a single frame to cover the duration of the frame. –  Sep 17 '08 at 22:10
  • Mos: Open your NVidia or ATI control panel and find the setting for "Vertical Sync". Flip it to "off". Run Doom 3 and see if it still runs at an exact 60 FPS. – 64BitBob Sep 18 '08 at 03:39
  • 2
    Bob: Doom will draw frames as fast as it possibly can. But it's not running the game-logic every frame it draws, only interpolating the worldstate between the previous and current game updates. –  Sep 19 '08 at 19:26
  • Framelooking also works better on machines without multitasking such as consoles and DOS. The modern approach: http://gafferongames.com/game-physics/fix-your-timestep/ – Prof. Falken Oct 24 '12 at 07:08
8

I used to maintain a Quake3 mod and this was a constant source of user-questions.

Q3 uses 20 'ticks per second' by default - the graphics subsystem interpolates so you get smooth motion on the screen. I initially thought this was way low, but it turns out to be fine, and there really aren't many games at all with faster action than q3

I'd personally go with the "good enough for john carmack, good enough for me"

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
  • 1
    Do you really mean interpolate or extrapolate? Because, if the graphics subsystem interpolates, it always lags behind one frame. – Kenji Dec 11 '14 at 18:03
  • Yes, I mean interpolate. It does lag, however it uses a form of client side "prediction" so you don't really notice it – Orion Edwards Jan 11 '15 at 22:59
5

I like 50 for fixed rate pc games. I can't really tell the difference between 50 and 60 (and if you are making a game that can/cares you should probably be at 100).

you'll notice the question is 'fixed-rate game logic' and not 'draw loop'. For clarity, the code will look something like:

while(1) 
{ 
  while(CurrentTime() < lastUpdate + TICK_LENGTH) 
  { 
     UpdateGame(); 
     lastUpdate += TICK_LENGTH;
  } 

  Draw();
}

The question is what should TICK_LENGTH be?

Jeff
  • 1,043
  • 1
  • 8
  • 14
  • But then if the monitor is running at 60Hz then every 5th frame will be displayed for two frames instead of one, leading to a nasty jerking effect that video geeks will recognise as a 3:2 pulldown. –  Sep 17 '08 at 22:19
  • 1
    It's NOT how many FPS are drawn to the screen, it's how many times per second you run code for collision-detection, AI, and so on – Orion Edwards Sep 18 '08 at 00:16
1

Your engine should both "tick" (update) and draw at 60fps with vertical sync (vsync). This refresh rate is enough to provide:

  1. low input lag for a feeling of responsiveness,
  2. and smooth motion even when the player and scene are moving rapidly.

Both the game physics and the renderer should be able to drop frames if they need to, but optimize your game to run as close to this 60hz standard as possible. Also, some subsystems like AI can tick closer to 10-20fps, and make sure your physics are interpolated on a frame-to-frame time delta, like this: http://gafferongames.com/game-physics/fix-your-timestep/

alteveer
  • 190
  • 1
  • 4
1

Bear in mind that unless your code is measured down to the cycle, not each game loop will take the same number of milliseconds to complete - so 16.6666 being irrational is not an issue really as you will need to time and compensate anyway. Besides it's not 16.6666 updates per second, but the average number of milliseconds your game loop should be targeting.

Alexandra Franks
  • 2,982
  • 1
  • 19
  • 23
1

Such variables are generally best found via the guess and check strategy.

Implement your game logic in such a way that is refresh agnostic (Say for instance, exposing the ms/update as a variable, and using it in any calculations), then play around with the refresh until it works, and then keep it there.

As a short term solution, if you want an even update rate but don't care about the evenness of the updates per second, 15ms is close to 60 updates/sec. While if you are about both, your closest options is 20ms or 50 updates/sec is probably the closest you are going to get.

In either case, I would simply treat time as a double (Or a long with high-resolution), and provide the rate to your game as a variable, rather then hard coding them.

Guvante
  • 18,775
  • 1
  • 33
  • 64
1

The ideal is to run at the same refresh-rate as the monitor. That way your visuals and the game updates don't go in and out of phase with each other. The fact that each frame doesn't last an integral number of milliseconds shouldn't matter to you; why is that a problem?

1

I usually use 30 or 33. It's often enough for the user to feel the flow and rare enough not to hog the CPU too much.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
1

Normally I don't limit the FPS of the game, instead I change all my logic to take the time elapsed from last frame as input.

As far as fixed-rate goes, unless you need a high rate for any reason, you should use something like 25/30. That should be enough rate, and will be making your game a little lighter on CPU usage.

fabiopedrosa
  • 2,521
  • 7
  • 29
  • 42
  • Download the fpscmp02.zip file from this site to see the difference between 30 and 60 with your own eyes. http://forums.gameon.co.uk/showthread.php?t=9989 – moswald Sep 17 '08 at 22:15