cameranode rotate as iOS device moving

后端 未结 2 928
庸人自扰
庸人自扰 2020-12-05 11:53

It\'s 360 degree video player project.

I add a cameranode(SCNNode) to a rootnode, the cameranode was put at the center(0,0,0) of the SCNSphere

2条回答
  •  失恋的感觉
    2020-12-05 12:04

    Device motion is best, as it ties in multiple sensors and fuses the data together in a meaningful way. It's also best to avoid using euler angles, as they suffer from what's known as gimbal lock. Instead, use quaternions and set your camera orientation with them.

    I've created a Swift class extension for CMDeviceMotion that'll let you determine what the device is looking at (for any orientation of the screen).

    From there, it's straightforward to rotate your camera. First, setup your device motion (in view did appear, or some other appropriate place):

    if motionManager.deviceMotionAvailable {
    
        motionManager.deviceMotionUpdateInterval = 0.017
        motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue(), withHandler: deviceDidMove)
    }
    

    Then handle those updates in your deviceDidMove implementation, using the previously mentioned CMDeviceMotion extension:

    func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {
    
        if let motion = motion {
    
            yourCameraNode.orientation = motion.gaze(atOrientation: UIApplication.sharedApplication().statusBarOrientation)
        }
    }
    

    And now your camera should follow your devices orientation in space.

提交回复
热议问题