cocos2dx之Box2D

cocos2dx之Box2D       
 
Box2D是与Cocos2d-x一起发布的一套开源物理引擎,也是Cocos2d-x游戏需要使用物理引擎时的首选。二者同样提供C++开发接口,所使用的坐标系也一致,因此Box2D与Cocos2d-x几乎可以做到无缝对接。

Box2D是一套基于刚体模拟的物理引擎,它的核心概念为世界、物体、形状、约束和关节.


  1. void MyBox2DLayer::initLayer() { 
  2.     CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  3.     this->setTouchEnabled(true); 
  4.     this->setAccelerometerEnabled(true);// 设置加速键 
  5.     /*
  6.     *Init the box2d physics
  7.     */ 
  8.     initPhysics(); 
  9.     // 
  10.     CCSpriteBatchNode *batch = CCSpriteBatchNode::create("Images/ball-hd.png", 100); 
  11.     m_SpriteNode = batch->getTexture(); 
  12.  
  13.     this->addChild(batch, 0, KSPRITE_BATCH); 
  14.     addAnNewSprite(ccp(size.width / 2.0f, size.height / 2.0f)); 
  15.  
  16.     scheduleUpdate();// update the time,时刻更新物体状态 
void MyBox2DLayer::initLayer() {
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    this->setTouchEnabled(true);
    this->setAccelerometerEnabled(true);// 设置加速键
    /*
    *Init the box2d physics
    */
    initPhysics();
    //
    CCSpriteBatchNode *batch = CCSpriteBatchNode::create("Images/ball-hd.png", 100);
    m_SpriteNode = batch->getTexture();

    this->addChild(batch, 0, KSPRITE_BATCH);
    addAnNewSprite(ccp(size.width / 2.0f, size.height / 2.0f));

    scheduleUpdate();// update the time,时刻更新物体状态
}

  1. void MyBox2DLayer::initPhysics() { 
  2.     CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  3.     b2Vec2 gravity;// step: 1 
  4.  
  5.     /*
  6.     |^(y) gravity ,if the second param is negative number that the sprite is running down(y is decreasing)or is running up(y is increasing)
  7.     |
  8.     |
  9.     |
  10.     |
  11.     |________________________________________________________>(x)
  12.     (0, 0)
  13.     */ 
  14.  
  15.     gravity.Set(0.0f, -10.0f); // direction of the sprite runs,base on the GL position, 
  16.     world = new b2World(gravity); 
  17.  
  18.     //step: 2 
  19.     world->SetAllowSleeping(true); // pause all ,init the box2d world 
  20.     world->SetContinuousPhysics(true); // resume the box2d 
  21.  
  22.     //step: 3, defint the body 
  23.     b2BodyDef groundbody; 
  24.     groundbody.position.Set(0, 0); 
  25.     b2Body *body = world->CreateBody(&groundbody); 
  26.  
  27.     //step: 3, define the edge 
  28.     // define the 4 edges 
  29.     b2EdgeShape edgeBox; 
  30.     edgeBox.Set(b2Vec2(0, size.height / PTM_RATIO), b2Vec2(0, 0)); 
  31.     body->CreateFixture(&edgeBox, 0); 
  32.  
  33.     edgeBox.Set(b2Vec2(0, 0), b2Vec2(size.width / PTM_RATIO, 0)); 
  34.     body->CreateFixture(&edgeBox, 0); 
  35.   
  36.     edgeBox.Set(b2Vec2(0, size.height / PTM_RATIO), b2Vec2(size.width / PTM_RATIO, size.height / PTM_RATIO)); 
  37.     body->CreateFixture(&edgeBox, 0); 
  38.  
  39.     edgeBox.Set(b2Vec2(size.width / PTM_RATIO, size.height / PTM_RATIO), b2Vec2(size.width / PTM_RATIO, 0)); 
  40.     body->CreateFixture(&edgeBox, 0); 
  41.      
void MyBox2DLayer::initPhysics() {
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    b2Vec2 gravity;// step: 1

    /*
    |^(y) gravity ,if the second param is negative number that the sprite is running down(y is decreasing)or is running up(y is increasing)
    |
    |
    |
    |
    |________________________________________________________>(x)
    (0, 0)
    */

    gravity.Set(0.0f, -10.0f); // direction of the sprite runs,base on the GL position,
    world = new b2World(gravity);

    //step: 2
    world->SetAllowSleeping(true); // pause all ,init the box2d world
    world->SetContinuousPhysics(true); // resume the box2d

    //step: 3, defint the body
    b2BodyDef groundbody;
    groundbody.position.Set(0, 0);
    b2Body *body = world->CreateBody(&groundbody);

    //step: 3, define the edge
    // define the 4 edges
    b2EdgeShape edgeBox;
    edgeBox.Set(b2Vec2(0, size.height / PTM_RATIO), b2Vec2(0, 0));
    body->CreateFixture(&edgeBox, 0);

    edgeBox.Set(b2Vec2(0, 0), b2Vec2(size.width / PTM_RATIO, 0));
    body->CreateFixture(&edgeBox, 0);
 
    edgeBox.Set(b2Vec2(0, size.height / PTM_RATIO), b2Vec2(size.width / PTM_RATIO, size.height / PTM_RATIO));
    body->CreateFixture(&edgeBox, 0);

    edgeBox.Set(b2Vec2(size.width / PTM_RATIO, size.height / PTM_RATIO), b2Vec2(size.width / PTM_RATIO, 0));
    body->CreateFixture(&edgeBox, 0);
    
}

  1. void MyBox2DLayer::addAnNewSprite(const CCPoint &point) { 
  2.     CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  3.     CCNode* parent = getChildByTag(KSPRITE_BATCH); 
  4.     sprite = new MyBox2DSprite; 
  5.     sprite->initWithTexture(m_SpriteNode); 
  6.     sprite->setAnchorPoint(ccp(0.5f, 0.5f)); 
  7.     sprite->setPosition(point); 
  8.     parent->addChild(sprite, 1); 
  9.     sprite->retain();     
  10.  
  11.     // 
  12.     b2BodyDef bodydef; 
  13.     bodydef.type = b2_dynamicBody; // set the kind of the sprite 
  14.     bodydef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO); 
  15.  
  16.     //body 
  17.     b2Body *body = world->CreateBody(&bodydef); 
  18.      
  19.     //shape 
  20.     b2PolygonShape dynamicShape; 
  21.     dynamicShape.SetAsBox(.5f, .5f); 
  22.      
  23.     b2FixtureDef fixtureDef;//set the 精灵添加夹具 
  24.     fixtureDef.shape = &dynamicShape;     
  25.     fixtureDef.density = 1.0f; 
  26.     fixtureDef.friction = 0.3f; 
  27.     fixtureDef.restitution = 0.8f; 
  28.      
  29.     //set bodydef values; 
  30.     body->CreateFixture(&fixtureDef); 
  31.  
  32.     sprite->setPhysicsBody(body); 
  33.  
  34. void MyBox2DLayer::update(float dt) { 
  35.     int velocityIterations = 1; 
  36.     int positionIterations = 1; 
  37.  
  38.     world->Step(dt, velocityIterations, positionIterations); 
void MyBox2DLayer::addAnNewSprite(const CCPoint &point) {
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCNode* parent = getChildByTag(KSPRITE_BATCH);
    sprite = new MyBox2DSprite;
    sprite->initWithTexture(m_SpriteNode);
    sprite->setAnchorPoint(ccp(0.5f, 0.5f));
    sprite->setPosition(point);
    parent->addChild(sprite, 1);
    sprite->retain();    

    //
    b2BodyDef bodydef;
    bodydef.type = b2_dynamicBody; // set the kind of the sprite
    bodydef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);

    //body
    b2Body *body = world->CreateBody(&bodydef);
    
    //shape
    b2PolygonShape dynamicShape;
    dynamicShape.SetAsBox(.5f, .5f);
    
    b2FixtureDef fixtureDef;//set the 精灵添加夹具
    fixtureDef.shape = &dynamicShape;    
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution = 0.8f;
    
    //set bodydef values;
    body->CreateFixture(&fixtureDef);

    sprite->setPhysicsBody(body);
}

void MyBox2DLayer::update(float dt) {
    int velocityIterations = 1;
    int positionIterations = 1;

    world->Step(dt, velocityIterations, positionIterations);
}

  1. //物体 
//物体

  1. MyBox2DSprite::MyBox2DSprite() { 
  2.  
  3.  
  4. void MyBox2DSprite::setPhysicsBody(b2Body * body) { 
  5.     m_pBody = body; 
  6.  
  7. bool MyBox2DSprite::isDirty(void) { 
  8.     return true
  9.  
  10. CCAffineTransform MyBox2DSprite::nodeToParentTransform(void) { 
  11.     b2Vec2 pos  = m_pBody->GetPosition(); 
  12.  
  13.     float x = pos.x * PTM_RATIO; 
  14.     float y = pos.y * PTM_RATIO; 
  15.  
  16.     if ( isIgnoreAnchorPointForPosition() ) { 
  17.         x += m_tAnchorPointInPoints.x; 
  18.         y += m_tAnchorPointInPoints.y; 
  19.     } 
  20.  
  21.     // Make matrix 
  22.     float radians = m_pBody->GetAngle(); 
  23.     float c = cosf(radians); 
  24.     float s = sinf(radians); 
  25.  
  26.     if( ! CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero) ){ 
  27.         x += c*-m_tAnchorPointInPoints.x + -s*-m_tAnchorPointInPoints.y; 
  28.         y += s*-m_tAnchorPointInPoints.x + c*-m_tAnchorPointInPoints.y; 
  29.     } 
  30.  
  31.     // Rot, Translate Matrix 
  32.     m_tTransform = CCAffineTransformMake( c,  s, 
  33.         -s,    c, 
  34.         x,    y ); 
  35.  
  36.     return m_tTransform; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值