cocos2d-x 之 CCPhysicsSprite 基于物理引擎的Sprite

勤奋努力,持之以恒!

box2d简单介绍可参阅:http://blog.csdn.net/song_hui_xiang/article/details/34116399

写这篇文章主要是说明:对一个刚体施加一个力,用 GetWorldVector() 与不用的区别。刚开始学习box2d,因为这个问题被虐了好几天,贴出来一是分享,二是备忘。下面直接上代码:

.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  Game.h  
  3. //  HeroJump  
  4. //  
  5. //  Created by XiangZi on 14-6-27.  
  6. //  
  7. //  
  8.   
  9. #ifndef __HeroJump__Game__  
  10. #define __HeroJump__Game__  
  11.   
  12. #include "cocos2d.h"  
  13. #include "Box2D.h"  
  14. #include "cocos-ext.h"//用CCPhysicsSprite类需要导入此头文件  
  15. using namespace std;  
  16. USING_NS_CC_EXT;  
  17. USING_NS_CC;  
  18.   
  19. enum GameTag{  
  20.     kTag,  
  21. };  
  22. enum MenuTag{  
  23.       
  24. };  
  25. enum GamezOrder{  
  26.     zOrder,  
  27. };  
  28.   
  29. class Game : public CCLayer {  
  30. public:  
  31.     ~Game();  
  32.     Game();  
  33.     static CCScene* scene();  
  34.     void initPhysics();  
  35.     virtual void draw();  
  36.     void update(float dt);  
  37.     CCPhysicsSprite* createNewPhysicsSprite(const char *imageName);  
  38.       
  39.     virtual void onEnter();  
  40.     virtual void onExit();  
  41.     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  42.     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  
  43.     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  
  44. private:  
  45.     b2World* world;  
  46.     CCSize size;  
  47. };  
  48.   
  49. #endif /* defined(__HeroJump__Game__) */  


