2

In my app I use the accelerometer to control the character in my game. Right now I only allow the portrait orientation so the user has to tilt the device to the right or left in order to move the character. That is working fine so far. What I am trying to accomplish now is to 'calibrate' the accelerometer to account for the current tilt the user is playing on. Lets say the user is laying on their side, the values will be skewed since it didn't account for that starting point so they won't be able to control the character. So I am trying to account for that initial tilt amount and then account for it during the game however it is not working.

So I have a button in my non-game view to 'calibrate' the accelerometer and this is the code that it executes:

This is in the IBAction

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0f/30.0f];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];

This is the accelerometer delegate call

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [[NSUserDefaults standardUserDefaults] setFloat:acceleration.x forKey:@"X-Calibrate"];
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}

Then in my game view in the init I set the iVar calibrationFloat to the value which I set in NSUserDefaults. Then I also have a kFilteringFactor to get rid of the little jitters. So this is the game accelerometer delegate method which sets rollingX equal to everything and then I move the character based upon rollingX:

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    rollingX = ((acceleration.x - calibrationFloat) * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
}

However this is not working, the calibration doesn't seem to do anything and I am not sure why. Does anyone set why this wouldn't be working?

Ben
  • 51,770
  • 36
  • 127
  • 149
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

1 Answers1

2

I can give you some pointers. The calculation for rollingX is incorrect, it will give out values outside the range -1 - +1, you need a way to detect it hitting the limit and adjust it appropriately. Also simply offsetting one axis will not work. The accelerometer works in 3D space. In order for your calculation to work correctly the phone would always have to be 0 in the z-plane. The further into the z-plane the user rotates the phone, the smaller the readings the device will output on the x-plane. Accelerometers sense gravity, so the planes are fixed relative to this.

In order to carry out an effective calibration you will need to apply a continuous matrix transformation to all 3-axis, as you will need weighted offsets (eg as z increases, pay more attention to y rather than x etc).

Sam Clewlow
  • 4,293
  • 26
  • 36
  • Would there be any way you can provide a code example? I myself am not too familiar with accelerometers and matrices so I posted the bounty for code examples. Thanks so much! – SimplyKiwi Nov 09 '12 at 16:56
  • I have done something simular before, but I couldn't tell you off the top of my head I'm afraid. Check out this blog post though, and hopefully this should help! http://www.paradeofrain.com/2010/07/lessons-learned-in-tilt-controls/ – Sam Clewlow Nov 13 '12 at 14:05
  • any luck with that blog post? Any more questions please let me know! – Sam Clewlow Nov 14 '12 at 10:27
  • Nope sorry, I just don't understand it or how I can apply it to my app in particular. – SimplyKiwi Nov 14 '12 at 20:59
  • 1
    I think going back to basics and learning a bit about how accelerometers work, and go through some code samples. You don't need be a physics/maths whizz, I'm not. Have a look at this Apple sample code. If you can understand this you'll be making good progress http://developer.apple.com/library/ios/#samplecode/pARk/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011083-Intro-DontLinkElementID_2 – Sam Clewlow Nov 15 '12 at 12:26
  • Let me ask you this, I know Apple does calibration in their source code called BubbleLevel however I cannot seem to find it. If you can point me to their project, I think I will be able to implement this! – SimplyKiwi Nov 19 '12 at 03:54
  • Hmmm it looks as though it's been taken down, but it was also dated in 2008, so chances are osme of the stuff has been depreciated. Have you watched the WWDC video on core motion? https://developer.apple.com/library/ios/navigation/#section=Frameworks&topic=CoreMotion – Sam Clewlow Nov 19 '12 at 10:53