6

I'm setting my SceneKit camera to the current CMDeviceMotion attitude using the CMDeviceMotion extension described in this answer:

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

This works beautifully, however, I'd like to block the rotation (roll) and only allow the camera to turn around (yaw) and pitch.

I tried to convert the quaternion back to Euler angles and simply leave roll at 0:

cameraNode.eulerAngles = SCNVector3(
            x: orientation.pitch(),
            y: orientation.yaw(),
            z: 0)

However, this works for only one half of the yaw movement. For the other half, the camera is turned upside down. I suspect this is a Gimbal Lock issue involving Euler angles.

Quaternions are a bit like black magic for me, but is there a way to remove the roll component directly on the quaternion level so I can avoid Euler angles?

Community
  • 1
  • 1
Ortwin Gentz
  • 48,409
  • 23
  • 127
  • 204
  • Have you had any luck figuring this out? – Confused Sep 18 '16 at 07:53
  • No. :( I'd post it here then. – Ortwin Gentz Sep 19 '16 at 12:53
  • I think there's two ways to think about this, but I'm no expert. One is to filter out the axis of movement you don't want from the CM data. The other is to do (as you suggest) a "gimbal lock" that prevents movement in a given axis. – Confused Sep 19 '16 at 13:00
  • I've seen this result achieved with CM, but don't know how it was done. – Confused Sep 19 '16 at 13:01
  • is this any help: http://stackoverflow.com/questions/11576484/calculating-lean-angle-with-core-motion – Confused Sep 19 '16 at 13:04
  • I know... you've probably seen this... but just in case you haven't: http://stackoverflow.com/questions/25282543/mapping-physical-360-rotation-to-scenekit – Confused Sep 19 '16 at 13:06
  • And, if you can get his attention, I'm almost certain this guy will know a solution: http://stackoverflow.com/users/957768/rickster – Confused Sep 19 '16 at 13:10

0 Answers0