[cocos2d-x学习笔记][入门基础]Box-2d物理引擎的使用02制作一个简易的愤怒小鸟Demo

在地图层创建一个游戏中出现的树杈发射器如图

部分代码如下

 //创建树杈
    auto tree01=Sprite::create("leftshot.png");
    this->addChild(tree01);
    tree01->setPosition(Vec2(200,200));
    tree01->setScale(4);
    auto tree02=Sprite::create("rightshot.png");
    tree02->setPosition(Vec2(200,200));
    tree02->setScale(4);
    this->addChild(tree02);

2.创建世界,初始化世界,定义地板绑定夹具(详情请看01);

    //创建地板
    b2BodyDef groundDef;
    groundDef.position.Set(.0f, 50/32.0f);
    b2Body *groundbody=world->CreateBody(&groundDef);
    //创建夹具定义刚体形状
    b2PolygonShape groundShop;
    groundShop.SetAsBox(visibleSize.width/32, 50/32);
    //刚体绑定夹具
    groundbody->CreateFixture(&groundShop,0);
    

3.添加敌人小猪

添加小猪的动态刚体并且绑定图层

void HelloWorld::addPigAtPosition(Vec2 p)
{
    //刚体
    b2BodyDef bodyDef;
    bodyDef.type=b2_dynamicBody;//??
    bodyDef.position.Set(p.x/32, p.y/32);
    b2Body *body=world->CreateBody(&bodyDef);
    b2PolygonShape dynamicBox;
    b2CircleShape d;
    d.m_radius=5;//??
    dynamicBox.SetAsBox(50/32.0/2.0f, 50/32.0/2.0f);
    b2FixtureDef fixtureDef;
    fixtureDef.shape=&dynamicBox;
    fixtureDef.density=1.0f;
    fixtureDef.friction=3.0f;
    fixtureDef.restitution=0.5;
    body->CreateFixture(&fixtureDef);
    //绑定图层
    auto sprite = Sprite::create("pig_2.png");
    sprite->setTag(10+pigIndex);
    sprite->setScale(0.5);
    this->addChild(sprite);
    body->SetUserData(sprite);//将这个新的Body中绑定精灵Sprite
    sprite->setPosition( Vec2( p.x, p.y) );
    pigIndex++;
    
}

在场景中添加小猪

void HelloWorld::addObstacles()
{
            pigIndex=0;
            this->addPigAtPosition(Vec2(700,0));
            this->addPigAtPosition(Vec2(700,32));
            this->addPigAtPosition(Vec2(700,64));
            this->addPigAtPosition(Vec2(700,96));
            this->addPigAtPosition(Vec2(700,128));
            this->addPigAtPosition(Vec2(700,160));
            //    this->addPigAtPosition(Vec2(700,192));
            //    this->addPigAtPosition(Vec2(700,224));
            //    this->addPigAtPosition(Vec2(700,256));
            //
            this->addPigAtPosition(Vec2(750,0));
            this->addPigAtPosition(Vec2(750,32));
            this->addPigAtPosition(Vec2(750,64));
            this->addPigAtPosition(Vec2(750,96));
            this->addPigAtPosition(Vec2(750,128));
            this->addPigAtPosition(Vec2(750,160));
            //    this->addPigAtPosition(Vec2(750,192));
            //    this->addPigAtPosition(Vec2(750,224));
            //    this->addPigAtPosition(Vec2(750,256));
            
            this->addPigAtPosition(Vec2(800,0));
            this->addPigAtPosition(Vec2(800,32));
            this->addPigAtPosition(Vec2(800,64));
            this->addPigAtPosition(Vec2(800,96));
            this->addPigAtPosition(Vec2(800,128));
            this->addPigAtPosition(Vec2(800,160));
            }

模拟世界的实现

 this->scheduleUpdate();

void HelloWorld::update(float t){
    world->Step(t, 8, 3);//模拟物理世界
    //获取所有刚体的信息
    for (b2Body* b=world->GetBodyList(); b; b=b->GetNext()) {
        if (b->GetUserData()) {//如果当前的body绑定了精灵
            Sprite * sp=(Sprite *)b->GetUserData();
            sp->setPosition(b->GetPosition().x*32,
                            b->GetPosition().y*32);
            sp->setRotation(-CC_RADIANS_TO_DEGREES(b->GetAngle()));
        }
    }
}
效果图:

4.添加愤怒的小鸟

点击屏幕在树杈中间出现一只小鸟。可以随着鼠标移动。

创建小鸟

void  HelloWorld::addNewSpriteAtPoint(Vec2 p){
    
    auto sprite = Sprite::create("bird1.png");
    sprite->setTag(110+birdIndex);
    sprite->setPosition(Vec2(200,200));
    this->addChild(sprite);
    sprite->setPosition( Vec2( p.x, p.y) );
}
屏幕交互
 //屏幕交互
    auto listener=EventListenerTouchOneByOne::create();
    listener->onTouchBegan=[&](Touch *t,Event *e){
        //birdBody
        addNewSpriteAtPoint(Vec2(200,200));
        this->getChildByTag(110+birdIndex)->setPosition(200,200);
        startx=t->getLocation().x;
        starty=t->getLocation().y;
        m_isFlying=false;
        return true;
    };
    listener->onTouchMoved=[&](Touch *t,Event *e){
        //        addNewSpriteAtPoint(t->getLocation());
        this->getChildByTag(110+birdIndex)->setPosition(200+t->getLocation().x-startx,
                                                        200+t->getLocation().y-starty);
        
        
    };
    listener->onTouchEnded=[&](Touch *t,Event *e){
        this->shootBirdAtPosition(t->getLocation());
        birdIndex++;
        m_isFlying=true;
    
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    //添加小鸟

效果图

5.将小鸟的图层与动态刚体绑定,并射出,撞到小猪

void HelloWorld::shootBirdAtPosition(Vec2 p)
{ //Set up a 1m squared box in the physics world
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;//动态刚体
    bodyDef.position.Set(p.x/32, p.y/32);
    b2Body *body = world->CreateBody(&bodyDef);
    this->birdBody=body;
    // Define another box shape for our dynamic body.
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(50/32.0/2.0f, 50/32.0/2.0f);//These are mid points for our 1m box
    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution=0.5;
    body->CreateFixture(&fixtureDef);
    
    body->SetUserData(this->getChildByTag(110+birdIndex));//将这个新的Body中绑定精灵Sprite
    b2Vec2 force = b2Vec2((200-p.x)/3, (200-p.y)/3);
    birdBody->ApplyLinearImpulse(force, birdBody->GetPosition(),true);
    
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值