title: Using Core Motion to determine phone's Altitude and Azimuth
body: <p>It would seem like this should be something easy, but I'm just not having any luck.</p>
  
  <p>What I want to be able to do is use core motion to keep track of where the phone's camera is being pointed at (for a sort of augmented reality app).  The user would point the phone to a reference point (as if taking a picture of it) and hit a button to "align" the phone's position with the object.  Now as the user points the phone's camera towards other objects, I need to determine the change phone's altitude (-90º to +90º) and azimuth (0º to 360º).</p>
  
  <p>The Euler angles given by CMAttitude do not appear to be what I need.  I've tried caching the CMAttitude obtained when the user hits "align".  Then as the user moves the phone around, I get a new CMAttitude and use the multiplyByInverseOfAttitude to determine the difference from the reference attitude.</p>
  
  <pre><code>- (void)checkForMotion {
      CMMotionManager *motionMgr = appDelegate.motionMgr;
      CMDeviceMotion *deviceMotion = motionMgr.deviceMotion;
      CMAttitude *attitude = deviceMotion.attitude;
  
      if (referenceAttitude != nil) 
      {
          [attitude multiplyByInverseOfAttitude: referenceAttitude];
  
          double deltaAlt = attitude.pitch;
          double deltaAzm = attitude.roll;
  
          // Do something with deltaAlt and deltaAz
      }
      else 
      {
          NSLog(@"Getting new referenceAttitude");
          self.referenceAttitude = attitude;
      }
  }
  </code></pre>
  
  <p>But this just isn't yielding the right results.  If the phone is held vertically (long axis up) then the deltaAlt works fine.  As long as the phone is pointed at the horizon (alt = 0) , then the deltaAzm is correct as well.  However, if the phone if pointed (say) 45º into the sky, then as I move the phone in azimuth, the altitude component is changed as well.  The two are intertwined in a way I can't understand.</p>
  
  <p>Is there some simple math that I need to tease these apart?  Again, I would think this would be a necessary need for an augmented reality app, but I've been unable to find any examples that do this.</p>