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

溫馨提示×

溫馨提示×

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

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

[cocos2d-x]box2d的簡單應用

發布時間:2020-07-04 01:04:16 來源:網絡 閱讀:478 作者:蓬萊仙羽 欄目:游戲開發

box2d創建簡單的入門示例:

實現功能:

1.實現剛體的創建

2.能夠創建自定義的精靈剛體

3.精靈之間的相互碰撞

工具的使用:

要創建一個 自定義的剛體精靈(不規則形狀),就要用到一個工具PhysicalEditor,下載地址:http://yun.baidu.com/share/link?shareid=1782518146&uk=1209145999&third=0
我們將圖片添加到工程中,但要確保圖片精靈周圍是透明的才行,然后選擇Exporter->Box2D generic(PLIST),最后直接publish就可以了,然后將plist文件和原圖片資源一起導入到工程的Resource下。

操作步驟:

1.導入外部的GLES-Render類

2.主場景類:

class QueryCallback : public b2QueryCallback { public:     b2Vec2 m_point;     b2Fixture * m_fixture;          QueryCallback(const b2Vec2 & point)     {         m_point = point;         m_fixture = NULL;     }     bool ReportFixture(b2Fixture * fixture)     {         b2Body * body = fixture->GetBody();         if (body->GetType() == b2_dynamicBody)         {             bool inside = fixture->TestPoint(m_point);             if(inside)             {                 m_fixture = fixture;                 return false;             }         }         return true;     }      };  class SecondScene : public CCLayer { public:     ~SecondScene();     SecondScene();      static CCScene* scene();          void initPhysics();     void addNewSpriteAtPosition(CCPoint p);          virtual void draw();     void update(float dt);          void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);     void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);     void ccTouchesEnded(CCSet* touches, CCEvent* event);          bool MouseDown(const b2Vec2 & p);     void MouseMove(const b2Vec2 & p);     void MouseUp();      private:     GLESDebugDraw * m_debugDraw;     b2World * world;     CCTexture2D * m_pSpriteTexture;     b2MouseJoint * m_mouseJoint;  //鼠標關節節點     b2Body * m_groundBody; }; #endif /* defined(__box2dDemo__SecondHelloScene__) */


cpp

