btschumy
btschumy

Reputation: 1463

Using Core Motion to determine phone's Altitude and Azimuth

It would seem like this should be something easy, but I'm just not having any luck.

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º).

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.

- (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;
    }
}

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.

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.

Upvotes: 1

Views: 4320

Answers (1)

Kay
Kay

Reputation: 13146

In general your approach sounds reasonable to me. I think it is same thing like it is used in Teapot demo app. Maybe the easiest way is to check it out and have a look at it:

CMMotionManager and the Gyroscope on iPhone 4

If you plan to handle more complex motions in the future, I recommend the hard way i.e. quaternions. Yep, not that easy but if you once got it it's very convenient.

Upvotes: 1

Related Questions