Tuesday 30 December 2014

Stutter and Frame-rate

I've spent a while working on annoying bugs so I thought I'd write about one of them.

I'm making a game currently titled "The Button" where you work on a call-in radio station censoring swear words spoken by callers. I'm using LibGDX for it and I thought it would be a perfect game for Android/iOS due to the simple controls.

When you play the game, you see the waveform representing the spoken audio, in the game it runs super-smooth except for every 8 seconds or so it judders back and forth for about 500ms. I thought it was something wrong with my waveform display or some stupid mistake I'd make in the game so I spent a few hours trying to fix it to no avail.


I eventually decided to simplify it and make a nice new simple project with just random lines moving across the screen. Try for yourself (simple source code). The stutter is still present. You might have to watch it for a few seconds to see it.

I played around with fixing the frame-rate, enabling v-sync, leaving the frame-rate uncapped and each time the stutter remained. It almost felt like there was a big garbage-collect slowing things down but nothing was being created each frame. With the frame-rate uncapped it ran at >2000 fps so it's definitely not my computer struggling to keep up. Floats can be weird sometimes so I switched to long milliseconds but that made no difference (clutching at straws there!).

After another few hours of messing about I decided to try another library, just to see if the stutter still happened. Love2D is one of my favourites due to how quick it is to get started so I mocked up the same code in there (look how tiny it is!) and to my surprise, the stutter was still present! At this point I was staring at my code, sure that I'd made a stupid mistake somewhere but I couldn't see it.

I started to talk to my friend at this point and we did some tests. If you fix the frame-rate at 60, ignore the delta time and just increment the index by 1 each time, it looks better. Unfortunately I can't use this method in the game because the 60 fps is an ideal target not an enforced limit so the audio would get out-of-sync with the graphics. So it must be something to do with delta time. We tried a lot of different things and managed to work out that the index was incrementing by 0 and 2 in rapid succession on the stuttering frames. It would increment by 0, 1, 1, 1, 2, 1, 0 ,1, 1 ,2 etc. on consecutive frames after a long period of non-stuttering frames (incrementing by 1 each time).

So I'm not exactly sure what causes the stutter to happen in waves but the stutter seems to be caused by frames not happening exactly before each time the screen refreshes. So frame 1 would cause the indexer to increment by 0.999 and this would not be enough to move the lines and frame 2 would increment indexer by 1.031 and this would cause them to jump by 2 pixels, causing the stuttery increments.

I've edited the update to be like this and it's a lot better. It's still not 100% smooth but it doesn't have the horrible jarring stutters of the first one. I might have missed something super obvious so please let me know if you notice anything or have any ideas.

Friday 12 December 2014

New projects

I've done a few tests with the map overworld mode and it turns out it's just not as fun as the arena mode. This is a shame because I had big ideas for this but there's no point continuing working on a game that won't be fun! I learnt a whole ton making this project and I think arena mode is fun in its own right. I was getting slow at working on it, maybe because I was realising it was going nowhere but luckily this leaves me free to work on other stuff now!

I have a couple of projects I want to work on-
  • Another space game with a friend of mine. This one is going to be a bit crazy- I'll start posting about this soon.
  • A silly little mobile game I've been thinking about. Audio-focused, I've got a rad voice-artist dude I'll be working with I hope!
For now I want to try to learn more about creating files in code and how sound words so I'm going to try making some .wav files of my own.