91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

cocos2d-x之box2d使用筆記

發布時間:2020-08-01 11:57:14 來源:網絡 閱讀:1331 作者:xuzw13 欄目:游戲開發

   下午學了一下box2d相關的內容,這里做個筆記。

1、創建頭文件

#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、編寫實現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);
    //在中間添加一個精靈
    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);
    //創建世界
    world = new b2World(gravity);
    //允許睡眠
    world->SetAllowSleeping(true);
    world->SetContinuousPhysics(true);
    //創建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);
     //進行繪制
     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);
    //同步精靈的物理動作
    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);
    //隨機創建精靈
    CCSprite* sprite = CCSprite::createWithTexture(m_pSpriteTexture,CCRectMake(32*idx,32*idy,32,32));
    sprite->setPosition(p);
    parent->addChild(sprite);
    //定義動態剛體
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(p.x/PTM_RATIO,p.y/PTM_RATIO);
    bodyDef.userData = sprite;
    //創建剛體
    b2Body* body = world->CreateBody(&bodyDef);
    //創建長方形
    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問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

高邮市| 延吉市| 克什克腾旗| 黎川县| 安阳市| 宜兴市| 如皋市| 阜城县| 册亨县| 德州市| 班玛县| 郯城县| 师宗县| 葫芦岛市| 德钦县| 新泰市| 大埔县| 香河县| 安陆市| 古蔺县| 囊谦县| 杂多县| 固原市| 武功县| 和田市| 巫山县| 姚安县| 琼海市| 青州市| 嘉定区| 出国| 军事| 百色市| 东明县| 朔州市| 泸水县| 沾化县| 曲松县| 沂南县| 油尖旺区| 文登市|