Help with collision filtering (maskBits)

I need help trying to understand how use maskBits for collision filtering.

I want to have lots of "zombies" in the playfield which collide with only one "ball" object (zombies should not collide with each other), I also have only one "sword" object that collides with both balls and zombies.

I don't know hot to use maskBits to allow the sword to collide with both Ball and Zombies and I tried lots of different things, please help.

Also how can activate/deactive the collision filtering for the swords and zombies only?. Thanks in advance.
Here's a thread with a similar question:
viewtopic.php?f=8&t=5607 (check the 2nd post)
Also, a tutorial (for the older version of Box2D, but filtering's the same):
http://www.emanueleferonato.com/2009/10 ... filtering/


Now, I haven't tried out filtering myself, but I think this should work for your second question:
Code:
// myFixture is your body fixture
var myFilter:b2FilterData = myFixture.GetFilterData();
// set these to whatever new values you want:
myFilter.groupIndex = 0;
myFilter.maskBits = 0xffff;
myFilter.categoryBits = 0x001;
// set the new filter:
myFixture.SetFilterData(myFilter);
Thanks a lot, using "2" instead of 0x0002 and using "|" to include more than 1 category in maskbits did the job, thanks for the help.

EDIT:

But I kept trying to change maskbits on real time, but the change seems to have no effect, this is the code of my contact listener.
I try to make that after the baseballbat and zombie collision, zombie can now collide with itself:

Code:
      override public function EndContact(contact:b2Contact):void
      {
         
         Object1 = contact.GetFixtureA().GetBody().GetUserData();
         Object2 = contact.GetFixtureB().GetBody().GetUserData();

         if (Object1 is BaseballBat || Object2 is BaseballBat)
         {
            var myFilter:b2FilterData;
            
            if (Object1 is zombie)
            {               

               myFilter = b2Fixture(zombie(Object1)._obj.GetFixtureList()).GetFilterData();
               trace("zombie on 2: " + myFilter.maskBits);
               myFilter.maskBits = 2|8;
               zombie(Object1)._obj.GetFixtureList().SetFilterData(myFilter)
            
               trace("zombie on 2: " + myFilter.maskBits);

               
            }else if (Object2 is zombie)
            {

               myFilter = b2Fixture(zombie(Object2)._obj.GetFixtureList()).GetFilterData();
               trace("zombie on 2: " + myFilter.maskBits);
               myFilter.maskBits = 2|8;
               zombie(Object2)._obj.GetFixtureList().SetFilterData(myFilter);

               trace("zombie on 2: " + myFilter.maskBits);   

            }   
            
            
         if (Object1 is zombie && Object2 is zombie)
         {
            

               trace("zombie & zombie Collisions!");

         }            
            
         }


The traces show change in maskbits, but there is no actual detection of the zombie&zombie collision.
I think you need to run the changes in the preSolve or postSolve events.
Thanks but the result is the same than before.
Any other ideas?.
I discovered that the maskbits apply the change only while they are inside the function, but once the collision is over it reverts back.

For example, when BaseballBat and 2 zombies collide at the same time, that's when maskbits aplly the change and a collision is detected.
That's odd. Is there no other place in your code where you set the maskbits?
In older box2d versions you had a Refilter() function, but I can't find it in the current version.
Anyway, I don't know what else you can do, 'cept for maybe looking for help in this forum section: viewforum.php?f=3

By the way, what's " 2|8 " ? Flash just traces that as 10, treating | as + . :S
Yes 8|2 is 10, I like to show things that way because it shows the change I want to make more clearly.

The only other place where I use maskbits is where I create the objects, nowhere else.

But thanks for the help.
I was thinking that maybe the collision is not detected because both objects should indicate that they can collide with each other, it does not work if only one of them can collide with the other, that's why the only way to make them collide is when both change their maskbits at the same time.
That makes sense. Did you give it a try? Did it work?

Original thread: https://box2d.org/forum/viewtopic.php?t=5644