
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-01-21 标题cocos2dx学习笔记陀螺仪的使用分类编程 / C C / cocos2dx 标签cocos2dx·陀螺仪cocos2dx学习笔记陀螺仪的使用陀螺仪的使用备注最近项目中用到了cocos2dx来开发一个基于陀螺仪的demo忙了半个星期一边学习一边开发总算搞了出来。这里备份下开发中学习到的一些cocos2dx知识。陀螺仪的使用demo的核心就是陀螺仪的使用怎么用这里就不说了楼主语文不好大家可以参考另一款APP【 疯狂射手 】我的DEMO就是仿照它的。整理后的代码如下.h文件#ifndef__MOTION_CONTROL_H__#define__MOTION_CONTROL_H__#includecocos2d.hclassMotionControl{public:virtualboolinit(void);voidrelease();voidsetbaseMotion(void);voidupdateMotion(void);floatgetYawOffset(void){returnm_fYawOffset;}floatgetRollOffset(void){returnm_fRollOffset;}floatgetPitchOffset(void){returnm_fPitchOffset;}private:void*getCurrAttitude();void*m_pMotionManger;boolm_bAvailable;void*m_BaseAttitude;floatm_fYaw;floatm_fRoll;floatm_fPitch;floatm_fLastYaw;floatm_fLastRoll;floatm_fLastPitch;floatm_fYawOffset;floatm_fRollOffset;floatm_fPitchOffset;boolm_bIsMotionUpdate;};#endif//___MOTION_CONTROL_H__.m文件#includeMotionControl.h#includeCoreMotion/CoreMotion.h#importCoreFoundation/CoreFoundation.hUSING_NS_CC;boolMotionControl::init(void){CMMotionManager*motionManager[[CMMotionManager alloc]init];motionManager.deviceMotionUpdateInterval1.0/60.f;if(motionManager.isDeviceMotionAvailable){;}m_BaseAttitudeNULL;m_pMotionManger(void*)motionManager;setbaseMotion();returntrue;}voidMotionControl::release(){if(m_pMotionManger!NULL){CMMotionManager*tmpMotion(CMMotionManager*)m_pMotionManger;;m_pMotionMangerNULL;}if(m_BaseAttitude!NULL){CMAttitude*tmpAttitude(CMAttitude*)m_BaseAttitude;;m_BaseAttitudeNULL;}}void*MotionControl::getCurrAttitude(){CMMotionManager*temMotion(CMMotionManager*)m_pMotionManger;CMDeviceMotion*currentDeviceMotiontemMotion.deviceMotion;CMAttitude*currentAttitudecurrentDeviceMotion.attitude;returncurrentAttitude;}voidMotionControl::setbaseMotion(void){if(m_BaseAttitude!NULL){CMAttitude*tmpAttitude(CMAttitude*)m_BaseAttitude;;m_BaseAttitudeNULL;}CMAttitude*currentAttitude(CMAttitude*)getCurrAttitude();m_BaseAttitude;m_bIsMotionUpdatefalse;m_fYawOffset0;m_fRollOffset0;m_fPitchOffset0;}voidMotionControl::updateMotion(void){if(NULLm_BaseAttitude){setbaseMotion();}CMAttitude*currentAttitude(CMAttitude*)getCurrAttitude();;m_fYawCC_RADIANS_TO_DEGREES(currentAttitude.yaw);m_fRollCC_RADIANS_TO_DEGREES(currentAttitude.roll);m_fPitchCC_RADIANS_TO_DEGREES(currentAttitude.pitch);if(!m_bIsMotionUpdate){m_bIsMotionUpdatetrue;m_fLastYawm_fYaw;m_fLastRollm_fRoll;m_fLastPitchm_fPitch;}m_fYawOffsetm_fYaw-m_fLastYaw;m_fRollOffsetm_fRoll-m_fLastRoll;m_fPitchOffsetm_fPitch-m_fLastPitch;m_fLastYawm_fYaw;m_fLastRollm_fRoll;m_fLastPitchm_fPitch;}备注作者当时比较赶项目结果欲速而不达了在网上搜索的各种代码均不靠谱反而浪费了时间最后还是老老实实的看了下apple的文档结果发现很简单。这里备份下几个网址以备不时之需。【 http://blog.csdn.net/aiyongyyy/article/details/7986810http://www.cnblogs.com/binbingg/p/3442624.html 】【 http://blog.csdn.net/cocos2der/article/details/7030384 】【 http://blog.sina.com.cn/s/blog_7b9d64af0101cu4p.html 】apple的文档【 https://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMAttitude_Class/Reference/Reference.html 】【 https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html#//apple_ref/doc/uid/TP40009541-CH6-SW1 】