如何在Android中从旋转矩阵中获取四元数?

如何在Android中从旋转矩阵中获取四元数?,android,android-sensors,quaternions,Android,Android Sensors,Quaternions,我试图获得android设备的方向,我需要它是四元数结构(float[4]基本上) 我现在得到的是: if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ last_acc = (float[])event.values.clone(); } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){ last_mag = (float[])event

我试图获得android设备的方向,我需要它是四元数结构(
float[4]
基本上)

我现在得到的是:

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
    last_acc = (float[])event.values.clone();
}
else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
    last_mag = (float[])event.values.clone();
}
if (last_acc != null && last_mag != null){
    SensorManager.getRotationMatrix(rotation, inclination, last_acc, last_mag);
    SensorManager.getOrientation(rotation, orientation);
在“旋转”中,我有4x4旋转矩阵,在方向中,我有一个浮点[3]向量,其中有方位角、俯仰和滚动


我现在怎么才能得到四元数呢?

我不会使用欧拉角(横摇、俯仰、偏航),这几乎是你的应用程序

至于将旋转矩阵转换为四元数,谷歌表示:


如果您不介意仅对API级别9及更高级别可用,则可以使用
传感器。键入旋转向量
,它将返回一个单位四元数,并为您节省工作量。如果输入长度为3的数组,则值将是满足
sin(θ/2)*u的四元数的向量部分,其中
u
是三维单位向量。如果数组长度为4,则第四个值将是单位四元数的标量值,
cos(θ/2)


答案是Thanx。我想这就是我需要的。另外,谷歌的视频解释得非常清楚。阿历克斯:很高兴听到这个消息。祝你好运