Friday, 2 March 2012

UIAccelerometer input causing UITouch lag

Something I noticed a while back, and finally decided to address, was touch latency issues in Balls Abound. The game uses the accelerometer for the main source of user input, and requires updates at 60fps. Using UIAccelerometer to dispatch the accelerometer updates at 60fps it turns out can induce problems with touch input performance (and possibly issues when debugging and resume after some pause in execution). In Balls Abound, rubbing your finger on the screen for a few seconds would degrade touch input performance from 60fps down to 1-2fps. The effect would clear up if you left the screen alone for a few seconds.

Unhappy with this I went searching for the cause on Google. I found anecdotal evidence that the root cause was the use of UIAccelerometer for receiving accelerometer updates, and that moving to CMMotionManager would resolve the issue.

So I tore into cocos2d and refactored the CCLayer to use CMMotionManager to track raw accelerometer data instead of using UIAccelerometer. Turns out, it's much better, consistent 60fps on all inputs no matter how much I rub the screen. To be completely honest, I removed accelerometer bindings from CCLayer altogether and put together a CMMotionDispatcher analogous to the CMTouchDispatcher. This gave me the flexibility to listen for accelerometer data on objects other than the layers. Eventually I would like to improve the game by taking advantage of the rotation rate data that can be read on devices with gyros using the motion manager, but that will have to wait.

No comments:

Post a Comment