溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

cocos2d-x之box2d使用筆記

發(fā)布時(shí)間:2020-08-01 11:57:14 來源:網(wǎng)絡(luò) 閱讀:1331 作者:xuzw13 欄目:游戲開發(fā)

   下午學(xué)了一下box2d相關(guān)的內(nèi)容,這里做個(gè)筆記。

1、創(chuàng)建頭文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "GLES-Render.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer
{
public:
   HelloWorld();
   ~HelloWorld();
                     
    static cocos2d::CCScene* scene();
    CCSize _screenSize;
    void initPhysics();
    virtual void draw();
    void update(float dt); 
    virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
    void addNewSpriteAtPosition(CCPoint p);
private:
    cocos2d::CCTexture2D* m_pSpriteTexture;
    b2World* world;
    GLESDebugDraw* m_debugDraw;
};
#endif // __HELLOWORLD_SCENE_H__


2、編寫實(shí)現(xiàn)cpp

#include "HelloWorldScene.h"
#include "VisibleRect.h"
USING_NS_CC;
#define PTM_RATIO 32.0f
enum{
    kTagParentNode = 1
};
HelloWorld::HelloWorld():m_pSpriteTexture(NULL),world(NULL){
    _screenSize = CCDirector::sharedDirector()->getVisibleSize();
    setTouchEnabled(true);
    this->initPhysics();
    //使用批處理方式加載圖片
    CCSpriteBatchNode *parent = CCSpriteBatchNode::create("blocks.png",100);
    m_pSpriteTexture = parent->getTexture();
    addChild(parent,0,kTagParentNode);
    //在中間添加一個(gè)精靈
    addNewSpriteAtPosition(ccp(_screenSize.width/2,_screenSize.height/2));
    scheduleUpdate();
}
HelloWorld::~HelloWorld(){
    delete world;
    world = NULL;
    delete m_debugDraw;
}
void HelloWorld::initPhysics(){
    b2Vec2 gravity;
    gravity.Set(0.0f,-10.0f);
    //創(chuàng)建世界
    world = new b2World(gravity);
    //允許睡眠
    world->SetAllowSleeping(true);
    world->SetContinuousPhysics(true);
    //創(chuàng)建GLESDebugDraw
     m_debugDraw = new GLESDebugDraw( PTM_RATIO );
     world->SetDebugDraw(m_debugDraw);
    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
    flags += b2Draw::e_jointBit;
     m_debugDraw->SetFlags(flags);
    //定義邊框
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);
    b2Body* groundBody = world->CreateBody(&groundBodyDef);
    //定義ground box shape
    b2EdgeShape groundBox;
    //bottom
    groundBox.Set(b2Vec2(0,0),
        b2Vec2(_screenSize.width/PTM_RATIO,0/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);
    //top
    groundBox.Set(b2Vec2(0,_screenSize.height/PTM_RATIO),
        b2Vec2(_screenSize.width/PTM_RATIO,_screenSize.height/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);
    //left
    groundBox.Set(b2Vec2(0,_screenSize.height/PTM_RATIO),
        b2Vec2(0,0));
    groundBody->CreateFixture(&groundBox,0);
    //right
    groundBox.Set(b2Vec2(_screenSize.width/PTM_RATIO,_screenSize.height/PTM_RATIO),
        b2Vec2(_screenSize.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);
}
void HelloWorld::draw(){
    CCLayer::draw();
         
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     //進(jìn)行繪制
     world->DrawDebugData();
    // restore default GL states
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);   
}
void HelloWorld::update(float dt){
    world->Step(dt,8,1);
    //同步精靈的物理動(dòng)作
    for(b2Body* b = world->GetBodyList();b;b=b->GetNext()){
        if(b->GetUserData()!=NULL){
            CCSprite* sprite = (CCSprite*)b->GetUserData();
            sprite->setPosition(ccp(b->GetPosition().x * PTM_RATIO,b->GetPosition().y*PTM_RATIO));
            sprite->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle()));
        }
    }
}
CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
            
    // 'layer' is an autorelease object
    HelloWorld *layer = new HelloWorld();
    // add layer as a child to scene
    scene->addChild(layer);
    layer->release();
    // return the scene
    return scene;
}
/************************************************************************/
/* 觸摸方法                                                                     */
/************************************************************************/
void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){
    CCSetIterator it;
    CCTouch* touch;
    for(it = pTouches->begin();it!=pTouches->end();it++){
        touch = (CCTouch*)(*it);
        if(!touch){
            break;
        }
        CCPoint location = touch->getLocation();
        //觸摸后添加精靈
        addNewSpriteAtPosition(location);
    }
}
/************************************************************************/
/* 添加精靈                                                                     */
/************************************************************************/
void HelloWorld::addNewSpriteAtPosition(CCPoint p){
    CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
    CCNode* parent = this->getChildByTag(kTagParentNode);
    int idx=(CCRANDOM_0_1()>0.5f?0:1);
    int idy=(CCRANDOM_0_1()>0.5f?0:1);
    //隨機(jī)創(chuàng)建精靈
    CCSprite* sprite = CCSprite::createWithTexture(m_pSpriteTexture,CCRectMake(32*idx,32*idy,32,32));
    sprite->setPosition(p);
    parent->addChild(sprite);
    //定義動(dòng)態(tài)剛體
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(p.x/PTM_RATIO,p.y/PTM_RATIO);
    bodyDef.userData = sprite;
    //創(chuàng)建剛體
    b2Body* body = world->CreateBody(&bodyDef);
    //創(chuàng)建長方形
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(sprite->getContentSize().width/PTM_RATIO/2,sprite->getContentSize().width/PTM_RATIO/2);
    //定義夾角
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 1.0f; //密度
    fixtureDef.friction = 0.3f;//摩擦力
    body->CreateFixture(&fixtureDef);
         
}

素材來源與cocos2d-x自帶demo

cocos2d-x之box2d使用筆記

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI