[英]How to get rotation around Y-axis relative to how the user holds the device?
[英]Getting the rotation of device around the world origin's y axis in ARKit
當我在 ARKit 中圍繞 y 軸旋轉我的設備時,我試圖計算它的旋轉。 為了澄清起見,ARKit 中的 y 軸是垂直於地面指向上方的軸。
我使用 eulerangles 來獲得相機的旋轉,如下所示:
var alpha = sceneView.pointOfView?.eulerAngles.y
當0=<alpha<pi/2 and when -pi/2<alpha<=0
,這大約有效,但是對於應該是其他角度的我得到錯誤的讀數。 我懷疑這與萬向節鎖定有關,並且我必須以某種方式使用四元數才能獲得正確的角度,而不管象限如何。 任何幫助深表感謝!
通過使用四元數而不是歐拉角來解決它。 這是我使用的代碼:
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)) )
我使用了這個網站上列出的四元數到標題/歐拉轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.