如何在 iPhone OS 上像迷宫游戏一样使用加速度计编程平滑运动? [英] How to program smooth movement with the accelerometer like a labyrinth game on iPhone OS?

查看:27
本文介绍了如何在 iPhone OS 上像迷宫游戏一样使用加速度计编程平滑运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过控制它的加速度计使图像真实地移动,就像任何迷宫游戏一样.下面显示了我到目前为止所拥有的,但它似乎非常紧张,根本不现实.球的图像似乎永远无法停止,到处都是抖动的动作.

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

推荐答案

您是否有某些原因无法使用针对您之前的问题提供的平滑过滤器:你如何使用移动平均来过滤 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 ?

这篇关于如何在 iPhone OS 上像迷宫游戏一样使用加速度计编程平滑运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