如何使用加速度计编写平滑的运动,就像 iPhone OS 上的迷宫游戏一样?

发布于 2024-08-21 22:02:34 字数 952 浏览 7 评论 0原文

我希望能够通过加速计控制图像使图像真实地移动,就像任何迷宫游戏一样。下面显示了我到目前为止所拥有的,但它看起来非常紧张,而且根本不现实。球的图像似乎永远无法停止,并在各处做出许多紧张的动作。

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

deviceTilt.x = 0.01 * deviceTilt.x + (1.0 - 0.01) * acceleration.x;
deviceTilt.y = 0.01 * deviceTilt.y + (1.0 - 0.01) * acceleration.y;
}

-(void)onTimer {

    ballImage.center = CGPointMake(ballImage.center.x + (deviceTilt.x * 50), ballImage.center.y + (deviceTilt.y  * 50));

    if (ballImage.center.x > 279) {

        ballImage.center = CGPointMake(279, ballImage.center.y);
    }
    if (ballImage.center.x < 42) {

        ballImage.center = CGPointMake(42, ballImage.center.y);
    }
    if (ballImage.center.y > 419) {

        ballImage.center = CGPointMake(ballImage.center.x, 419);
    }
    if (ballImage.center.y < 181) {

        ballImage.center = CGPointMake(ballImage.center.x, 181);
    }

I want to be able to make image move realistically with the accelerometer controlling it, like any labyrinth game. Below shows what I have so far but it seems very jittery and isnt realistic at all. The ball images seems to never be able to stop and does lots of jittery movements around everywhere.

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

deviceTilt.x = 0.01 * deviceTilt.x + (1.0 - 0.01) * acceleration.x;
deviceTilt.y = 0.01 * deviceTilt.y + (1.0 - 0.01) * acceleration.y;
}

-(void)onTimer {

    ballImage.center = CGPointMake(ballImage.center.x + (deviceTilt.x * 50), ballImage.center.y + (deviceTilt.y  * 50));

    if (ballImage.center.x > 279) {

        ballImage.center = CGPointMake(279, ballImage.center.y);
    }
    if (ballImage.center.x < 42) {

        ballImage.center = CGPointMake(42, ballImage.center.y);
    }
    if (ballImage.center.y > 419) {

        ballImage.center = CGPointMake(ballImage.center.x, 419);
    }
    if (ballImage.center.y < 181) {

        ballImage.center = CGPointMake(ballImage.center.x, 181);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-08-28 22:02:34

是否有某种原因导致您无法使用针对上一个问题提供的平滑过滤器:如何使用移动平均值来过滤加速度计值在 iPhone 操作系统中?

Is there some reason why you can not use the smoothing filter provided in response to your previous question: How do you use a moving average to filter out accelerometer values in iPhone OS ?

静水深流 2024-08-28 22:02:34

您需要计算这些值的运行平均值。为此,您需要将最后 n 个值存储在数组中,然后在读取加速度计数据时从数组中推送和弹出值。这是一些伪代码:

const SIZE = 10;
float[] xVals = new float[SIZE];

float xAvg = 0;

function runAverage(float newX){
  xAvg += newX/SIZE;
  xVals.push(newX);
  if(xVals.length > SIZE){
    xAvg -= xVals.pop()/SIZE;
  }
}

您需要对所有三个轴执行此操作。尝试调整 SIZE 的值;它越大,值越平滑,但响应速度似乎也越慢。这实际上取决于您读取加速度计值的频率。如果每秒读取 10 次,则 SIZE = 10 可能太大。

You need to calculate the running average of the values. To do this you need to store the last n values in an array, and then push and pop values off the array when ever you read the accelerometer data. Here is some pseudocode:

const SIZE = 10;
float[] xVals = new float[SIZE];

float xAvg = 0;

function runAverage(float newX){
  xAvg += newX/SIZE;
  xVals.push(newX);
  if(xVals.length > SIZE){
    xAvg -= xVals.pop()/SIZE;
  }
}

You need to do this for all three axis. Play around with the value of SIZE; the larger it is, the smoother the value, but the slower things will seem to respond. It really depends on how often you read the accelerometer value. If it is read 10 times per second, then SIZE = 10 might be too large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文