Android加速度计角度计算

时间:2012-03-31 20:03:52

标签: android accelerometer android-sensors

任何人都可以根据加速度计x,y,z值计算罗盘针旋转指向重力方向的角度吗?

1 个答案:

答案 0 :(得分:1)

我认为X应为0且y应为正,而z接近0时指南针指向地球。 (这意味着手机是垂直的)。

一般来说,从0角度看,指南针的角度应该类似于

float accelerometerMaxRange = 10; // This is NOT right, but it's a good value to work with
float newAngle = 0;
if (z > 9) {
    // Phone is horizontally flat, can't point towards gravity, really. Do whatever you think is right
} else {
    newAngle  = (float)(x * 90 / accelerometerMaxRange);
    if (y < 0) {
        newAngle = 180 - newAngle;
    }
}