我设置了我的身体的maskbits和categorybits。但是,为什么在不应该检测到的物体之间仍能检测到碰撞呢?
身体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);
身体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不应相互碰撞,但我无法通过设置maskbit位和categorybit来实现。任何人都可以帮忙。
答案 0 :(得分:2)
十六进制中的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等。