What do you do if your gameUpdate() method can't update fast enough?
Members - Reputation: 568
Posted 05 September 2012 - 05:33 AM
I realized that my game isn't updating quick enough, and causes the game play to stutter. The more objects, the heavier stuttering it gets. Below is a picture of me benchmarking my game loop. "Start" means the CPU has entered the game loop, and "Finish" means the CPU has left the game loop.
I have utilized two concurrent threads. One is for rendering the objects onto the screen, and the other is for updating the objects' properties. I only benchmarked the updating thread, and not the rendering thread.
I find that there's a noticable, average lag of 0.07 seconds. That's enough for the game to stutter, since I can easily see it stutter.
I am now pondering what techniques the more experienced developers did to mitigate this issue? Thanks in advance.
Senior Staff - Reputation: 17925
Posted 05 September 2012 - 06:02 AM
If there's a visible stutter, you should also check your rendering (or the interaction between your rendering and logic threads) are correct before rushing to blame the update logic and potentially wasting time on needless optimization.
(Posted from mobile.)
- Jason Astle-Adams.
Moderators - Reputation: 29302
Posted 05 September 2012 - 06:39 AM
You need to keep track of the number of milliseconds-per-frame over the course of development. Whenever it goes over 33 (bigger than 1000/33ms = less than 30FPS), then you're in trouble and it's time to either optimize your code or cut back on features.
Regardless of what language any programmer is using, they would have to come to some point in the development stages where their game would not update fast enough.
0.07s is 70ms, which is way over budget; that's less than 15FPS. You need to profile your update code and find out which parts of it are eating up all that time. Then, once you've found the culprits, you need to figure out how to optimize or eliminate them.
Edited by Hodgman, 05 September 2012 - 06:41 AM.
Prime Members - Reputation: 2034
Posted 06 September 2012 - 12:42 PM
some things you can skip, some things you can do every 2nd or 10th or 100th frame
if you time things correctly: let's say you have a double max_time is 0.0125 or 0.012 for decent margin,
then your gameupdate() function should do all the _absolutely_ necessary work _first_, and when it runs out of time - exit immediately
the small margin will help you avoid stuttering because there are functions in gameUpdate() that could take some time to do
i hope this helps, because i had to find this out myself, but i learned alot from it =)
don't be afraid to frameskip!
Prime Members - Reputation: 2034
Posted 06 September 2012 - 12:47 PM
there are some annoying functions in any game that just takes time - pathing, raycasting, AI, physics
to avoid getting run over by cpu time, you could do any number of cheap things like only doing pathing for 4 and 4 'NPCs' at a time
or if its critical work - see if you can't get away with doing half the work this frame, and the other half next frame
sometimes it can even be unnoticable if you are a frame late, or run 'something' (anything) at a different framerate!
other things to consider are - things that are very far away from the camera don't need to be as 'fine' or 'smooth', so you can do less there
but it's generally harder to accomplish...
in my case i would progressively 'frameskip' depending on distance from camera any AI or pathing
Members - Reputation: 429
Posted 07 September 2012 - 01:53 AM
Profiling is key at every stage of development. But having an eye for performance is also essential in game design even before you start the main coding. You may have to do tests beforehand .. how many characters can you have on screen? How rich can the environments be? How far can you see into the distance? How much animation? How much physics? How many ray tests, path finds etc?
As you develop each part you will get a better handle on how long it takes so you can budget your performance time better. This will feedback into the game design - changes may need to be made, but hopefully not too major if you were in the correct ballpark with your tests / calculations etc.
If you get it wrong, it can be a disaster, because you can end up having to redo large sections of the code / game design, or else release a laggy mess that won't sell, in the hope that in 10 years time processors will have caught up with your 'vision' (sloppiness).
One technique I've seen to help is to draw 'performance bars' on screen each frame, a little bar chart which can give you an idea of the time taken by each section of the code. You can see with this as you work what is using up lots of performance, and what is spiking performance too, which can be as big an issue. You can also see as you develop, oops, I just made something incredibly performance sapping.
I won't say too much on making your code fast as there's loads written on this already, it's a massive topic.
You may also be facing some rendering / gameloop synchronization issues, which can be a bit fiddly to get right.
As hodgman says, 0.07 seconds sounds like way over for a gametick. However, to play devil's advocate, if you fixed your timestep at 10 ticks per second, and used interpolating rendering as you should, you'd get silky smooth gameplay (assuming your rendering was totally independent). In fact if you write your code right, you can just change your tickrate #define to e.g. once per second, and it will still look silky smooth (just the physics / reactions etc will be well stodgy lol).
However in the real world, with spikes in gametick times, you'll probably need a bigger comfort zone to prevent the render thread getting 'starved' of updates, and stuttering.
Also, if you are talking about a non-fixed platform (i.e. not a console) then remember people might be playing on a lower spec machine than your development box. It's great if your PC game runs just about on top spec hardware, but not so great if it becomes unplayable on the other 90% of people's machines.
Personally I tend to shoot for great performance on a machine that is about quarter the power of current 'high spec'. That covers most of the bases. It's crucial that the game tick rate at least runs fine on low spec machines, because then at least the rendering can be scaled back without 'breaking' the game.
Crossbones+ - Reputation: 6034
Posted 07 September 2012 - 02:37 AM
1) Make sure you are actually doing an optimized build when you profile, (Debug builds can be horribly slow).
2) Avoid making unnecessary calculations in tight loops, (If a part of a longer expression is constant you can calculate it outside the loop and reuse it).
3) Avoid creating/destroying objects frequently. (keep short lived objects (bullets, particles, etc) in a pool and reuse them instead of constantly creating new ones)
4) Use appropriate containers for your objects. (vectors tend to be faster than lists for most purposes unless you need to frequently remove items from the middle while retaining object order).
5) If you are doing things like collission detection between many objects try to use some form of space partitioning to cut down the number of checks made or use fast coarse checks before the more detailed checks. (AABB or Sphere/Distance checks are fast, mesh or per pixel checks are relativly slow)
6) If you are comparing distances/vector lengths, use the squared distance/length to eliminate the sqrt call (They're fairly expensive and often unnecessary). (This trick makes circle or sphere collision checks even faster)
The voices in my head may not be real, but they have some good ideas!
Members - Reputation: 122
Posted 07 September 2012 - 08:26 AM
For math it might be usefully to shorten the formulars (espacially cosinus ect) as set of matrices methods or functions wich should
be inlined, to get opcode cache benefits. Just my 2 cents.