转自http://bbs.lanou3g.com/forum.php?mod=viewthread&tid=4854&extra=page%3D1

我们可以在每加飞机的机身上加两个矩形,当然这两个矩形是不可见的,把与每个飞机所绑定的矩形放到不同的数组里面,数组的元素类型是矩形,然后再遍利两个数组,看一个数组里面的矩形是否和另外一个数组里面的矩形发生了碰撞,原理就是这样,内部实现用的还是引擎提供的方法,具体的看代码,大家如果用得到可以直接用了


wKioL1YUuiayFHLGAADdwf7iX9s553.jpg


     //定义两个矩形数组,把矩形加到数组里面;
     Rect rectOneArray[OneArrayCount];
     Rect rectSecondAaary[TwoArrayCount];
     //要进行碰撞检测的两个精灵
     Sprite*sp1;
     Sprite*sp2;
     //为精灵加上矩形
     void setRect1(Point pos);
     void setRect2(Point pos);

     //判断矩形数组里面的矩形是否有碰撞
     bool isCollusion(Rect array1[], int array1len,Rect array2[], int array2len);
     //添加标签

----------------------------------------------------------------------

bool PolygonCollusion::isCollusion(Rect array1[], int array1len,Rect array2[], int array2len)
{
     bool flag= false ;
    //双层循环,遍利两个数组
     for ( int i =0; i<array1len; i++) {
         Rect rect1=array1[i];
         for ( int j =0; j<array2len; j++) {[/i]
[i]          //判断两个矩形是否相交
             if (rect1.intersectsRect(array2[j])) {
                 
                 flag= true ;
             }
         }
     }
     return flag;
}

void PolygonCollusion::setRect1(Point pos)
{
     //主角人物两个矩形的设定
     //第一个矩形的起点(即矩形的左上角)
     float orignalx1=pos.x-20;
     float orignaly1=pos.y+40;
     //第二个矩形的起点
     float orignalx2=pos.x-40;
     float orignaly2=pos.y+9;
     //把两个矩形加到数组里面
     rectOneArray[0].setRect(orignalx1, orignaly1, 25, 90);
     rectOneArray[1].setRect(orignalx2, orignaly2, 82, 32);
}
void PolygonCollusion::setRect2(Point pos)
{
     //钻石两个矩形的设定
     //第一个矩形的起点(即矩形的左上角)
     float orignalx1=pos.x-19;
     float orignaly1=pos.y+50;
     //第二个矩形的起点
     float orignalx2=pos.x-45;
     float orignaly2=pos.y+20;
     //把两个矩形加到数组里面
     rectSecondAaary[0].setRect(orignalx1, orignaly1, 26, 89);
     rectSecondAaary[1].setRect(orignalx2, orignaly2, 100, 40);
     
}