.cpp

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  Game.cpp  
  3. //  HeroJump  
  4. //  
  5. //  Created by XiangZi on 14-6-27.  
  6. //  
  7. //  
  8.   
  9. #include "Game.h"  
  10.   
  11. #define PTM_RATIO 32  
  12.   
  13. Game::Game()  
  14. {  
  15.     setAccelerometerEnabled( true );  
  16.     size = CCDirector::sharedDirector()->getWinSize();  
  17.     //创建物理世界  
  18.     b2Vec2 gravity;  
  19.     gravity.Set(0.0f, -10.0f);  
  20.     world = new b2World(gravity);  
  21.     world->SetAllowSleeping(true);  
  22.     world->SetContinuousPhysics(true);  
  23.       
  24.     b2BodyDef groundBodyDef;  
  25.     groundBodyDef.position.Set(0, 0);  
  26.     b2Body* groundBody = world->CreateBody(&groundBodyDef);  
  27.     b2EdgeShape groundBox;  
  28.     // bottom  
  29.     groundBox.Set(b2Vec2(0,0), b2Vec2(size.width/PTM_RATIO,0));  
  30.     groundBody->CreateFixture(&groundBox,0);  
  31.     // top  
  32.     groundBox.Set(b2Vec2(0,size.height/PTM_RATIO), b2Vec2(size.width/PTM_RATIO,size.height/PTM_RATIO));  
  33.     groundBody->CreateFixture(&groundBox,0);  
  34.     // left  
  35.     groundBox.Set(b2Vec2(0,size.height/PTM_RATIO), b2Vec2(0,0));  
  36.     groundBody->CreateFixture(&groundBox,0);  
  37.     // right  
  38.     groundBox.Set(b2Vec2(size.width/PTM_RATIO,size.height/PTM_RATIO), b2Vec2(size.width/PTM_RATIO,0));  
  39.     groundBody->CreateFixture(&groundBox,0);  
  40.       
  41.     this->initPhysics();  
  42.     scheduleUpdate();  
  43. }  
  44. Game::~Game()  
  45. {  
  46.     delete world;  
  47.     world = NULL;  
  48. }  
  49.   
  50. void Game::initPhysics()  
  51. {  
  52.     CCPhysicsSprite* sprite = createNewPhysicsSprite("Icon.png");  
  53.     sprite->setPosition(ccp(size.width/2, size.height/2));  
  54.     this->addChild(sprite,1,1234);  
  55. }  
  56.   
  57. //封装此方法用于创建一物理精灵  
  58. CCPhysicsSprite* Game::createNewPhysicsSprite(const char *imageName)  
  59. {  
  60.     CCPhysicsSprite* physicsSprite = CCPhysicsSprite::create(imageName);  
  61.       
  62.     b2BodyDef bodyDef;  
  63.     bodyDef.type = b2_dynamicBody;  
  64.       
  65.     b2Body* body = world->CreateBody(&bodyDef);  
  66.       
  67.     b2PolygonShape polygonShape;  
  68.     polygonShape.SetAsBox(physicsSprite->getContentSize().width/PTM_RATIO/2, physicsSprite->getContentSize().height/PTM_RATIO/2);  
  69.       
  70.     b2FixtureDef fixtureDef;  
  71.     fixtureDef.shape = &polygonShape;  
  72.     fixtureDef.density = 5.5f;  
  73.     fixtureDef.friction = 0.3f;  
  74.     fixtureDef.restitution = 0.1f;  
  75.     body->CreateFixture(&fixtureDef);  
  76.       
  77.     physicsSprite->setPTMRatio(PTM_RATIO);  
  78.     physicsSprite->setB2Body(body);  
  79.       
  80.     return physicsSprite;  
  81. }  
  82.   
  83.   
  84. bool Game::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  85. {  
  86.     CCPoint point = pTouch->getLocation();  
  87.     CCPhysicsSprite* sprite = (CCPhysicsSprite*)this->getChildByTag(1234);  
  88.     b2Body* body = sprite->getB2Body();  
  89.       
  90.     float gravityX = 5.0f;  
  91.     float gravityY = 10.0f;  
  92.       
  93.     //给body施加一个力。 GetWorldCenter()方法用来获取刚体的重心。  
  94.     //b2Vec2 force = body->GetWorldVector(b2Vec2(-gravityX,gravityY));  以此body的坐标系计算向量,body有转动向量方向也会随之转动。  
  95.     b2Vec2 force = b2Vec2(-gravityX,gravityY); //以整个wrold世界的坐标系计算向量,不随单个body装动而转动。  
  96.       
  97.     if (point.x < size.width/2) {//点击屏幕左半边给予一个左上的速度  
  98.         force.x = -gravityX;  
  99.     }else{//点击屏幕右半边给予一个右上的速度  
  100.         force.x = gravityX;  
  101.     }  
  102.     //直接影响刚体的速度。setLinearVelocity添加的速度会覆盖刚体原有的速度。  
  103.     body->SetLinearVelocity(b2Vec2_zero);  
  104.     body->SetAngularVelocity(0);  
  105.     body->SetAwake(true);  
  106.     body->SetLinearVelocity(force);  
  107.       
  108.     return true;  
  109. }  
  110. void Game::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}  
  111. void Game::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){}  
  112.   
  113. void Game::onEnter()  
  114. {  
  115.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);  
  116.     CCLayer::onEnter();  
  117. }  
  118. void Game::onExit()  
  119. {  
  120.     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  
  121.     CCLayer::onExit();  
  122. }  
  123. void Game::update(float dt)  
  124. {  
  125.     int velocityIterations = 8;  
  126.     int positionIterations = 1;  
  127.     world->Step(1.0f/60.0f, velocityIterations, positionIterations);  
  128. }  
  129. void Game::draw()  
  130. {  
  131.     CCLayer::draw();  
  132.     ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );  
  133.     kmGLPushMatrix();  
  134.     world->DrawDebugData();  
  135.     kmGLPopMatrix();  
  136. }  
  137. CCScene* Game::scene()  
  138. {  
  139.     CCScene *scene = CCScene::create();  
  140.     CCLayer* layer = new Game();  
  141.     scene->addChild(layer);  
  142.     layer->release();  
  143.     return scene;  
  144. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值