当我在ARKit中围绕y轴旋转时,我正在尝试计算设备的旋转。为了澄清,ARKit中的y轴是指向垂直于地面的轴。
我使用eulerangles来获得相机的旋转,如下所示:
var alpha = sceneView.pointOfView?.eulerAngles.y
这大约适用于0=<alpha<pi/2 and when -pi/2<alpha<=0
,但对于应该是其他角度的我得到了错误的读数。我怀疑这与万向节锁有关,我不得不以某种方式使用四元数来获得正确的角度而不管象限。任何帮助深表感谢!
通过使用四元数而不是eulerangles来解决它。这是我使用的代码:
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 website上列出的标题/欧拉转换。