I did a google search and found some Box2D explosion-making code that extended the main b2World. It was for an older version so I rewrote it to work with the current; I couldn't find a source to cite:
Code:
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2Body;
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
public class ExplodeWorld extends b2World {
public function ExplodeWorld(worldAABB:b2AABB, gravity:b2Vec2, doSleep:Boolean) {
super(worldAABB, gravity, doSleep);
}
public function explode(position:b2Vec2, radius:Number, strength:Number):void {
var aabb:b2AABB = new b2AABB()
var vMin:b2Vec2 = position.Copy();
vMin.Subtract(new b2Vec2(radius, radius));
var vMax:b2Vec2 = position.Copy();
vMax.Add(new b2Vec2(radius, radius));
aabb.lowerBound = vMin;
aabb.upperBound = vMax;
var shapes:Array = new Array();
this.Query(aabb, shapes, 100);
var b:b2Body;
var forceVec:b2Vec2;
for (var i = 0; i < shapes.length; i++) {
b = shapes[i].GetBody();
forceVec = b.GetWorldCenter().Copy();
forceVec.Subtract(position);
forceVec.Normalize();
forceVec.Multiply(strength);
b.WakeUp();
b.ApplyForce(forceVec, b.GetWorldCenter())
}
}
}
It only explodes items within a square bounding box, and it applies the force vector to the center of the body (not the closest point), and it even seems fairly horrible to me that it extends the b2World instead of just being it's own little class.
That said, it works, and it's a very quick and dirty way to put explosions into your Flash Box2D app. I had to use a strength of 500,000 to get any results on larger/heavier objects.
Maybe I'll work on something a bit more circular and less dependant on B2World in the coming months. Hope this helps someone!