CoreMotion--iOS中的陀螺仪用法
2013-04-03 09:24
337 查看
ios中有一个陀螺仪模块,CoreMotion
下面使用例子
下面使用例子
//初始化CoreMotion
#import <CoreMotion/CoreMotion.h>
CMMotionManager
*motionManager = [[
CMMotionManager
alloc
]init];
//1. Accelerometer 获取手机加速度数据
CMAccelerometerData
*newestAccel = motionManager
.accelerometerData
;
double
accelerationX = newestAccel
.acceleration
.x
;
double
accelerationY = newestAccel
.acceleration
.y
;
double
accelerationZ = newestAccel
.acceleration
.z
;
//2. Gravity 获取手机的重力值在各个方向上的分量,根据这个就可以获得手机的空间位置,倾斜角度等
double
gravityX = motionManager
.deviceMotion
.gravity
.x
;
double
gravityY = motionManager
.deviceMotion
.gravity
.y
;
double
gravityZ = motionManager
.deviceMotion
.gravity
.z
;
//获取手机的倾斜角度:
double
zTheta = atan
2
(gravityZ,sqrtf(gravityX*gravityX+gravityY*gravityY))/M_PI*
1
8
0
.0
;
double
xyTheta = atan
2
(gravityX,gravityY)/M_PI*
1
8
0
.0
;
//zTheta是手机与水平面的夹角, xyTheta是手机绕自身旋转的角度
//3. DeviceMotion 获取陀螺仪的数据 包括角速度,空间位置等
//旋转角速度:
CMRotationRate
rotationRate = motionManager
.deviceMotion
.rotationRate
;
double
rotationX = rotationRate
.x
;
double
rotationY = rotationRate
.y
;
double
rotationZ = rotationRate
.z
;
//空间位置的欧拉角(通过欧拉角可以算得手机两个时刻之间的夹角,比用角速度计算精确地多)
double
roll = motionManager
.deviceMotion
.attitude
.roll
;
double
pitch = motionManager
.deviceMotion
.attitude
.pitch
;
double
yaw = motionManager
.deviceMotion
.attitude
.yaw
;
//空间位置的四元数(与欧拉角类似,但解决了万向结死锁问题)
double
w = motionManager
.deviceMotion
.attitude
.quaternion
.w
;
double
wx = motionManager
.deviceMotion
.attitude
.quaternion
.x
;
double
wy = motionManager
.deviceMotion
.attitude
.quaternion
.y
;
double
wz = motionManager
.deviceMotion
.attitude
.quaternion
.z
;
//通过陀螺仪模块可以实现模拟赛车,模拟射击等。
相关文章推荐
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的用法
- ios中陀螺仪CoreMotion的用法
- iOS 6中NSString新用法
- IOS笔记(三)——Objective-C中的@property和@synthesize用法
- IOS开发中nil和release的用法
- iOS中复制对象的用法及深拷贝和浅拷贝详解
- iOS中的translucent和automaticallyAdjustsScrollViewInsets用法 - o.0...w - 博客园
- iOS 中json数据用法
- iOS 工作之余小总结(五)--(1) Block在程序中的三个用法
- iOS开发 Coretext基本用法, 讲得很好。
- IOS中 Block简介与用法(一)
- iOS - 中三种定时器的用法NSTimer、CADisplayLink、GCD
- iOS_陀螺仪、加速计的使用
- iOS Runtime 几种基本用法简记
- iOS Quartz 各种绘制图形用法--画图片、写文字、画线、椭圆、矩形、棱形等
- iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)
- iOS 陀螺仪 加速计
- iOS项目开发小技巧(二)--KVC在项目中常见用法