Body设置掩码位和类别后仍然检测碰撞

Body Still detect collision after setting maskbit and category

我已经设置了 body 的掩码位和类别位。但是为什么在 object 之间仍然检测到不应检测到的碰撞?

Body 1:

    BodyDef def = new BodyDef();
    FixtureDef fixtureDef = new FixtureDef();

    def.type = BodyDef.BodyType.DynamicBody;
    def.position.set( 5, 2);

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(getWidth() /GC.UNITS_PER_METER /7);

    fixtureDef.shape = circleShape;
    fixtureDef.density = 1f;
    fixtureDef.friction = 1;
    fixtureDef.filter.categoryBits = GC.CATEGORY_MONSTER_MOUTH;
    fixtureDef.filter.maskBits = GC.MASK_MONSTER_MOUTH;

    Body box = world.createBody(def);
    box.createFixture(fixtureDef);
    circleShape.dispose();

    sensorBody= box;
    sensorBody.setFixedRotation(true);
    sensorBody.setAwake(true);
    sensorBody.setUserData(this);

Body 2:

    BodyDef def = new BodyDef();
    FixtureDef fixtureDef = new FixtureDef();

    def.type = BodyDef.BodyType.DynamicBody;
    def.position.set(getX() - (getWidth()* getScaleX()) , getY() - (getHeight() * getScaleY()));

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(box2dWidth/2);

    fixtureDef.shape = circleShape;
    fixtureDef.density = 1f;
    fixtureDef.friction=0.5f;
    fixtureDef.filter.categoryBits = GC.CATEGORY_MONSTER;
    fixtureDef.filter.maskBits = GC.MASK_MONSTER;
    Body box = world.createBody(def);
    box.createFixture(fixtureDef);
    polygonShape.dispose();

    body = box;
    body.setAwake(true);
    body.setUserData(this);
    body.setAngularDamping(4);
    body.setFixedRotation(true);

我的类别详细信息:

public static final short CATEGORY_ARROW = 0x0001; // 0000000000000001 in binary
public static final short CATEGORY_MONSTER = 0x0002; // 0000000000000010 in binary
public static final short CATEGORY_SCENERY = 0x0004; // 0000000000000100 in binary
public static final short CATEGORY_GRAVEYARD = 0x0008; // 0000000000000100 in binary
public static final short CATEGORY_PENGUIN = 0X0016;
public static final short CATEGORY_FISH = 0x0032;
public static final short CATEGORY_MONSTER_MOUTH = 0x0064;

public static final short MASK_ARROW = CATEGORY_MONSTER | CATEGORY_SCENERY; 
public static final short MASK_MONSTER = CATEGORY_ARROW | CATEGORY_SCENERY ; 
public static final short MASK_PENGUIN = CATEGORY_SCENERY | CATEGORY_MONSTER; 
public static final short MASK_FISH = CATEGORY_MONSTER_MOUTH| CATEGORY_SCENERY; 
public static final short MASK_GRAVEYARD = CATEGORY_SCENERY;
public static final short MASK_MONSTER_MOUTH = CATEGORY_FISH | CATEGORY_SCENERY;

Body1 和 body2 不应该相互碰撞,但我未能通过设置 maskbits 和 categorybits 来实现它。任何人都请帮助。

十六进制的 CATEGORY 值是错误的。目前您的 CATEGORY 的值为:

public static final short CATEGORY_ARROW = 0x0001; // 1 in binary
public static final short CATEGORY_MONSTER = 0x0002; // 10 in binary
public static final short CATEGORY_SCENERY = 0x0004; // 100 in binary
public static final short CATEGORY_GRAVEYARD = 0x0008; // 1000 in binary
public static final short CATEGORY_PENGUIN = 0X0016; // 10110 in binary
public static final short CATEGORY_FISH = 0x0032; // 110010 in binary
public static final short CATEGORY_MONSTER_MOUTH = 0x0064; // 1100100 in binary

所以 MASK_MONSTER = CATEGORY_MONSTER | CATEGORY_SCENERY = 二进制 110
CATEGORY_MONSTER_MOUTH = 1100100 二进制

最后,MASK_MONSTER | CATEGORY_MONSTER_MOUTH 会匹配,因为它的结果是 100


你的问题是你的十六进制计数是错误的你必须写:

public static final short CATEGORY_ARROW = 0x0001; // 1 in binary
public static final short CATEGORY_MONSTER = 0x0002; // 10 in binary
public static final short CATEGORY_SCENERY = 0x0004; // 100 in binary
public static final short CATEGORY_GRAVEYARD = 0x0008; // 1000 in binary
public static final short CATEGORY_PENGUIN = 0X0010; // 10000 in binary
public static final short CATEGORY_FISH = 0x0020; // 100000 in binary
public static final short CATEGORY_MONSTER_MOUTH = 0x0040; // 1000000 in binary

之后是 0x80、0x100、0x200、0x400、0x800、0x1000 等