如何避免在Core Motion控制的SceneKit相机中滚动?

Ort*_*ntz 6 camera quaternions ios core-motion scenekit

我正在CMDeviceMotion使用本答案中描述的CMDeviceMotion扩展将SceneKit相机设置为当前姿态:

func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {
    if let motion = motion {
        let orientation = motion.gaze(atOrientation: UIApplication.sharedApplication().statusBarOrientation)
        cameraNode.orientation = orientation
    }
}
Run Code Online (Sandbox Code Playgroud)

这很漂亮,但是,我想阻止旋转(滚动)并且只允许相机转动(偏转)和俯仰.

我试图将四元数转换回欧拉角,并将roll保持为0:

cameraNode.eulerAngles = SCNVector3(
            x: orientation.pitch(),
            y: orientation.yaw(),
            z: 0)
Run Code Online (Sandbox Code Playgroud)

然而,这仅适用于偏航运动的一半.另一半,相机倒置.我怀疑这是涉及欧拉角的万向节锁定问题.

四元数对我来说有点像黑魔法,但是有没有办法直接在四元数上去除滚动分量,这样我就可以避免欧拉角?