Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android game loop, how to control speed and frame rate

I've written a game for Android, and I've tested it on the Dev Phone 1. It works perfectly, the speed is just right. However, I'm sure phone CPU's are getting faster. They may already be faster than the dev phone.

How do I make sure that my game runs at the exact same speed no matter what the device or how fast it runs? Do you know of any techniques? Should I check some kind of timer at the top of the loop each time?

I guess I'm referring to frame rate - but mostly the speed at which my game runs through the main game loop.

Any theory or experience would be great! Thank you.

like image 748
Ben Mc Avatar asked Feb 15 '10 09:02

Ben Mc


1 Answers

If you are targeting certain frame rate, the basic idea is that you should have a timer or thread that executes your game's tick method at desired intervals. With timers the implementation is pretty trivial: just schedule a timer to execute at regular intervals. When using threads you have to put the thread to sleep between consecutive ticks if it runs faster than the desired frame rate.

However, this alone doesn't lead to the best possible results as the interval can vary a bit between the frames. There is a very good article on this issue: http://gafferongames.com/game-physics/fix-your-timestep/.

Also, there are already slower and faster Android phones than the dev phone 1. So you have to prepare for the both cases if you are targeting all Android devices. If your game is not that CPU heavy, it might be that you can achieve the desired frame rate on all the devices. But if you don't limit the frame rate, your game will be too fast on the faster Android phones.

like image 112
Lauri Avatar answered Nov 09 '22 05:11

Lauri