【问题标题】:iPhone Accelerometer calibrationiPhone加速度计校准
【发布时间】:2011-01-07 23:32:51
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: iphone accelerometer calibration


    【解决方案1】:

    好的,问题解决了,而且解决方案非常简单,实际上我很尴尬我没有早点想出它。就这么简单:

    在“校准按钮”中:

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

    在游戏时间加速度计功能中:

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

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

    【讨论】:

    • 这里是对主题的全面解释iphonedevsdk.com/forum/iphone-sdk-tutorials/…
    • 这是一个旧线程,但我很好奇 currentAccelX 是什么类型的对象以及AccelerationFactor 是什么。你能帮忙吗?
    • 是的,你从哪里得到这些变量; currentAccelX 和AccelerationFactor?这篇文章早在 2011 年就已经过时了……我知道它现在更老了,但也许有人可以帮助我?谢谢
    • 这是我到目前为止的代码: //calibrate code from StackOverflow floatcalibration = -acceleration.x * accelerationFraction;浮点 x = (-acceleration.y * accelerationFraction) + 校准;加速度分数 = 加速度.y*2; if (accelerationFraction 1) { accelerationFraction = 1; } //API CHANGE 将委托设置为 self if (orientation == UIDeviceOrientationLandscapeLeft){ accelerationFraction *= -1; }
    • 在这里阅读我的解释:iphonedevsdk.com/forum/iphone-sdk-tutorials/…
    【解决方案2】:

    这是我到目前为止的代码:

    //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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-03
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      相关资源
      最近更新 更多