问题描述
当我在ARKit中绕y轴旋转设备时,我正在尝试计算设备的旋转.为了澄清起见,ARKit中的y轴是垂直于地面的指向上的轴.
I am trying to calculate the rotation of my device when I rotate it around the y-axis in ARKit. For clarification the y-axis in ARKit is the axis pointing upwards perpendicular to the ground.
我使用euangleangle来获得相机的旋转,如下所示:
I used eulerangles to get the rotation of the camera like this:
var alpha = sceneView.pointOfView?.eulerAngles.y
这在0=<alpha<pi/2 and when -pi/2<alpha<=0
时大致有效,但是对于其他角度来说,我得到了错误的读数.我怀疑这与万向节锁定有关,而且我必须以某种方式使用四元数来获得正确的角度,而与象限无关.任何帮助深表感谢!
This approximately works when 0=<alpha<pi/2 and when -pi/2<alpha<=0
, but for what is supposed to be other angles I get the wrong readings. I suspect this has to do with gimbal lock and that I have to somehow use quaternions to be able to get the correct angle regardless of quadrant. Any help is much appreciated!
推荐答案
通过使用四元数而不是欧拉角来解决.这是我使用的代码:
Solved it by using quaternions instead of eulerangles. This is the code I used:
guard let cameraNode = sceneView.pointOfView else { return }
//Get camera orientation expressed as a quaternion
let q = cameraNode.orientation
//Calculate rotation around y-axis (heading) from quaternion and convert angle so that
//0 is along -z-axis (forward in SceneKit) and positive angle is clockwise rotation.
let alpha = Float.pi - atan2f( (2*q.y*q.w)-(2*q.x*q.z), 1-(2*pow(q.y,2))-(2*pow(q.z,2)) )
我使用了这个网站.
这篇关于在ARKit中获取设备绕世界原点y轴的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!