ContactListener
work with ContactListener
. Here are just examples that I used, were not quite correctly selected. Box2D has much more convenient collision filtering tools, namely filters. I will write about them this time.ContactListener
, its methods for such objects will not work. Face optimization. And now let's take a closer look. // 0000000000000001 in binary final public static short CATEGORY_PLAYER = 0x0001; // 0000000000000010 in binary final public static short CATEGORY_BALOOM = 0x0002; // 0000000000000100 in binary final public static short CATEGORY_RUNNER = 0x0004; // 0000000000001000 in binary final public static short CATEGORY_SCENERY = 0x0008;
// f.categoryBits = MyWorld.CATEGORY_PLAYER; // f.categoryBits = MyWorld.CATEGORY_SCENERY; // Baloom' f.categoryBits = MyWorld.CATEGORY_BALOOM; // Runner' f.categoryBits = MyWorld.CATEGORY_RUNNER;
final public static short MASK_PLAYER = CATEGORY_RUNNER | CATEGORY_SCENERY; // ~MASK_PLAYER final public static short MASK_BALOOM = CATEGORY_SCENERY ; final public static short MASK_RUNNER = CATEGORY_PLAYER | CATEGORY_SCENERY ; final public static short MASK_SCENERY = -1;
// f.maskBits = MyWorld.MASK_PLAYER; // f.maskBits = MyWorld.MASK_SCENERY; // Baloom' f.maskBits = MyWorld.MASK_BALOOM; // Runner' f.maskBits = MyWorld.MASK_RUNNER;
Filter f = new Filter(); f.categoryBits = MyWorld.CATEGORY_PLAYER; f.maskBits = MyWorld.MASK_PLAYER; playerSensorFixture.setFilterData(f); playerPhysicsFixture.setFilterData(f);
BodyDef def2 = new BodyDef(); def.type = BodyType.DynamicBody; Body boxP2 = world.createBody(def2); player2 = new Player(boxP2); player2.getBody().setTransform(5.0f, 1.0f, 0); player2.getBody().setFixedRotation(true);
Player
class, set the group. Filter f = new Filter(); f.groupIndex = -1; playerSensorFixture.setFilterData(f); playerPhysicsFixture.setFilterData(f);
Source: https://habr.com/ru/post/163885/
All Articles