#include "SecondHelloScene.h" #include "SimpleAudioEngine.h" #include "GLES-Render.h" #include "GB2ShapeCache-x.h" #include "ContactListener.h" //碰撞檢測 using namespace cocos2d; using namespace CocosDenshion;  #define PTM_RATIO 32  enum {     kTagParentNode = 1, };  //PhysicsSprite::PhysicsSprite() //: m_pBody(NULL) //{ //     //} // //void PhysicsSprite::setPhysicsBody(b2Body * body) //{ //    m_pBody = body; //} // //bool PhysicsSprite::isDirty(void) //{ //    return true; //} // //// returns the transform matrix according the Chipmunk Body values //CCAffineTransform PhysicsSprite::nodeToParentTransform(void) //{ //    b2Vec2 pos  = m_pBody->GetPosition(); //     //    float x = pos.x * PTM_RATIO; //    float y = pos.y * PTM_RATIO; //     //    if ( isIgnoreAnchorPointForPosition() ) { //        x += m_obAnchorPointInPoints.x; //        y += m_obAnchorPointInPoints.y; //    } //     //    // Make matrix //    float radians = m_pBody->GetAngle(); //    float c = cosf(radians); //    float s = sinf(radians); //     //    if( ! m_obAnchorPointInPoints.equals(CCPointZero) ){ //        x += c*-m_obAnchorPointInPoints.x + -s*-m_obAnchorPointInPoints.y; //        y += s*-m_obAnchorPointInPoints.x + c*-m_obAnchorPointInPoints.y; //    } //     //    // Rot, Translate Matrix //    m_sTransform = CCAffineTransformMake( c,  s, //                                         -s,    c, //                                         x,    y ); //     //    return m_sTransform; //}  SecondScene::SecondScene() {     setTouchEnabled( true );     setAccelerometerEnabled( true );          CCSize s = CCDirector::sharedDirector()->getWinSize();     this->initPhysics();          addNewSpriteAtPosition(ccp(s.width / 2, s.height / 2));      //    CCSpriteBatchNode *parent = CCSpriteBatchNode::create("blocks.png", 100); //    m_pSpriteTexture = parent->getTexture(); //     //    addChild(parent, 0, kTagParentNode); //     //     //    addNewSpriteAtPosition(ccp(s.width/2, s.height/2)); //     //    CCLabelTTF *label = CCLabelTTF::create("Tap screen", "Marker Felt", 32); //    addChild(label, 0); //    label->setColor(ccc3(0,0,255)); //    label->setPosition(ccp( s.width/2, s.height-50));          scheduleUpdate(); }  SecondScene::~SecondScene() {     delete world;     world = NULL;          //delete m_debugDraw; }  void SecondScene::initPhysics() {     //添加自定義的精靈     GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile("zhizhu.plist");          CCSize s = CCDirector::sharedDirector()->getWinSize();          b2Vec2 gravity;     gravity.Set(0.0f, -10.0f);     world = new b2World(gravity);          b2BodyDef bodydef;     m_groundBody = world->CreateBody(&bodydef);          world->SetAllowSleeping(true);          world->SetContinuousPhysics(false);//隧道效應,穿透,true表示不能穿透               //接觸監聽,碰撞檢測     b2ContactListener *contactListener = new ContactListener();     world->SetContactListener(contactListener);          //靜態剛體     m_debugDraw = new GLESDebugDraw( PTM_RATIO );     world->SetDebugDraw(m_debugDraw);          uint32 flags = 0;     flags += b2Draw::e_shapeBit;     //        flags += b2Draw::e_jointBit;     //        flags += b2Draw::e_aabbBit;     //        flags += b2Draw::e_pairBit;     //        flags += b2Draw::e_centerOfMassBit;     m_debugDraw->SetFlags(flags);               // Define the ground body.     b2BodyDef groundBodyDef;     groundBodyDef.position.Set(5, 1);           b2Body* groundBody = world->CreateBody(&groundBodyDef);          // Define the ground box shape.     b2EdgeShape groundBox;          // bottom          groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));     groundBody->CreateFixture(&groundBox,0);          // top     groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));     groundBody->CreateFixture(&groundBox,0);          // left     groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));     groundBody->CreateFixture(&groundBox,0);          // right     groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));     groundBody->CreateFixture(&groundBox,0); }  void SecondScene::draw() {     CCLayer::draw();          ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );          kmGLPushMatrix();          world->DrawDebugData();          kmGLPopMatrix(); }  void SecondScene::addNewSpriteAtPosition(CCPoint p) {               string name = "alien";     CCSprite * sprite = CCSprite::create((name + ".png").c_str());     sprite->setPosition(p);     this->addChild(sprite);      //    CCSprite * sp = CCSprite::create("Icon.png"); //    sp->setPosition(CCPointMake(100, 100)); //    this->addChild(sp);          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);               // Define another box shape for our dynamic body.     //通過vertexHelper工具生成多邊形     b2PolygonShape dynamicBox; //    b2Vec2 verts[] = { //        b2Vec2(-12.8f / PTM_RATIO, 50.9f / PTM_RATIO), //        b2Vec2(-48.8f / PTM_RATIO, 2.3f / PTM_RATIO), //        b2Vec2(-30.7f / PTM_RATIO, -37.9f / PTM_RATIO), //        b2Vec2(28.8f / PTM_RATIO, -18.9f / PTM_RATIO), //        b2Vec2(30.8f / PTM_RATIO, 16.9f / PTM_RATIO), //        b2Vec2(20.8f / PTM_RATIO, 44.9f / PTM_RATIO) //    }; //    dynamicBox.Set(verts, 6);     //dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box           //    b2FixtureDef fixtureDef; //    fixtureDef.shape = &dynamicBox; //    fixtureDef.density = 1.0f;//密度 //    fixtureDef.friction = 0.3f;//摩擦 //    fixtureDef.restitution = 0.5f;//恢復,也可看成彈性系數 //     //     //    body->CreateFixture(&fixtureDef);          GB2ShapeCache * sc = GB2ShapeCache::sharedGB2ShapeCache();     sc->addFixturesToBody(body, name);     sprite->setAnchorPoint(sc->anchorPointForShape(name)); }   void SecondScene::update(float dt) {     int velocityIterations = 8;     int positionIterations = 1;      world->Step(dt, velocityIterations, positionIterations);      for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())     {         if (b->GetUserData() != NULL) {             //Synchronize the AtlasSprites position and rotation with the corresponding body             CCSprite* myActor = (CCSprite*)b->GetUserData();             myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );             myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );//CC_RADIANS_TO_DEGREES弧度轉角度         }     } }  CCScene* SecondScene::scene() {     // 'scene' is an autorelease object     CCScene *scene = CCScene::create();          // add layer as a child to scene     CCLayer* layer = new SecondScene();     scene->addChild(layer);     layer->release();          return scene; }  void SecondScene::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) {     CCSetIterator it;     CCTouch * touch;     for(it = pTouches->begin(); it != pTouches->end(); it++)     {         touch = (CCTouch *)(*it);         if(!touch)             break;         CCPoint location = touch->getLocation();                  if(!this->MouseDown(b2Vec2(location.x / PTM_RATIO, location.y / PTM_RATIO)))         {             this->addNewSpriteAtPosition(location);         }     } }  void SecondScene::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) {     CCSetIterator it;     CCTouch * touch;     for(it = pTouches->begin(); it != pTouches->end(); it++)     {         touch = (CCTouch *)(*it);         if(!touch)             break;         CCPoint location = touch->getLocation();                  this->MouseMove(b2Vec2(location.x / PTM_RATIO,location.y / PTM_RATIO));     } }  void SecondScene::ccTouchesEnded(CCSet* touches, CCEvent* event) {     CCSetIterator it;     CCTouch* touch;          for( it = touches->begin(); it != touches->end(); it++)     {         touch = (CCTouch*)(*it);              if(!touch)             break;              CCPoint location = touch->getLocationInView();              location = CCDirector::sharedDirector()->convertToGL(location);              addNewSpriteAtPosition( location );     }     this->MouseUp(); }  bool SecondScene::MouseDown(const b2Vec2 &p) {     if(m_mouseJoint != NULL)     {         return false;     }          b2AABB aabb;     b2Vec2 d;     d.Set(0.001f, 0.001f);     aabb.lowerBound = p - d;     aabb.upperBound = p + d;          QueryCallback callback(p);          world->QueryAABB(&callback, aabb);          if (callback.m_fixture)     {         b2Body * body = callback.m_fixture->GetBody();         b2MouseJointDef md;         md.bodyA = m_groundBody;  //m_groundBody需要在上面init方法里定義一下         md.bodyB = body;         md.target = p;         md.maxForce = 1000.0f * body->GetMass();         m_mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);         body->SetAwake(true);         return true;     }          return true; }  void SecondScene::MouseMove(const b2Vec2 &p) {     if(m_mouseJoint)     {         m_mouseJoint->SetTarget(p);     } }  void SecondScene::MouseUp() {     if(m_mouseJoint)     {         world->DestroyJoint(m_mouseJoint);         m_mouseJoint = NULL;     } }


