如何为我的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);
}
}
谢谢!
答案 0 :(得分:9)
好的,问题已解决并且解决方案非常简单我实际上很尴尬我没有早点提出它。就这么简单:
在“校准按钮”中:
//store the current offset for a "neutral" position
calibration = -currentAccelX * accelerationFactor;
在游戏时间加速度计功能中:
//add the offset to the accel value
float x = (-acceleration.y * accelerationFactor) + calibration;
就像添加偏移一样简单。 * 隐藏他的头 *
答案 1 :(得分:0)
这是我到目前为止的代码:
//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;
}