如何在 android 中将设备坐标系映射到世界坐标系

How to map device co-ordinate system to world co-ordinate system in android

我从加速度计得到矢量 (Ax,Ay,Az)。当我的 phone 躺在 table 上且顶部朝北时,我知道我的 phone 坐标系和世界坐标系匹配。但是当我改变 phone 的位置时,坐标系不一样,需要重新映射。为此,我需要一个旋转矩阵来修复问题。但无法确定旋转矩阵。我试过跟风但没有运气

Not able to translate device co-ordinate system to world co-ordinate system on android

那么有什么方法可以获取旋转矩阵并在 android 中执行映射?

我尝试将向量与三个轴的旋转矩阵相乘。但它没有给出任何令人满意的结果。由于加速度计确定的角度不正确。另外我认为旋转矩阵没有达到目的。

rotation matrix

出于这些目的,android 提供了 SensorManager 给定的重力传感器数据和磁传感器数据。首先是重力传感器(加速度计)读数和磁传感器读数。声明数组并将数据存储到它们。旋转数组将用于存储旋转矩阵数据。 temp[] 用于临时存储数据。

private static final float rotation[] = new float[9];
private static final float temp[] = new float[9];
private static final float grav[] = new float[3];
private static final float mag[] = new float[3];

然后通过以下代码获取旋转矩阵。这里 temp 是存储旋转信息的临时矩阵

SensorManager.getRotationMatrix(temp, null, grav, mag);

使用以下方法重新映射坐标

SensorManager.remapCoordinateSystem(temp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotation);