Swift SceneKit - get camera direction

I need to find out in which direction the camera is looking. if he is looking at Z +, Z-, X + or X -.

I tried using eulerAngles, but the range for yaw goes 0 → 90 → 0 → -90 → 0, which means that I can only determine if the camera is looking at Z or X, and not if it is looking at the positive or negative directions of these axes .

+4
source share
1 answer

You can create an SCNNode that puts it in the worldFront property to get a vector with x, y, and z directions.

Another way you could do this is how this project did it:

// 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))
    }
+7
source

Source: https://habr.com/ru/post/1681768/


All Articles