1. 程式人生 > >cocos2dx遊戲開發加速度計

cocos2dx遊戲開發加速度計

在cocos2d-x引擎中 使用了類CCAccelerometer來儲存加速度計的資訊 類CCAccelerometer的作用和使用者操作的分發器類似 區別在於使用者操作的分發器可以擁有很多委託物件 而加速度計只存在一個委託物件 這是因為一個移動裝置只有一個硬體 所以介面進行了簡化 CCAccelerometerDelegate就是加速度計的委託物件 

和觸控事件事件一樣 重力感應的處理先被引擎抽象為一個觸控代理的協議 然後由CCLayer提供了一個介面 在實際開發中 只需要過載加速度計事件即可

virtual void didAccelerate(CCAcceleration* pAccelerationValue);

CCAcceleration是一個結構體 包含加速度計獲得的三個方向的加速度

[cpp] view plaincopy
  1. typedef struct  
  2. {  
  3. double x;  
  4. double y;  
  5. double z;  
  6. double timestamp;  
  7. }CCAcceleration;  

為了便於遊戲中開發使用 每一個結構體中的每一個方向的加速度大小都是以一個重力加速度為單位9.8m/s的平方 舉例來說 當手機放置得桌子上的時候 獲得的加速度應該為(0,1,0)

 正常使用的時候 總的加速度應該在1上下波動 如果檢測到一個大幅度的偏離1 可以判斷為突然動作:手搖手機 會在一個或者多個方向上出現出很大的加速度 投擲或者墜落則很容易檢測到一個很小的加速度 


[cpp] view plaincopy
  1. void AccelerometerTest::onEnter()  
  2. {  
  3.     CCLayer::onEnter();  
  4.   
  5.     setAccelerometerEnabled(true);//想要當前CCNode物件可以接收加速度計的操作資訊 必須在初始化函式中呼叫此函式  
  6.   
  7.   
  8.     CCLabelTTF* label = CCLabelTTF::create(title().c_str(), "Arial", 32);  
  9.     addChild(label, 1);  
  10.     label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) );  
  11.   
  12.     m_pBall = CCSprite::create("Images/ball.png");  
  13.     m_pBall->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y));  
  14.     addChild(m_pBall);  
  15.   
  16.     m_pBall->retain();  
  17. }  

重寫didAccelerate這個函式 獲得CCAcceleration結構體資訊 獲得X,Y,Z方向上的加速度 分別乘以重力加速度獲得相應的加速度

[cpp] view plaincopy
  1.   
[cpp] view plaincopy
  1. void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)  
  2. {  
  3. //     double fNow = pAccelerationValue->timestamp;  
  4. //   
  5. //     if (m_fLastTime > 0.0)  
  6. //     {  
  7. //         CCPoint ptNow = convertToUI  
  8. //     }  
  9. //   
  10. //     m_fLastTime = fNow;  
  11.   
  12.     CCDirector* pDir = CCDirector::sharedDirector();  
  13.   
  14.       
  15.     if ( m_pBall == NULL ) {  
  16.         return;  
  17.     }  
  18.   
  19.     CCSize ballSize  = m_pBall->getContentSize();  
  20.   
  21.     CCPoint ptNow  = m_pBall->getPosition();  
  22.     CCPoint ptTemp = pDir->convertToUI(ptNow);  
  23.   
  24.     ptTemp.x += pAccelerationValue->x * 9.81f;  
  25.     ptTemp.y -= pAccelerationValue->y * 9.81f;  
  26.   
  27.     CCPoint ptNext = pDir->convertToGL(ptTemp);  
  28.     FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));  
  29.     FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));  
  30.     m_pBall->setPosition(ptNext);  
  31. }  
  32.   
  33. //------------------------------------------------------------------  
  34. //  
  35. // AccelerometerTestScene  
  36. //  
  37. //------------------------------------------------------------------  
  38. void AccelerometerTestScene::runThisTest()  
  39. {  
  40.     CCLayer* pLayer = new AccelerometerTest();  
  41.     addChild(pLayer);  
  42.     pLayer->release();  
  43.   
  44.     CCDirector::sharedDirector()->replaceScene(this);  
  45. }  



初始化加速度計資訊: [cpp] view plaincopy
  1. class CC_DLL CCAccelerometer  
  2. {  
  3. public:  
  4.     CCAccelerometer();  
  5.     ~CCAccelerometer();  
  6.   
  7.     void setDelegate(CCAccelerometerDelegate* pDelegate);  
  8.     void setAccelerometerInterval(float interval);  
  9.     void update( double x,double y,double z,double timestamp );  
  10. private:  
  11.     CCAcceleration m_obAccelerationValue;  
  12.     CCAccelerometerDelegate* m_pAccelDelegate;  
  13. };  
  14.   
  15. NS_CC_END  

[cpp] view plaincopy
  1. CCAccelerometer::CCAccelerometer() :   
  2.     m_pAccelDelegate(NULL)  
  3. {  
  4.     memset(&m_obAccelerationValue, 0, sizeof(m_obAccelerationValue));  
  5. }  
  6.   
  7. CCAccelerometer::~CCAccelerometer()   
  8. {  
  9.   
  10. }  
  11.   
  12. void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)   
  13. {  
  14.     m_pAccelDelegate = pDelegate;  
  15.   
  16.     // Enable/disable the accelerometer.  
  17.     // Well, there isn't one on Win32 so we don't do anything other than register  
  18.     // and deregister ourselves from the Windows Key handler.  
  19.     if (pDelegate)  
  20.     {  
  21.         // Register our handler  
  22.         CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( &myAccelerometerKeyHook );  
  23.     }  
  24.     else  
  25.     {  
  26.         // De-register our handler  
  27.         CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( NULL );  
  28.         resetAccelerometer();  
  29.     }  
  30. }  
  31.   
  32. void CCAccelerometer::setAccelerometerInterval(float interval)  
  33. {  
  34.   
  35. }  
  36.   
  37. void CCAccelerometer::update( double x,double y,double z,double timestamp )   
  38. {  
  39.     if (m_pAccelDelegate)  
  40.     {  
  41.         m_obAccelerationValue.x            = x;  
  42.         m_obAccelerationValue.y            = y;  
  43.         m_obAccelerationValue.z            = z;  
  44.         m_obAccelerationValue.timestamp = timestamp;  
  45.   
  46.         // Delegate  
  47.         m_pAccelDelegate->didAccelerate(&m_obAccelerationValue);  
  48.     }      
  49. }  
  50.   
  51. NS_CC_END