IPhone Accelerometer Calibration

How to calibrate the accelerometer for my iPhone game? Currently, when the phone is on a flat surface, the paddle drifts to the left. High or low pass filters are not an acceptable solution, as I need full control of the paddle even at low and high values. I know that Apple has a sample of BubbleLevel, but it's hard for me to follow ... can someone simplify the process?

My accelerometer code looks like this:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { float acelx = -acceleration.y; float x = acelx*40; Board *board = [Board sharedBoard]; AtlasSprite *paddle = (AtlasSprite *)[board.spriteManager getChildByTag:10]; if ( paddle.position.x > 0 && paddle.position.x < 480) { paddle.position = ccp(paddle.position.x+x, paddle.position.y); } if ( paddle.position.x < 55 ) { paddle.position = ccp(56, paddle.position.y); } if ( paddle.position.x > 435 ) { paddle.position = ccp(434, paddle.position.y); } if ( paddle.position.x < 55 && x > 1 ) { paddle.position = ccp(paddle.position.x+x, paddle.position.y); } if ( paddle.position.x > 435 && x < 0) { paddle.position = ccp(paddle.position.x+x, paddle.position.y); } } 

Thanks!

+6
iphone accelerometer calibration
source share
2 answers

Ok, the SOLVED problem and the solution is so simple that I'm really confused. I have not come up with this before. It is so simple:

In the "Calibration button":

 //store the current offset for a "neutral" position calibration = -currentAccelX * accelerationFactor; 

In the function of the accelerometer playing time:

 //add the offset to the accel value float x = (-acceleration.y * accelerationFactor) + calibration; 

It is as simple as adding an offset. * hides his head *

+9
source share

This is the code that I still have:

 //calibrate code from StackOverflow float calibration = -acceleration.x * accelerationFraction; float x = (-acceleration.y * accelerationFraction) + calibration; //My original code accelerationFraction = acceleration.y*2; if (accelerationFraction < -1) { accelerationFraction = -1; } else if (accelerationFraction > 1) { accelerationFraction = 1; } //API CHANGE set delegate to self if (orientation == UIDeviceOrientationLandscapeLeft){ accelerationFraction *= -1; } 
0
source share

All Articles