2017-07-19 90 views
3

我需要找出相機正在看的方向,例如,如果它正在尋找Z +,Z-,X +或X-。Swift SceneKit - 獲取相機的方向

我試過使用eulerAngles,但偏航的範圍爲0-> 90-> 0-> -90-> 0這意味着我只能檢測到相機是朝Z或X方向看,而不是如果它是朝着這些軸的正面或負面方向展望。

回答

5

您可以創建一個SCNNode,將它放置在worldFront屬性中以獲取具有x,y和z方向的矢量。

你可以做的另一種方式是像這樣的項目是如何做的:

// Credit to https://github.com/farice/ARShooter 

func getUserVector() -> (SCNVector3, SCNVector3) { // (direction, position) 
     if let frame = self.sceneView.session.currentFrame { 
      let mat = SCNMatrix4(frame.camera.transform) // 4x4 transform matrix describing camera in world space 
      let dir = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33) // orientation of camera in world space 
      let pos = SCNVector3(mat.m41, mat.m42, mat.m43) // location of camera in world space 

      return (dir, pos) 
     } 
     return (SCNVector3(0, 0, -1), SCNVector3(0, 0, -0.2)) 
    } 
+0

此外,這是假設你正在使用ARKit,因爲你的標籤提示 – wriuhasdfhvhasdv