No collision with certain bodies

Hello there,
I'm having an issue with some static bodies, a really strange one. Basicly there's no collision between dynamic and these static bodies. I used the Box2D debug renderer and I can see the bodies quite well. I've also checked the contact filter, shouldCollide function returns true, but there's no call of begin/end contact. I really can't understand what is wrong, if it returns true why shouldn't they collide? :\ Can someone explain me when does this happen and what it is caused to?
Are they connected by a joint with collideConnected set to the default value of false?
No, there are no joints.
Still having the same problem, I'm creating some boundary boxes around the screen.
This is the way I do it:

Code:
      void World::setBoundaries(const Math::Rectf& rect)
      {
         const float size = 4.f;
         destroyBoundaries();
         m_boundaries.resize(4);

         for (uint32 i = 0; i < 4; ++i) {
            m_boundaries[i] = new Box();
            m_boundaries[i]->setStatic();
         }

         Math::Vector2f lr_size   = Math::Vector2f(size, rect.getHeight());
         Math::Vector2f tb_size   = Math::Vector2f(rect.getWidth(), size);

         m_boundaries[0]->setSize(lr_size);
         m_boundaries[1]->setSize(tb_size);
         m_boundaries[2]->setSize(lr_size);
         m_boundaries[3]->setSize(tb_size);

         Math::Vector2f pos = rect.m_p1;
         Math::Vector2f left_pos      = Math::Vector2f(pos[0]-size/2.f, pos[1]+rect.getHeight()/2.f);
         Math::Vector2f top_pos      = Math::Vector2f(pos[0]+rect.getWidth()/2.f, pos[1]-size/2.f);
         Math::Vector2f right_pos   = Math::Vector2f(pos[0]+rect.getWidth()+size/2.f, pos[1]+rect.getHeight()/2.f);
         Math::Vector2f bottom_pos   = Math::Vector2f(pos[0]+rect.getWidth()/2.f, pos[1]+rect.getHeight()+size/2.f);

         m_boundaries[0]->setPosition(left_pos);
         m_boundaries[1]->setPosition(top_pos);
         m_boundaries[2]->setPosition(right_pos);
         m_boundaries[3]->setPosition(bottom_pos);

         for (uint32 i = 0; i < 4; ++i)
            m_boundaries[i]->create();
      }

All the bodies I create and transfrom collides between each other, but they do not collide with left and right boundary boxes. Positions and sizes of these boxes are right, but for unknown reasons all my bodies begins to contact only with the top and botton boxes. For the lateral ones ShouldCollide returns true, but doesn't begin to contact and my contactlistener isn't called.

I'm using box2D only for collision detection so my world step is 0, I do something like this
Code:
world->Step(0.f, 10, 2);

Can it be the problem? If so, why are top and botton working and the others two not?
The code there does not show how you are creating the walls. Are you getting the vertex winding order correct? It should be counter-clockwise.
Code:
         // World::create         
         b2Body* body = getBody(obj);

         if (body) return;

         uint32 type = obj->getType();

         Math::Vector2f pos   = pixelToMeter(obj->getPosition());
         Math::Vector2f c   = pixelToMeter(obj->getCenter());
         float fRotation      = Math::deg2rad(obj->getRotation());

         b2BodyDef bodyDef;
         bodyDef.position   = b2Vec2(c[0], c[1]);
         bodyDef.type      = static_cast<b2BodyType>(obj->getBodyType());
         body            = m_world->CreateBody(&bodyDef);
         
         body->SetUserData(reinterpret_cast<void*>(obj));
         setBody(obj);
         body->SetTransform(b2Vec2(pos[0], pos[1]), fRotation);


         // World::setBody
         Math::Vector2f size = pixelToMeter(obj->getSize());

         b2PolygonShape polDef;
         polDef.SetAsBox(size[0] / 2.f, size[1] / 2.f, b2Vec2(center[0], center[1]), 0.f);
         
         b2FixtureDef fixDef;
         fixDef.filter.groupIndex = obj->getGroup();
         fixDef.shape = &polDef;
         fixDef.density = obj->getDensity();
         fixDef.friction = obj->getFriction();
         fixDef.restitution = obj->getRestitution();
         
         body->CreateFixture(&fixDef);
         body->ResetMassData();


This is how my interface creates boxes, I can't see anything wrong. Between the floor coordiantes are ok, I also can see the boxes using box2d debugger, I clearly see that when the others bodies get closer to the left and right boxes, should collide gets called and returns true, but BeginContact isn't called.

Image
The purple boxes shown in that screenshot look like the bounding boxes used internally by the collision detection to make collision checks more efficient. They are a loose fit around the fixture, so for them to be touching does not mean that the fixture inside them is touching. The ShouldCollide will be called when those bounding boxes begin to overlap, but BeginContact will not be called until the actual fixtures inside begin to touch. Since you're only using Box2D for collision detection, I guess you are moving the bodies with SetTransform, which is like teleporting them around. I don't know if it will help, but you could try calling the Refilter function of fixtures after you move their body.
Apart from that slim chance, I can't see anything wrong either, and I can't think why it would only happen for some of the walls. What happens if you don't create the top and bottom walls at all, and you have a sideways gravity?
irresistible force wrote:
The purple boxes shown in that screenshot look like the bounding boxes used internally by the collision detection to make collision checks more efficient. They are a loose fit around the fixture, so for them to be touching does not mean that the fixture inside them is touching. The ShouldCollide will be called when those bounding boxes begin to overlap, but BeginContact will not be called until the actual fixtures inside begin to touch. Since you're only using Box2D for collision detection, I guess you are moving the bodies with SetTransform, which is like teleporting them around. I don't know if it will help, but you could try calling the Refilter function of fixtures after you move their body.
Apart from that slim chance, I can't see anything wrong either, and I can't think why it would only happen for some of the walls. What happens if you don't create the top and bottom walls at all, and you have a sideways gravity?


just checked right now, it gives me the same problem when using gravity. Infact, box2d doesn't make the bodies collide, even if they overlaps
problem solved.
aww come on, tell us 🙂

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