kingBook

导航

Box2d FilterData

Box2D.Dynamics.b2ContactFilter类,碰撞源码:

public virtual function ShouldCollide(fixtureA:b2Fixture, fixtureB:b2Fixture) : Boolean{
    var filter1:b2FilterData = fixtureA.GetFilterData();
    var filter2:b2FilterData = fixtureB.GetFilterData();
    
    if (filter1.groupIndex == filter2.groupIndex && filter1.groupIndex != 0)
    {
        return filter1.groupIndex > 0;
    }
    
    var collide:Boolean = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
    return collide;
}

 演示:

var filter1={categoryBits:0x0001,maskBits:0xFFFF};
var filter2={categoryBits:0x0001,maskBits:0xFFFF};
var filter3={categoryBits:0x0001,maskBits:0xFFFF};
var filter4={categoryBits:0x0001,maskBits:0xFFFF};
var list:Array=[filter1,filter2,filter3,filter4];

filter1.categoryBits=1<<1;//2
filter2.categoryBits=1<<2;//4
filter3.categoryBits=1<<3;//8
filter4.categoryBits=1<<4;//16

filter1.maskBits=0|4|16;//只和4,16碰撞
//filter1.maskBits=~(4|16);//不和4,16碰撞
for(var i:int=0;i<1;i++){
    for(var j:int=0;j<list.length;j++){
        getCollide(list[i],list[j]);
    }
}

function getCollide(filter1:*,filter2:*):Boolean{
    var collide:Boolean = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
    trace("filter1.categoryBits:"+filter1.categoryBits, "filter1.maskBits:"+filter1.maskBits);
    trace("filter2.categoryBits:"+filter2.categoryBits, "filter2.maskBits:"+filter2.maskBits);
    trace("collide:"+collide);
    trace("______________________________________________________");
    return collide;
}

/*output:
filter1.categoryBits:2 filter1.maskBits:20
filter2.categoryBits:2 filter2.maskBits:20
collide:false
______________________________________________________
filter1.categoryBits:2 filter1.maskBits:20
filter2.categoryBits:4 filter2.maskBits:65535
collide:true
______________________________________________________
filter1.categoryBits:2 filter1.maskBits:20
filter2.categoryBits:8 filter2.maskBits:65535
collide:false
______________________________________________________
filter1.categoryBits:2 filter1.maskBits:20
filter2.categoryBits:16 filter2.maskBits:65535
collide:true
______________________________________________________
*//

 

posted on 2015-12-29 09:55  kingBook  阅读(398)  评论(0编辑  收藏  举报