SceneKit – Get direction of camera

拥有回忆 提交于 2019-12-20 10:55:54

问题


I need to find out which direction a camera is looking at, e.g. if it is looking towards Z+, Z-, X+, or X-.

I've tried using eulerAngles, but the range for yaw goes 0 -> 90 -> 0 -> -90 -> 0 which means I can only detect if the camera is looking towards Z or X, not if it's looking towards the positive or negative directions of those axes.


回答1:


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

Another way you could do it is like 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))
    }


来源:https://stackoverflow.com/questions/45185555/scenekit-get-direction-of-camera

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