6

如何正确校准 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);
}
}

谢谢!

4

2 回答 2

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;

就像添加偏移量一样简单。*把头藏起来*

于 2010-01-22T23:53:40.877 回答
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;
}
于 2012-08-30T14:49:01.063 回答