Android accelerometer angle calculation

老子叫甜甜 提交于 2020-01-14 20:08:05

问题


Can anyone give any idea to calculate the angle by which a compass needle should be rotated to point in the direction of gravity from accelerometer x, y, z values?


回答1:


I think X should be 0 and y should be positive while z is near 0 for the compass to point down towards earth. (Which means the phone is held vertical).

In general, from the 0 angle, the compass' angle should be something like

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


来源:https://stackoverflow.com/questions/9959221/android-accelerometer-angle-calculation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!