Filter有3种属性
public short categoryBits = 0x0001; //Normally you would just set one bit 设置一位为1就好,so box2d的categoryBits总共有16种,short 16位
public short maskBits = -1;
public short groupIndex = 0;
categoryBits和maskBits配合使用,groupIndex和他们比具有更高的优先级
如果两个body的groupIndex是相同的负数代表不相碰,是相同的正数代表会相碰;zero代表这个body的groupIndex无意义,进而比较mask
如果这两个body的groupIndex不相同,比较mask
eg. 玩家之间不会碰撞,怪物之间不会碰撞,但是玩家和怪物会发生碰撞,通过掩码来实现。
playerFixtureDef.filter.categoryBits=0x0001 //001
monster1FixtureDef.filter.categoryBits=0x0002//010
monster2FixtureDef.filter.categoryBits=0x0004//100
playerFixtureDef.filter.maskBits=0x0006 //110
monster1FixtureDef.filter.maskBits=0x0001
monster2FixtureDef.filter.maskBits=0x0001
maskBits跟其他的categoryBits进行位与运算,!=0表示碰撞
运算的公式:
if((catA&maskB)!=0&&(catB&maskA)!=0){ //fixtures can collide}
注意:
1.static body上的fixture只会和dynamic body 相碰
2.kinematic body上的fixture只会和dynamic body 相碰
3.同一个body上的fixtures不会相碰
4.joint上的fixtures相碰与否可以设置collideConnected
ContactFilter 接触筛选interface
,扼杀在摇篮里
boolean shouldCollide (Fixture fixtureA, Fixture fixtureB);
world.setContactFilter
Contact
The class manages contact between two shapes. A contact exists for each overlapping AABB in the broad-phase (except if
filtered). Therefore a contact object may exist that has no contact points.
filtered). Therefore a contact object may exist that has no contact points.
只要两个shape重叠了就会产生contact,不一定需要有contact points
Sensor
A sensor shape collects contact information but never generates a collision response
//At the definition of the Fixture fixtureDef.isSensor = true;
Sensor不会产生contact points,但跟其他shape overlap了会有contact.检查sensor状态
Contact::isTouching
ContactListener::BeginContact and EndContact
ContactListener
接触监听器interface,
能监听哪两个fixture相碰,然后自定义动作
public void beginContact (Contact contact);
public void endContact (Contact contact);
public void preSolve (Contact contact, Manifold oldManifold);
public void postSolve (Contact contact, ContactImpulse impulse);
world.setContactListener