I have set maskbits and categorybits of my body. But why collision is still detected between object that is not supposed to be detected?
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);
My categories details:
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 and body2 are not supposed colliding each other, but I failed to acheive it by setting maskbits and categorybits. Anyone please help.
The CATEGORY values in Hex are wrong. Currently the values of your CATEGORYs are:
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
So MASK_MONSTER = CATEGORY_MONSTER | CATEGORY_SCENERY
= 110 in binary
And CATEGORY_MONSTER_MOUTH
= 1100100 in binary
Finally, MASK_MONSTER | CATEGORY_MONSTER_MOUTH
will match because it results in 100
Your problem is that your count up in Hex is wrong you must write:
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
After that comes 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000 etc.