Strange accelerometer readings

Hi there. I’m porting my Android game to iOS and am using the following code to grab the accelerometer x value (using Microsoft.Devices.Sensors):

private void InitAccelerometer() {
   try {
      Accelerometer acc = new Accelerometer();
      acc.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(HandleAccelerometer);
      acc.Start();
   }
   catch(Exception) {}
}

private void HandleAccelerometer(object sender, SensorReadingEventArgs<AccelerometerReading> e) {
   float accelX = e.SensorReading.Acceleration.X;
}

The results are very strange though. The values are fluctuating between -1.0 and 1.0 as I would expect but they aren’t being affected by tilting the phone. They even change when the phone is lying on a flat surface. Has anyone experienced similar results on iOS?

Thanks,
Ronny

Have you looked at the Y and Z values too?

This might be related:


Maybe try undo this change and see if it helps.

Hi there.

The Y and Z values are behaving strangely as well. I’m also seeing similar behaviour, ie. Incorrect or really laggy values when I use the CMMotionManager class. Both methods seem to work fine when I have them in a separate MonoGame project that does nothing except display the readings so it does fundamentally work in the version of MonoGame I’m using (3.2). I’m developing on an iPhone 4 with OS 6.1, almost seems like the phone is struggling and the accelerometer readings are being ignored.

Just tried changing the 2 lines of code back from the GitHub link and it works much better now but there’s some definite lag there.

Weird. Is your game running smoothly at 60fps?
If its not hitting full frame rate that might explain it.

I have the frame rate set to 30fps using TargetElapsedTime = TimeSpan.FromTicks(333333) and it drops to the low 20s at times so there are performance issues with my game. I’ve posted another question about that here:

Do you think that could be affecting the accelerometer readings?

Ok, latest update. I’ve tried both methods for reading the accelerometer in Microsoft.Devices.Sensors.Accelerometer::ReadingChangedHandler and both have issues on the iPhone 4.

If I use the data object passed into the method as per the latest MonoGame release there seems to be a lot of lag, which gets worse when I tap the screen to fire. My game is an accelerometer controlled shooter so is essentially unplayable without reliable readings.

If I revert to the code from GitHub, ie. Using the motionManager instance variable from the SensorBase class, the lag problem is fixed. This method however seems to cause a short stutter, almost like a garbage collect every 2-3 seconds. This happens with a release version on a very simple screen containing only 2 sprites.

Any help would really be appreciated as this problem will prevent me from releasing my game on the iPhone 4, which should be more than capable of handling it.

Thanks
Ronny

I’ve now given up using the Accelerometer class altogether. I’m guessing this is working fine on the iPhone 5 but on the iPhone 4, if my game drops below 30 FPS, the readings completely mess up.

The GitHub revert keeps the readings accurate but seems to create massive amounts of objects that result in a short stutter every few seconds, which I presume is the garbage collector.

The problem seems to be with the underlying MonoTouch CMMotionManager class as it still doesn’t work properly when I just use this directly. I’m now using the UIAccelerometer class from the MonoTouch.UIKit which seems to provide a much better performance:

private UIAccelerometer acc;
private double _xAccel;

private void InitAccelerometer() {
    acc = UIAccelerometer.SharedAccelerometer;
    acc.Acceleration += delegate(object sender, UIAccelerometerEventArgs e) {
        _xAccel = e.Acceleration.X; 
    };
}

Hopefully this proves useful for anyone else experiencing similar issues.