苹果陀螺仪示例代码 [英] Apple gyroscope sample code

查看:813
本文介绍了苹果陀螺仪示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划开发一个基于陀螺仪的项目,例如使用陀螺仪数据旋转 opengl纹理,是否有任何关于陀螺仪的示例代码或关于将陀螺仪与OpenGL集成的任何教程...搜索google我没有找到任何东西,除了核心运动指南事件处理指南

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



在你的接口定义中添加:

  CMMotionManager * motionManager; 
CMAttitude * referenceAttitude;

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



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

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

当您要启用运动更新时,您可以创建一个enableMotion方法,方法。以下将存储初始装置姿态并使装置继续采样陀螺仪并更新其姿态性质。

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


I am planning to develop an gyroscope based project like rotating an opengl texture using gyroscope data, is there any sample code released from apple about gyroscope or any tutorial about integrating gyroscope with openGL... I searched google i didn't find anything except core motion guide and event handling guide.

Updated: Please let me know if any sample available..

解决方案

To get gyro updates you need to create a motion manager object and optionally (but recommended) a reference attitude object

So in your interface definition you add:

CMMotionManager *motionManager;
CMAttitude *referenceAttitude;

According to the docs you should only create one of these managers per application. I recommend making the motionManager accesible through a singleton but thats some extra work which you might not need to do if you only instantiate your class once.

Then in your init method you should allocate the motion manager object like so;

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

When you want to enable the motion updates you could create an enableMotion method or just call it from the init method. The following will store the initial device attitude and cause the device to continue sampling the gyro and updating its attitude property.

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

For virtual reality applications using the gyro and OpenGL is pretty simple. You need to get the current gyro attitude (rotation) and then store it in an OpenGL compatible matrix. The code below retrieves and saves the current device motion.

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

Depending on what you want to do with that you may have to invert it which is very easy. The inverse of a rotation is just its transpose which means swapping the columns and rows. So the above becomes:

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;

If you want the yaw, pitch and roll angles then you can access them easily using

attitude.yaw
attitude.pitch
attitude.roll

这篇关于苹果陀螺仪示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