SetFilterData fall in infinite loop

Hello everybody,

I have a little dynamic square which is bouncing on every side of my screen and on a big static centered box. I copy/paste TestBed mousejoint code's to allow me dragging the little moving square. Everything goes well, but now I want the little square don't collide the centered box when it is dragged, thus I will put it inside the centered box. So to do that i just set different filter category and mask at creation, then when the mousejoint is created I modify the little square mask to don't collide the box, and finally I reset the mask when dragging stops. Here is the code :

// Constants
Code:
const uint16 kBoxCategory = 0x0004;
const uint16 kSquareCategory = 0x0008;

const uint16 kBoxMask = 0xFFFF;
const uint16 kSquareMask = 0xFFFF ^ kSquareCategory; // Squares don't collide with other squares



// Centered box creation
Code:
{
   b2BodyDef bodyDef;
   bodyDef.position.Set(200.0f / PTM_RATIO, 100.0f / PTM_RATIO);
   b2Body *body = world->CreateBody(&bodyDef);
   
   b2PolygonShape shape;
   b2FixtureDef fixture;
   
   shape.SetAsEdge(b2Vec2(0.0f, 0.0f), b2Vec2(0.0f, 100.0f / PTM_RATIO));
   fixture.shape = &shape;
   fixture.filter.categoryBits = kBoxCategory;
   fixture.filter.maskBits = kBoxMask;
   body->CreateFixture(&shape);
   
   // top
   shape.SetAsEdge(b2Vec2(0.0f, 100.0f / PTM_RATIO), b2Vec2(100.0f / PTM_RATIO, 100.0f / PTM_RATIO));
   fixture.shape = &shape;
   fixture.filter.categoryBits = kBoxCategory;
   fixture.filter.maskBits = kBoxMask;
   body->CreateFixture(&shape);
   
   // left
   shape.SetAsEdge(b2Vec2(100.0f / PTM_RATIO, 100.0f / PTM_RATIO), b2Vec2(100.0f / PTM_RATIO, 0.0f));
   fixture.shape = &shape;
   fixture.filter.categoryBits = kBoxCategory;
   fixture.filter.maskBits = kBoxMask;
   body->CreateFixture(&shape);
   
   // right
   shape.SetAsEdge(b2Vec2(100.0f / PTM_RATIO, 0.0f), b2Vec2(0.0f, 0.0f));
   fixture.shape = &shape;
   fixture.filter.categoryBits = kBoxCategory;
   fixture.filter.maskBits = kBoxMask;
   body->CreateFixture(&shape);
}


// Little square creation
Code:
{
   b2BodyDef bodyDef;
   bodyDef.position.Set(20.0f/PTM_RATIO, 20.0f/PTM_RATIO);
   bodyDef.fixedRotation = true;
   b2Body *body = world->CreateBody(&bodyDef);
   
   b2PolygonShape dynamicBox;
   dynamicBox.SetAsBox(0.5f, 1.0f);
   
   b2MassData massData;
   massData.mass = 0.0001f;
   body->SetMassData(&massData);
   
   b2FixtureDef fixtureDef;
   fixtureDef.shape = &dynamicBox;   
   fixtureDef.density = 0.0f;
   fixtureDef.friction = 0.0f;
   fixtureDef.restitution = 0.999f;
   fixtureDef.filter.categoryBits = kSquareCategory;
   fixtureDef.filter.maskBits = kSquareMask;
   body->CreateFixture(&fixtureDef);
   
   body->SetLinearVelocity(b2Vec2(2.456f, 3.123f));
}


// Mousejoint creation
Code:
QueryCallback callback(p);
world->QueryAABB(&callback, aabb);

if (callback.m_fixture)
{
   b2Body* body = callback.m_fixture->GetBody();
   
   b2Filter filter;
   for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext())
   {
      filter = f->GetFilterData();
      filter.maskBits ^= kBoxCategory;
      f->SetFilterData(filter);
   }
   
   ...
   mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
   body->WakeUp();
}



// Mousejoint destruction
Code:
if (mouseJoint) {
   b2Filter filter;
   for (b2Fixture* f = mouseJoint->GetBody2()->GetFixtureList(); f; f = f->GetNext())
   {
      filter = f->GetFilterData();
      filter.maskBits = kSquareMask;
      f->SetFilterData(filter);
   }
   
   world->DestroyJoint(mouseJoint);
   mouseJoint = NULL;
}


Unfortunately the little square continue to collide with the box during the dragg and the program ran into infinite loop when the dragg is stoped !

I don't understand why but all my very simple example with box2d get me crazy and doesn't works !!! I want to kill my self... 😠

Any help will be very appreciated.

Regards,
Where is the infinite loop?
Hi Erin,

It's very strange but the infinite loop seems to be on the code which reset the mask in the mousejoint destruction method (When the dragg stops)...

But I just do another test by deleting only the code which set the mask in the mousejoint creation and the program (with the mask reset in desctruction method) doesn't crash !

I can provide you an Xcode Project if you want.

Regards,
Ah, so who calls the mouse joint destruction code?

Is it called from b2DestructionListener? Perhaps body2 has already been destroyed. I guess I should set the body pointer to NULL in this case.

If this is correct, please file a bug on Google code.
the mouse joint destruction code is called by the touchEnded iPhone event, when I stop dragging the square.
Code:
- (void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent*)event

I think the body can't be destroyed at this time because in my code I never destroyed any body and during all tests no body go out the world to be destroyed automatically...



Moreover why filtering doesn't works ? When I set, even at the creation time, the mask of the square to:
Quote:
const uint16 kSquareMask = 0xFFFF ^ kBoxCategory;

the square continue to collide the box... But if I set the square mask to:
Quote:
const uint16 kSquareMask = 0xFFFF ^ kSquareCategory;

two squares don't collide each others...
up 😳

I just starting to be more comfortable with box2d api and concept and I don't want to switch on chipmunk...

Please someone could help me ?
Have you tried migrating to the SVN version of Box2D on Google?

Have you tried looking at the data to see why it is looping infinitely?

In older versions of Box2D you need to call b2World::RefilterShape after changing the filter bits.

Remember, Box2D is free and open source. This means: 1) Support is not paid for. 2) You have all the source code, so you can debug things yourself. 3) Because of 1 and 2, you are expected to take more responsibility over your code and library usage than you would with a commercial product.

If Chipmunk is a better engine for you, I recommend you switch now.
Quote:
Have you tried migrating to the SVN version of Box2D on Google?

Yes I'm working with the latest version.

Quote:
Have you tried looking at the data to see why it is looping infinitely?

Not yet, because I'm not very comfortable with C++ 😳

Quote:
In older versions of Box2D you need to call b2World::RefilterShape after changing the filter bits.

I read that on the documentation, and when as I couldn't find this function, I make some search on the forum and I found your post explaining that this function is no more available because it is been called automatically in internal.

Quote:
Remember, Box2D is free and open source. This means: 1) Support is not paid for. 2) You have all the source code, so you can debug things yourself. 3) Because of 1 and 2, you are expected to take more responsibility over your code and library usage than you would with a commercial product.

I'm totally agree with you, even if I want to solve my problem, I also want to help you to debug (if bug exists) your library 😉

Quote:
If Chipmunk is a better engine for you, I recommend you switch now.

I don't say that and if I can figured out, I'll prefer to not switch 😉

I will do more tests, I will try to debug more in depth alone, and after I'll post my feedbacks 😉.

Thanks a lot for all.

Regards.
Reported and solved in Issue 31. Waiting for approval.
Yes, I was working on the code today and found this bug. Please fix the issue locally until I submit my changes.

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