简体   繁体   English

获取手机的 z 轴和磁北极(而不是 y 轴)之间的角度

[英]Get angle between phone's z axis and the magnetic north pole (instead of y axis)

I am aware how to get the orientation angle between the y-axis of the phone and the magnetic north using the getOrientation method (as described here https://developer.android.com/guide/topics/sensors/sensors_position ).我知道如何使用 getOrientation 方法(如此处所述https://developer.android.com/guide/topics/sensors/sensors_position )获取手机 y 轴和磁北之间的方向角。 However, I would need the orientation between the z-axis of the phone and the magnetic north, so to get the direction of the phone's camera.但是,我需要手机的 z 轴和磁北之间的方向,以便获得手机相机的方向。 I have tried writing my own code to calculate this angle from the angle between the y-axis and the magnetic north, but it gives me unreliable data, even if corrected with the gravity sensor data (eg when the phone is flipped over).我曾尝试编写自己的代码来根据 y 轴和磁北之间的角度计算这个角度,但它给了我不可靠的数据,即使使用重力传感器数据进行了校正(例如,当手机翻转时)。

Is there a way to directly get the angle between the z-axis and the magnetic north?有没有办法直接得到z轴和磁北之间的角度?

在此处输入图片说明

Building on this question , I would suggest this:基于这个问题,我建议:

    private val rotationMatrix = FloatArray(9)

    override fun onSensorChanged(event: SensorEvent) {
        if (event.sensor.type != Sensor.TYPE_ROTATION_VECTOR) return
        SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values)
        val azimuth = atan2(-rotationMatrix[2], -rotationMatrix[5])
        ... use azimuth ...
    }

Reasoning:推理:

(rotationMatrix[5], rotationMatrix[2]) describes the projection of the phone's z-vector on the Earth's horizontal plane, in terms of the (east, north) coordinates. (rotationMatrix[5], rotationMatrix[2])根据(东、北)坐标描述了手机的 z 向量在地球水平面上的投影。 You need the angle to the north axis, so the north-direction is your x and the east-direction is the y .您需要与北轴的角度,因此北向是您的x ,东向是y atan2 takes the parameters in the (y, x) order, that's why I put the index 2 first and then 5. The z-vector points out of the front of the phone, but I presume you want the direction of the phone's back, where the main camera is. atan2(y, x)顺序获取参数,这就是为什么我先把索引 2 然后 5。 z 向量指向手机正面,但我想你想要手机背面的方向,主摄像头在哪里。 That's why I put the minus signs.这就是为什么我把减号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM