Filtering issue w/ dynamic bodies

Okay... Very basic question. New here. I come from a Chipmunk background.

Now, I have created a filter maskbit and category for may main character. I am also creating a body dynamically (on touch end) with maskbit and category filtering properties. I have set up my contact listener class and have instantiated that, and set the world to listen for it.

My issue is that the dynamically created body's filerting methods are not being captured by the world. A Google Search revealed to me that in the Flash version of Box2D, you have to perform the m_world->Refilter() method. There is no such method in the iPhone version, however.

Can someone help me out with this issue?

Thanks,

Code for my contact listener class is below:

Code: Select all

const uint16 ENEMY_BIT = 0x0003;
const uint16 CHARACTER_BIT = 0x0004;

class MyContactListener : public b2ContactListener {
public:
    void BeginContact(b2Contact* contact)
    {
        //check if fixture A was a ball
        b2Filter filterA = contact->GetFixtureA()->GetFilterData();
        b2Filter filterB = contact->GetFixtureB()->GetFilterData();
       
        bool collide = (filterA.maskBits & filterB.categoryBits) != 0 &&
        (filterA.categoryBits & filterB.maskBits) != 0;
       
        if (filterA.categoryBits == ENEMY_BIT)
            NSLog(@"This is a test");
    }
    // ...
}
};
It's not really clear what you mean by "the body's filtering methods are not being captured". Perhaps you could take a small step back and tell us what the overall goal is... it could be you just have the flags incorrect, it can be a headache sometimes.

In any case while I'm here I will mention a few things.
The first two lines probably will not compile, I guess this was just added accidentally when you wrote the post.
0x0003 is actually two bits 🙂
The "bool collide = ..." part looks like the check Box2D makes to decide if two fixtures should ever contact. It's only if this is true that you will ever get a BeginContact event for them, so I would expect this to be true every time here.
Hi,
I am trying to make the two bodies collide. The first body is created in the Cocos2d init() method, and the second is added to the Box2d world dynamically. However, the dynamically created body's mask bits / category bits aren't being "picked up" (for lack of better term) by the Box2d world, and so the collision between those two maskbits never happens. Hope that clears up any confusion.

Thanks,
What are the bits?
Hi there,
Those were the bits I posted in my code above; am I defining them incorrectly? New to this so that could be the case.
Thanks again,
-G.

EDIT: Ahh, I've figured it out! There was an error when defining the shape :S

Code: Select all

    for (int m = 0; m < numSegments; m++) {
        forceFieldShape.SetAsEdge(b2Vec2(SCREEN_TO_WORLD(pointsArrayX[m]), SCREEN_TO_WORLD(pointsArrayY[m])), b2Vec2(SCREEN_TO_WORLD(pointsArrayX[m + 1]), SCREEN_TO_WORLD(pointsArrayY[m + 1])));
        b2FixtureDef forceFieldFixture;
        forceFieldFixture.shape = &forceFieldShape;
        forceFieldFixture.filter.categoryBits = FORCE_FIELD_BIT;
        forceFieldFixture.filter.maskBits = ENEMY_BIT;
        forceFieldBody->CreateFixture(&forceFieldFixture);
// WAS THIS:         forceFieldBody->CreateFixture(&forceFieldShape, 0);
    }

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