Getting the rotation of device around the world origin's y axis in ARKit

对着背影说爱祢 提交于 2020-02-28 09:46:05

问题


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

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!


回答1:


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)) )

I used the quaternion to heading/euler conversion listed on this website.



来源:https://stackoverflow.com/questions/54893331/getting-the-rotation-of-device-around-the-world-origins-y-axis-in-arkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!