-
-
Save travisnewby/96ee1ac2bc2002f1d480 to your computer and use it in GitHub Desktop.
extension CMDeviceMotion { | |
func gaze(atOrientation orientation: UIInterfaceOrientation) -> SCNVector4 { | |
let attitude = self.attitude.quaternion | |
let aq = GLKQuaternionMake(Float(attitude.x), Float(attitude.y), Float(attitude.z), Float(attitude.w)) | |
let final: SCNVector4 | |
switch orientation { | |
case .landscapeRight: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(Float.pi / 2, 0, 1, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: -q.y, y: q.x, z: q.z, w: q.w) | |
case .landscapeLeft: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(-Float.pi / 2, 0, 1, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: q.y, y: -q.x, z: q.z, w: q.w) | |
case .portraitUpsideDown: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(Float.pi / 2, 1, 0, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: -q.x, y: -q.y, z: q.z, w: q.w) | |
case .unknown: | |
fallthrough | |
case .portrait: | |
fallthrough | |
@unknown default: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(-Float.pi / 2, 1, 0, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: q.x, y: q.y, z: q.z, w: q.w) | |
} | |
return final | |
} | |
} |
Thanks so much for this! One thing to point out: it looks like you should be switching on the orientation
parameter passed into the function, not UIApplication.sharedApplication().statusBarOrientation
This is awesome, thanks!!
Thank you Travis! This is super helpful!!
Great extension! My camera however is not updating. I can drag the view with my finger and it changes apporpriatly. But I want to be able to 'look around' the picture when the device moves.
Thank you for this.
Thanks for the code. It solved my problem, which I was facing for more than 3 months.
Thanks for your code, it solved my 3D game camera motion control perfectly.
Here is the StackOverflow raw problem.
And I think u can update it to Swift4 version :P
@travisnewby My math is very bad, could you please explain more detailed? Hopefully you can introduce the codes line by line, please forgive my foolishness.
Thank you very much!
And I think u can update it to Swift4 version :P
Done!
@travisnewby My math is very bad, could you please explain more detailed? Hopefully you can introduce the codes line by line, please forgive my foolishness.
@evanxlh The math is nothing more than applying a rotation – based on the orientation of your phone – to a vector. I can't explain the math as clearly as many of the youtube videos talking about quaternions for game developers. That's your best bet for understanding.
thank you for your "magic" extension.
@ostholz you’re welcome.
Thank you very much for this! Saves a lot of headaches!
You saved my life. Thank you so much for this.
I will post the magic this class will help em create.
Thank you again.
@99centbrains.