物理引擎中不規則物體的碰撞檢測

來源:互聯網
上載者:User

標籤:碰撞檢測   不規則物體的碰撞檢測   cocos2d-x中不規則物體的碰撞檢測   

在運用物理引擎的時候,經常回出現這種情況,不規則物體的碰撞檢測無法更加精確的測量,那麼,小傑今天就帶您一起解決這個問題!!!

首先下載一個windows下面的軟體 名稱是:PhysicsEditor ,這個軟體可以協助你設計不規則的圖形。軟體的使用方法在這裡不具體講解了,說一下注意的問題,添加進精靈後 :按照這個步驟走


好了,這時候 做完了就會產生 plist檔案,把它和png圖片一起拷進資來源目錄下面:


定義一個成員方法:

std::vector<std::vector<Vec2>> getShapeFromPlistFile(std::string fileName);

實現:

std::vector<std::vector<Vec2>> Chipmunck::getShapeFromPlistFile(std::string fileName){std::vector<std::vector<Vec2>> vectors;//從檔案中讀取plist檔案,將內容讀取到 vec中ValueVector vec = FileUtils::getInstance()->getValueVectorFromFile(fileName);for (int i =0;i<vec.size();i++){//遍曆vec,得到所有的形狀ValueVector shape = vec.at(i).asValueVector();std::vector<Vec2> points;for (int j=0;j<shape.size();j++){//遍曆形狀,拿到所有的點Vec2 point = PointFromString(shape.at(j).asString());points.push_back(point);//所有的點放在集合中}vectors.push_back(points);//所有的 形狀 放在集合中}return vectors;}

調用它:

void Chipmunck::addSprites(){    auto circleSp = Sprite::create("c.png");    circleSp->setPosition(visSize.width/2,visSize.height/2);    this->addChild(circleSp);    auto body = PhysicsBody::createCircle(circleSp->getContentSize().width/2);    body->setMass(10000);    body->setVelocity(Vec2(0,-1000));    circleSp->setPhysicsBody(body);    auto circleSp2 = Sprite::create("c.png");    circleSp2->setPosition(visSize.width/2,20);    this->addChild(circleSp2);    auto body2 = PhysicsBody::createCircle(circleSp->getContentSize().width/2);    body2->setMass(10000);    body2->setDynamic(false);    circleSp2->setPhysicsBody(body2);    auto paoku = Sprite::create("paoku 00188.png");    paoku->setPosition(Vec2(300,200));    this->addChild(paoku);    //通過傳過去檔案名稱字 擷取 返回來的 所有 圖形的集合(每個圖形中存放著很多組成這個圖形的所有的點)    std::vector<std::vector<Vec2>> shapes = this->getShapeFromPlistFile("test.plist");    //定義一個剛體    auto ployon = PhysicsBody::create();    for (auto shape:shapes){//遍曆圖形集合中的每個 圖形        //這裡的shape.data是一個指向 第一個元素的指標          auto points = PhysicsShapePolygon::create(shape.data(),(int)shape.size());//根據這個圖形建立剛體        //points是一個點的數組        ployon->addShape(points);//把所有的 放在 剛體中    }    paoku->setPhysicsBody(ployon);    }  

好了,這樣基本就解決了!!,運行




物理引擎中不規則物體的碰撞檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.