Making bodies static after adding them

I'm trying to add the ability to make bodies static/dynamic without having to remove them and add them again. This code almost works:
Code:
        public void setFixed(boolean v)
        {
            if(v && m_mass != 0)
            {
                m_pmass = m_mass;
                MassData md = new MassData();
                md.mass = 0;
                md.I = m_I;
                md.center = ((PolygonShape)m_shapeList).getCentroid();
                setMass(md);
                m_type = e_staticType;
            }
            else
            {
                MassData md = new MassData();
                md.mass = m_pmass;
                md.I = m_I;
                md.center = ((PolygonShape)m_shapeList).getCentroid();
                setMass(md);
            }
        }

But when a moving body collides with the static body, its vertical velocity doesn't get affected, so it will start jumping around and then fall through and off the screen. What's wrong?
First off, I have to thank you for noting this problem. It's something that my level editor was also suffering from though I never noticed it until you said something! It seems that the problem is that our newly static bodies still have some velocity that is affecting collision response. I first noticed this when I created a dynamic body and immediately made it static without starting the simulation (so the body never had a chance to start moving). This body did not suffer from the strange problem you describe, whereas a body that had been allowed previously to move would cause problems. So, the solution is (and I tested this to make sure) to set the linear and angular velocities to zero when going from dynamic to static (or kinematic if you've bothered to add that in). 😀

Following is the the code I am using to switch body types

Code:
Body body = shapes.get(i).getBody();
switch( si )
{
   case Body.e_dynamicType:
   {
      for( Shape s = body.getShapeList(); s != null; s = s.getNext() )
         s.setDensity( 1.0f );
      body.setMassFromShapes();
      break;
   }
   case Body.e_staticType:
        case Body.e_kinematicType:
   {
      body.setLinearVelocity( new Vec2() );
      body.setAngularVelocity( 0.0f );
      for( Shape s = body.getShapeList(); s != null; s = s.getNext() )
         s.setDensity( 0.0f );
      body.setMassFromShapes();
      break;
   }
   body.setType( si );
}


The body.setType( int ... ) function is something I added into the Body.java class. All it does is set the body flag to e_staticType or whichever is appropriate.

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