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
// Centered box creation
// Little square creation
// Mousejoint creation
// Mousejoint destruction
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,
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
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);
}
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));
}
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();
}
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;
}
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,