如何为我的iPhone游戏正确校准加速度计?目前,当手机在平坦的表面上时,桨叶向左漂移.高通或低通滤波器不是可接受的解决方案,因为即使在低值和高值时我也需要完全控制桨.我知道Apple有BubbleLevel样本,但我觉得很难遵循......有人可以简化这个过程吗?
我的加速度计代码如下所示:
-(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); } }
谢谢!
好的,问题解决了,解决方案很简单我真的很尴尬我没有早点提出来.这很简单:
在"校准按钮"中:
//store the current offset for a "neutral" position calibration = -currentAccelX * accelerationFactor;
在游戏时间加速计功能:
//add the offset to the accel value float x = (-acceleration.y * accelerationFactor) + calibration;
它就像添加偏移一样简单.*隐藏他的头*