在 ARKit 中获取设备围绕世界原点 y 轴的旋转 [英] Getting the rotation of device around the world origin's y axis in ARKit
问题描述
当我在 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.
我像这样使用欧拉角来获得相机的旋转:
I used eulerangles to get the rotation of the camera like this:
var alpha = sceneView.pointOfView?.eulerAngles.y
当 0=<alpha<pi/2 和 -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)) )
我使用了 this 中列出的四元数到标题/euler 转换网站.
这篇关于在 ARKit 中获取设备围绕世界原点 y 轴的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!