Creating "springy" bodies

First off, thanks to Erin for such an excellent 2D physics simulator, and thanks to BorisTheBrave and mayobutter for their Flash efforts.

I'm currently using the Alchemy port of Box2D (just the basic port, not WCK)

I'm trying to create "springy" bodies - basically bubbles that are allowed to overlap but when they do they exert pressure on each other relative to the overlap area. So, if a character jumps on a pile of bubbles, they should sink down into each other and then spring back up when the character jumps off of them.

The approach I was at first considering is to make each bubble a sensor, and then go through the contact list between timesteps and apply the appropriate amount of force to each overlapping bubble pair. The only problem is that the character (and other "solid" objects) should not sink into the bubbles, and the bubbles shouldn't sink into the ground.

So - is this an area where I should add an ContactEvent listener to each bubble's fixture? Should I listen to POST_SOLVE, and modify the impulse within the event handler? Or is POST_SOLVE too late for that?

Also, the bubbles can grow in size over time and then pop. Currently I am destroying the fixture and recreating it - which means I'll have to reattach the ContactEvent listener as well. I'm a little concerned about performance - does the Alchemy port provide a way to resize fixtures without destroying/recreating them each timestep?
I'm not sure if Box2D is even appropriate for that non-rigid simulation.

For increasing the size of the bubbles, just try changing the fixtures radius - it might work well enough.
Thanks - I read somewhere else that the only way to resize the shape was to destroy/recreate the fixture, but simply changing the radius works perfectly. Just have to set allowSleep to false.

I've done the non-rigid bubble simulation using a simple sweep/prune broadphase and a runge-kutta integrator, but I also need rigid-body simulations. Maybe I should filter out bubble-bubble collisions and handle those contacts separately after the timestep?
So I figured this out by listening to PreSolve, applying a spring force for bubbles that overlap, and disabling the contact, but I'm not sure if there's a more efficient way to do this:

Code:
public function AddBubbles():void {
   for (var i:int = 0; i < 30; i += 1)
   {
      var c:Circle = new Circle(50 + i * 10, 400, 20 + 50 * Math.random());
      c.categoryBits = 2;
      //c.maskBits = 1;
      c.type = "Dynamic";
      c.graphics.lineStyle(10.0, 0x000000);
      c.fixedRotation = true;
      c.restitution = 0;
      c.friction = .3;
      c.density = .05;
      c.linearDamping = 2;
      world.addChild(c);
      c.b2fixtures[0].m_reportPreSolve = true;
      c.b2fixtures[0].addEventListener(ContactEvent.PRE_SOLVE, handlePreSolve);
   }
}

private function handlePreSolve(e:ContactEvent):void
{
   if (e.fixture.m_filter.categoryBits == 2 && e.other.m_filter.categoryBits == 2)
   {
      var c1:b2CircleShape = e.fixture.m_shape as b2CircleShape;
      var c2:b2CircleShape = e.other.m_shape as b2CircleShape;
      
      var p1:V2 = e.fixture.m_body.GetPosition();
      var p2:V2 = e.other.m_body.GetPosition();
      
      // spring constant
      var k:Number = 2;
      
      // spring resting distance (target distance)
      var tgtd:Number = c1.m_radius + c2.m_radius;
      // square the target distance to avoid square root calculations
      tgtd = tgtd * tgtd;
      
      // calculate the actual distance (squared) and subtract from target distance
      var dx:Number = p1.x - p2.x;
      var dy:Number = p1.y - p2.y;
      var d:Number = tgtd - (dx * dx + dy * dy);
      
      // apply the spring force, if necessary
      if(d > 0)
         e.applyForce(k * d);
         
      // disable this contact (just for this timestep)
      e.contact.Disable();
   }
}

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