1

看起来这应该很容易,但我只是没有运气。

我想要做的是使用核心运动来跟踪手机摄像头指向的位置(对于一种增强现实应用程序)。用户将手机指向一个参考点(就像给它拍照一样)并点击一个按钮以将手机的位置与对象“对齐”。现在,当用户将手机的摄像头指向其他物体时,我需要确定更改手机的高度(-90º 到 +90º)和方位角(0º 到 360º)。

CMAttitude 给出的欧拉角似乎不是我需要的。我尝试缓存用户点击“对齐”时获得的 CMAttitude。然后当用户移动手机时,我得到一个新的 CMAttitude 并使用 multiplyByInverseOfAttitude 来确定与参考姿态的差异。

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

但这并没有产生正确的结果。如果手机垂直(长轴向上),那么 deltaAlt 工作正常。只要手机指向地平线 (alt = 0) ,那么 deltaAzm 也是正确的。但是,如果手机指向(比如说)45º 的天空,那么当我在方位角移动手机时,高度分量也会发生变化。两者以我无法理解的方式交织在一起。

是否有一些简单的数学需要我将它们分开?同样,我认为这将是增强现实应用程序的必要需求,但我一直无法找到任何这样做的示例。

4

1 回答 1

1

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.

于 2011-02-01T22:27:24.800 回答