Currently I meet a problem on iOS platform. Basically, I load an arrow which points to east in 2D environment (upside is north, leftside is west, rightside is east and downside is south). I expect it can point to real east in 3D environment so that it will automatically rotates to the right direction. I draw a picture to describe my situation precisely. (The dotted arrow is my loaded arrow and the solid arrow is what I want if I use core motion data)
Now I did
let motionManager = CMMotionManager()
motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
if motionManager.isDeviceMotionAvailable {
motionManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: { (devMotion, error) -> Void in
parent_arrows[0].orientation = SCNQuaternion(-CGFloat((motionManager.deviceMotion?.attitude.quaternion.x)!), -CGFloat((motionManager.deviceMotion?.attitude.quaternion.y)!), -CGFloat((motionManager.deviceMotion?.attitude.quaternion.z)!), CGFloat((motionManager.deviceMotion?.attitude.quaternion.w)!))
})}
The snippet can't automatically make the arrow rotate to the right direction. My idea is to get the angle between the device and the north and then apply this angle to the arrow's orientation. But how can I add the angle to a quaternion? And is there any other idea to achieve this?