3.碰撞檢測類(ContactListener)

#ifndef __box2dDemo__ContactListener__ #define __box2dDemo__ContactListener__  //碰撞檢測 #include <iostream>  #include "cocos2d.h" #include "Box2D.h"  using namespace cocos2d;  class ContactListener : public b2ContactListener {     void BeginContact(b2Contact * contact);     void EndContact(b2Contact *contact); };  #endif /* defined(__box2dDemo__ContactListener__) */

cpp

#include "ContactListener.h"  void ContactListener::BeginContact(b2Contact *contact) {     b2Body *bodyA = contact->GetFixtureA()->GetBody();     b2Body *bodyB = contact->GetFixtureB()->GetBody();     CCSprite *spriteA = (CCSprite *)bodyA->GetUserData();     CCSprite *spriteB = (CCSprite *)bodyB->GetUserData();          if (spriteA != NULL && spriteB != NULL) {         spriteA->setColor(ccMAGENTA);         spriteB->setColor(ccMAGENTA);     } }  void ContactListener::EndContact(b2Contact *contact) {     b2Body *bodyA = contact->GetFixtureA()->GetBody();     b2Body *bodyB = contact->GetFixtureB()->GetBody();     CCSprite *spriteA = (CCSprite *)bodyA->GetUserData();     CCSprite *spriteB = (CCSprite *)bodyB->GetUserData();          if (spriteA != NULL && spriteB != NULL) {         spriteA->setColor(ccWHITE);         spriteB->setColor(ccWHITE);     } }


運行效果:

[cocos2d-x]box2d的簡單應用

[cocos2d-x]box2d的簡單應用


cocos2d-x游戲開發QQ交流群:280818155

備注:加入者必須修改:地區-昵稱,很少有動態的將定期清理


向AI問一下細節

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

AI

彭山县| 赤水市| 黑龙江省| 阳春市| 揭西县| 肇州县| 称多县| 卫辉市| 基隆市| 灌云县| 仲巴县| 延边| 法库县| 灵山县| 赫章县| 鄂伦春自治旗| 九龙坡区| 哈巴河县| 普兰店市| 南雄市| 璧山县| 吴忠市| 高阳县| 府谷县| 泸溪县| 来安县| 西贡区| 洛阳市| 乐亭县| 民乐县| 临清市| 无极县| 桂阳县| 垣曲县| 岳阳市| 高阳县| 萍乡市| 黔江区| 聂拉木县| 锡林郭勒盟| 冕宁县|