LIBGDX BOX2D filter

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.
只要两个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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值