【问题标题】:Apple gyroscope sample code苹果陀螺仪示例代码
【发布时间】:2011-03-15 19:34:06
【问题描述】:

我正计划开发一个基于陀螺仪的项目,例如使用陀螺仪数据旋转 opengl 纹理,有没有苹果发布的关于陀螺仪的示例代码或任何关于将陀螺仪与 openGL 集成的教程...我谷歌搜索除了core motion guideevent handling guide,我什么也没找到。

更新:如果有可用的示例,请告诉我..

【问题讨论】:

标签: iphone cocoa-touch ios4


【解决方案1】:

要获得陀螺仪更新,您需要创建一个运动管理器对象和可选(但推荐)一个参考姿态对象

所以在你的接口定义中添加:

CMMotionManager *motionManager;
CMAttitude *referenceAttitude;

根据文档,您应该只为每个应用程序创建这些管理器之一。我建议通过单例使 motionManager 可访问,但如果您只实例化您的类一次,那可能不需要做一些额外的工作。

然后在您的 init 方法中,您应该像这样分配运动管理器对象;

motionManager = [[CMMotionManager alloc] init];
referenceAttitude = nil; 

当您想要启用运动更新时,您可以创建一个 enableMotion 方法或仅从 init 方法调用它。下面将存储初始设备姿态并导致设备继续采样陀螺并更新其姿态属性。

-(void) enableMotion{
        CMDeviceMotion *deviceMotion = motionManager.deviceMotion;      
        CMAttitude *attitude = deviceMotion.attitude;
        referenceAttitude = [attitude retain];
        [motionManager startDeviceMotionUpdates];
}

对于虚拟现实应用程序,使用陀螺仪和 OpenGL 非常简单。 您需要获取当前的陀螺姿态(旋转),然后将其存储在 OpenGL 兼容矩阵中。下面的代码检索并保存当前设备运动。

GLfloat rotMatrix[16];

-(void) getDeviceGLRotationMatrix 
{
        CMDeviceMotion *deviceMotion = motionManager.deviceMotion;      
        CMAttitude *attitude = deviceMotion.attitude;

        if (referenceAttitude != nil) [attitude multiplyByInverseOfAttitude:referenceAttitude];
        CMRotationMatrix rot=attitude.rotationMatrix;
        rotMatrix[0]=rot.m11; rotMatrix[1]=rot.m21; rotMatrix[2]=rot.m31;  rotMatrix[3]=0;
        rotMatrix[4]=rot.m12; rotMatrix[5]=rot.m22; rotMatrix[6]=rot.m32;  rotMatrix[7]=0;
        rotMatrix[8]=rot.m13; rotMatrix[9]=rot.m23; rotMatrix[10]=rot.m33; rotMatrix[11]=0;
        rotMatrix[12]=0;      rotMatrix[13]=0;      rotMatrix[14]=0;       rotMatrix[15]=1;
}

根据您想要做什么,您可能需要反转它,这很容易。 旋转的逆只是它的转置,这意味着交换列和行。 于是上面就变成了:

rotMatrix[0]=rot.m11; rotMatrix[4]=rot.m21; rotMatrix[8]=rot.m31;  rotMatrix[12]=0;
rotMatrix[1]=rot.m12; rotMatrix[5]=rot.m22; rotMatrix[9]=rot.m32;  rotMatrix[13]=0;
rotMatrix[2]=rot.m13; rotMatrix[6]=rot.m23; rotMatrix[10]=rot.m33; rotMatrix[14]=0;
rotMatrix[3]=0;       rotMatrix[7]=0;       rotMatrix[11]=0;       rotMatrix[15]=1;

如果您想要偏航角、俯仰角和滚动角,那么您可以使用

轻松访问它们
attitude.yaw
attitude.pitch
attitude.roll

【讨论】:

  • 有谁知道在所有这些代码之后,我如何简单地获取陀螺仪的 x、y、z 值?简单地?没有矩阵之类的东西?
  • @Rant 你需要用陀螺仪做什么?你只想要你面对的方向吗?
  • 我喜欢这个问题上 GLfloat 矩阵的格式:stackoverflow.com/questions/4449565/… GLfloat rotMat[] = { mat.m11, mat.m21, mat.m31, 0, mat.m12, mat.m22 , mat.m32, 0, mat.m13, mat.m23, mat.m33, 0, 0, 0, 0, 1 };
  • @twerdster 我能知道如何使用你的概念来解决这个问题吗stackoverflow.com/questions/11106102/…
  • deviceMotion 总是为空,我错过了什么吗?
【解决方案2】:
【解决方案3】:

我一直在寻找一些示例代码作为一个非常简单的项目。找了好几天,终于找到了。给你们!

http://cs491f10.wordpress.com/2010/10/28/core-motion-gyroscope-example/

【讨论】:

    【解决方案4】:

    CoreMotion 是如何获取陀螺仪数据的。查看 CMGyrodata 获取原始数据或使用 DeviceMotion 姿态和旋转速率属性。

    如果您是注册的 Apple 开发者,我建议您观看“Device Motion”WWDC 会议。

    【讨论】:

    • 不,它根据保密协议,因此只有付费的开发人员才能获得它,并且其中的示例代码不能公开复制。可能要过一段时间才会有很多示例陀螺仪代码,如果你有的话,可能值 99 美元。
    • 这些视频不需要付费的 iPhone 开发者。使用与下载 sdk 相同的苹果开发者帐户。在实际设备上进行测试需要付费会员。鉴于您的目标是硬件功能,您将不得不升级。
    • 这就是说,有些视频仍然保密。在每一行的底部,有以下一行:"These are confidential sessions -- please refrain from streaming, blogging, or taking pictures"。此外,在 WWDC 视频网页的左栏中,它显示"The content presented within the session videos and slides is Apple Confidential Information and is subject to the Registered Apple Developer Agreement."。 Apple 希望这些视频仅供开发者观看(免费或付费),因为他们需要同意他们的条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多