在 ARKit 中获取设备围绕世界原点 y 轴的旋转

A. *_*son 4 math 3d euler-angles ios arkit

当我在 ARKit 中围绕 y 轴旋转我的设备时,我试图计算它的旋转。为了澄清起见,ARKit 中的 y 轴是垂直于地面指向上方的轴。

我使用 eulerangles 来获得相机的旋转,如下所示:

var alpha = sceneView.pointOfView?.eulerAngles.y
Run Code Online (Sandbox Code Playgroud)

这大约在 时有效0=<alpha<pi/2 and when -pi/2<alpha<=0,但是对于应该是其他角度的内容,我得到了错误的读数。我怀疑这与万向节锁定有关,并且我必须以某种方式使用四元数才能获得正确的角度,而不管象限如何。任何帮助深表感谢!

A. *_*son 7

通过使用四元数而不是欧拉角来解决这个问题。这是我使用的代码:

  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)) )
Run Code Online (Sandbox Code Playgroud)

我使用了该网站上列出的四元数到标题/欧拉转换。