我计划开发一个基于陀螺仪的项目,如使用陀螺仪数据旋转一个openGL纹理,有没有从苹果发布的关于陀螺仪的示例代码,或者关于将陀螺仪与openGL集成的教程……我搜索了谷歌,除了岩心运动导轨和事件处理指南什么都没找到。
更新:请告诉我是否有可用的样本..
发布于 2010-10-16 18:19:16
要获得陀螺更新,您需要创建一个运动管理器对象和(但建议)一个参考姿态对象。
因此,在您的接口定义中添加:
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发布于 2011-09-30 23:26:29
我一直在寻找一些示例代码,作为一个非常简单的项目。经过几天的搜寻,我终于找到了。给你们!
http://cs491f10.wordpress.com/2010/10/28/core-motion-gyroscope-example/
发布于 2010-07-14 11:57:10
CoreMotion是如何获取陀螺仪数据的。查看CMGyrodata获取原始数据或使用DeviceMotion姿态和旋转速率属性。
如果你是一个注册的苹果开发者,我建议你看“设备运动”WWDC会议。
https://stackoverflow.com/questions/3245733
复制相似问题