本文介绍了Android加速度计角度计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能从加速度计的x,y,z值中计算出指南针旋转以指向重力方向的角度吗?

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?

推荐答案

我认为X应当为0,y应当为正,而z接近0才能使罗盘指向地球。
(这意味着手机是垂直放置的。)

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).

通常,从0角度来看,指南针的角度应该类似于

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

这篇关于Android加速度计角度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 13:28