加速度计移动的距离

时间:2011-07-11 02:08:18

标签: iphone xcode accelerometer distance

我想通过移动iPhone在iPhone屏幕上移动对象(矩形,圆形等)。 例如,我沿着X轴移动iPhone,对象沿着X轴移动。 Y轴,Z轴相同。

我该怎么做? 我可以为它获得算法吗?

谢谢。

P.S: 我看了一会儿,似乎可以使用加速度计。

3 个答案:

答案 0 :(得分:13)

答案 1 :(得分:3)

不容易做到准确。加速度计仅报告加速度,而非运动。您可以尝试(数字地)对加速度进行积分,但最终可能会导致累积误差累加并导致意外运动。

伪代码,显然未经测试:

init:
    loc = {0, 0, 0}    ; location of object on screen
    vel = {0, 0, 0}    ; velocity of object on screen
    t = 0              ; last time measured

step:
    t0 = time          ; get amount of time since last measurement
    dt = t0 - t
    accel = readAccelerometer()
    vel += accel * dt  ; update velocity
    loc += vel * dt    ; update position
    displayObjectAt(loc)

答案 2 :(得分:2)

为什么不使用加速度本身而不是距离移动?例如; 如果你的加速度是1.4G以东,你的物体应该每秒向东移动14像素,直到你的加速度变化。

所以对象的运动就像是在相对的力量下,我觉得它更现实。更快的移动装置,更大的力量来影响物体。

另见:

http://www.freescale.com/files/sensors/doc/app_note/AN3397.pdf

Using accelerometer, gyroscope and compass to calculate device's movement in 3D world

相关问题