title: Box2D - When creating a body at runtime, the body does not collide
body: <p>I've been working on a game with destructible environments and I've come up with a solution where I check for possible destruction within my <code>ContactListener</code> object. Obviously because this is taking place within <code>Step()</code>, I postpone processing the destruction until the moment after the step. I do this by pooling "destruction events" that need to be processed within the contact listener, and then immediately after <code>Step()</code> calling something like <code>contactListener-&gt;processDestructionEvents();</code>.</p>
  
  <p>The way I do this is by capturing the colliding fixtures within the beginContact event and then determining the angle of impact, then using that angle to raycast on the fixture itself. I then grap the vertices from the <code>b2PolygonShape</code> of the fixture, then generate two new shapes which are split based on the impact and exit points of the ray. The original fixture is destroyed on the body, and then a new fixture is generated for the first new shape and added to the original body. For the second shape, a new body is generated and that shape is added to this new body.</p>
  
  <p>Anyway everything works great, in debug view I can see that the new shapes have been generated and are all in place, as they should be. However, I get really screwed up behavior at this point. As soon as this process is complete, neither the original nor the newly generated body will collide with anything. If I enable continuous physics, SOMETIMES a dynamic object will collide with one of the edges of these bodies/fixtures, but not always. I'm wondering if it's something I'm doing wrong in my approach to rebuilding bodies/fixtures at runtime. Here is the code for generating the new objects, any help would be greatly appreciated.</p>
  
  <pre><code>void PhysicsContactListener::processDestructionEvents() {
     if(!hasDestructionEvents) {return;}
  
     for(destructionEventsIterator = destructionEvents.begin(); destructionEventsIterator != destructionEvents.end(); ++destructionEventsIterator) {
  
        b2Filter f1, f2;
        f1.groupIndex = destructionEventsIterator-&gt;originalFixture-&gt;GetFilterData().groupIndex;
        f1.categoryBits = destructionEventsIterator-&gt;originalFixture-&gt;GetFilterData().categoryBits;
        f1.maskBits = destructionEventsIterator-&gt;originalFixture-&gt;GetFilterData().maskBits;
  
        f2.groupIndex = destructionEventsIterator-&gt;originalFixture-&gt;GetFilterData().groupIndex;
        f2.categoryBits = destructionEventsIterator-&gt;originalFixture-&gt;GetFilterData().categoryBits;
        f2.maskBits = destructionEventsIterator-&gt;originalFixture-&gt;GetFilterData().maskBits;
  
        b2PolygonShape newShape0 = destructionEventsIterator-&gt;newFixtures[0];
  
        b2FixtureDef fixture0Def;
        fixture0Def.shape = &amp;newShape0;
        fixture0Def.density = 1.0f;
        fixture0Def.restitution = 0.2f;
  
        b2Fixture* fixture1 = destructionEventsIterator-&gt;hostBody-&gt;CreateFixture(&amp;fixture0Def);
        fixture1-&gt;SetFilterData(f1);
        //destructionEventsIterator-&gt;hostBody-&gt;SetAwake(true);
        destructionEventsIterator-&gt;hostBody-&gt;ResetMassData();
        //destructionEventsIterator-&gt;hostBody-&gt;SetActive(true);
        destructionEventsIterator-&gt;hostBody-&gt;SetTransform(destructionEventsIterator-&gt;hostBody-&gt;GetPosition(), destructionEventsIterator-&gt;hostBody-&gt;GetAngle());
  
        b2BodyDef bd;
        bd.position = destructionEventsIterator-&gt;hostBody-&gt;GetPosition();
        bd.angle = destructionEventsIterator-&gt;hostBody-&gt;GetAngle();
        bd.type = destructionEventsIterator-&gt;hostBody-&gt;GetType();
  
        b2Body* newBody = destructionEventsIterator-&gt;hostBody-&gt;GetWorld()-&gt;CreateBody(&amp;bd);
        b2PolygonShape* newShape1 = (b2PolygonShape*)(&amp;destructionEventsIterator-&gt;newFixtures[1]);
        b2Fixture* fixture2 = newBody-&gt;CreateFixture(newShape1, destructionEventsIterator-&gt;hostBodyDensity);
        fixture2-&gt;SetFilterData(f2);
        newBody-&gt;SetAngularVelocity(destructionEventsIterator-&gt;hostBody-&gt;GetAngularVelocity());
        newBody-&gt;SetLinearVelocity(destructionEventsIterator-&gt;hostBody-&gt;GetLinearVelocity());
        //newBody-&gt;SetAwake(true);
        newBody-&gt;ResetMassData();
        //newBody-&gt;SetActive(true);
        newBody-&gt;SetTransform(destructionEventsIterator-&gt;hostBody-&gt;GetPosition(), destructionEventsIterator-&gt;hostBody-&gt;GetAngle());
  
        destructionEventsIterator-&gt;hostBody-&gt;DestroyFixture(destructionEventsIterator-&gt;originalFixture);
  
     }
  </code></pre>