2
votes

I have been writing a c++ game engine using OpenGL ES 2.0 for iPhone. However, I have been getting strange results when working the the accelerometer.

I have a character which moves around a 2D world with input from the accelerometer. Initially when the game is started the movement would be delayed for 10 seconds. By delayed I mean if you tilt the phone to move the character to the right it wont be until 0.5 seconds until the character would move to the right. I solved this by calling this code:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.updateInterval = 1.0f/60.0f;
accel.delegate = self;

2 seconds after this code has been called and not at the same time:

displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(drawFrame)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

For some reason this caused the tilting to work perfectly as soon as it was enabled.

However, if you touch the screen many times triggering the touches began and touches ended functions the tilting will go out of sync again and it wont be until 15 seconds or so until it syncs back up again.

It should be noted that the frame rate never drops below 60 and it seems like the accelerometer method doesn't drop below being updated 60 times a seconds but somehow gets out of sync.

Thanks.

1

1 Answers

0
votes

I have fixed all accelerometer issues by switching from using UIAccelerometer to CMMotionManager. I am still unsure why what caused the issue but getting the accelerometer data from CMMotionManager seems to have resolved any problems.