1. 程式人生 > >判斷兩個矩形是否相交的原理詳解

判斷兩個矩形是否相交的原理詳解

bool Rect::intersectsRect(const Rect& rect) const

{

    return !(     getMaxX() < rect.getMinX() ||

             rect.getMaxX() <      getMinX() ||

                  getMaxY() < rect.getMinY() ||

             rect.getMaxY() <      getMinY());

}

上面這幾行程式碼很簡單就是判斷相交的反面,就是不想交的情況、

前兩行如果其中一個矩形的最左端的x座標比另外一個矩形的最右端還要大那麼這兩個矩形一定不想交

後面兩行就是判斷,其中的一個矩形的最下端y的座標如果大於了另外一個矩形的最上端那麼這兩個矩形也一定是不想交的。

當然如果要是從正面來寫的話就會很費力,下面是賽車當中的一段程式碼,大家可以看看寫的比較麻煩,但是功能上都是實現了兩個矩形碰撞的判斷

 //主車在敵車的右側並主車的在敵車的上側有相同的部分說明是相撞了,直接讓主角死亡。(右上)

//if(   mainRole->getPosition().x > roles[j]->getPosition().x

//&&mainRole->getPosition().x<roles[j]->getPosition().x+roles[j]->getContentSize().width

//&&mainRole->getPosition().y>roles[j]->getPosition().y

//&&mainRole->getPosition().y<roles[j]->getPosition().y+roles[j]->getContentSize().height

//){

//gameState=CCLabelTTF::create("Game Over","Arial",96);

//gameState->setPosition(Point(visibleSize.width/2,visibleSize.height/2-200));this->addChild(gameState);

//isPlaying=false;

//pStart->setVisible(true);

//CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("boom.mp3");

//marks=0;

//}

//        

//        //主角的位置在敵車的左側且位於敵車的上側(左上)

//        else if(mainRole->getPosition().x+mainRole->getContentSize().width>roles[j]->getPosition().x

//&&mainRole->getPosition().x+mainRole->getContentSize().width<roles[j]->getPosition().x+roles[j]->getContentSize().width

//&&mainRole->getPosition().y>roles[j]->getPosition().y

//&&mainRole->getPosition().y<roles[j]->getPosition().y+roles[j]->getContentSize().height

//){;

//gameState=CCLabelTTF::create("Game Over","Arial",96);

//gameState->setPosition(Point(visibleSize.width/2,visibleSize.height/2-200));this->addChild(gameState);

//isPlaying=false;

//pStart->setVisible(true);

//CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("boom.mp3");

//marks=0;

//}

//        //(主車在左下)

//        else if(mainRole->getPosition().x+mainRole->getContentSize().width>roles[j]->getPosition().x

//&&mainRole->getPosition().x+mainRole->getContentSize().width<roles[j]->getPosition().x+roles[j]->getContentSize().width

//&&mainRole->getPosition().y+mainRole->getContentSize().height>roles[j]->getPosition().y

//&&mainRole->getPosition().y+mainRole->getContentSize().height<roles[j]->getPosition().y+roles[j]->getContentSize().height

//){

//gameState=CCLabelTTF::create("Game Over","Arial",96);

//gameState->setPosition(Point(visibleSize.width/2,visibleSize.height/2-200));this->addChild(gameState);

//isPlaying=false;

//pStart->setVisible(true);

//marks=0;

//CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("boom.mp3");

//}

//        //(主車在敵車的右下)

//        else if(mainRole->getPosition().x > roles[j]->getPosition().x

//&&mainRole->getPosition().x < roles[j]->getPosition().x+roles[j]->getContentSize().width

//&&mainRole->getPosition().y+mainRole->getContentSize().height>roles[j]->getPosition().y

//&&mainRole->getPosition().y+mainRole->getContentSize().height<roles[j]->getPosition().y+roles[j]->getContentSize().height

//){

//gameState=CCLabelTTF::create("Game Over","Arial",96);

//gameState->setPosition(Point(visibleSize.width/2,visibleSize.height/2-200));

//this->addChild(gameState);

//isPlaying=false;

//pStart->setVisible(true);

//CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("boom.mp3");

//marks=0;

//}