In the thread for the tank game phervers and I are working on, someone asked how we do explosions.
The first thing we did was extend the b2World object so we can execute our explosion command on that object. This code does cover any visual explosion effects or debris creation, just the explosive force acting on surrounding bodies.
The first thing we did was extend the b2World object so we can execute our explosion command on that object. This code does cover any visual explosion effects or debris creation, just the explosive force acting on surrounding bodies.
Code:
public class World extends b2World
{
public function World(worldAABB:b2AABB, gravity:b2Vec2, doSleep:Boolean)
{
super(worldAABB, gravity, doSleep);
}
//TODO: pass a position, not a body
public function explode(position:b2Vec2):void
{
var aabb:b2AABB = new b2AABB()
var vMin:b2Vec2 = position.Copy();
vMin.Subtract(new b2Vec2(100, 100));
var vMax:b2Vec2 = position.Copy();
vMax.Add(new b2Vec2(100, 100));
aabb.minVertex = vMin;
aabb.maxVertex = vMax;
var shapes:Array = new Array();
this.Query(aabb, shapes, 100);
for (var i = 0; i < shapes.length; i++)
{
var b:b2Body = shapes[i].GetBody();
var fv:b2Vec2 = b.GetCenterPosition().Copy();
fv.Subtract(position);
fv.Normalize();
fv.Multiply(500000);
b.WakeUp();
b.ApplyForce(fv, b.GetCenterPosition())
}
}
}
{
public function World(worldAABB:b2AABB, gravity:b2Vec2, doSleep:Boolean)
{
super(worldAABB, gravity, doSleep);
}
//TODO: pass a position, not a body
public function explode(position:b2Vec2):void
{
var aabb:b2AABB = new b2AABB()
var vMin:b2Vec2 = position.Copy();
vMin.Subtract(new b2Vec2(100, 100));
var vMax:b2Vec2 = position.Copy();
vMax.Add(new b2Vec2(100, 100));
aabb.minVertex = vMin;
aabb.maxVertex = vMax;
var shapes:Array = new Array();
this.Query(aabb, shapes, 100);
for (var i = 0; i < shapes.length; i++)
{
var b:b2Body = shapes[i].GetBody();
var fv:b2Vec2 = b.GetCenterPosition().Copy();
fv.Subtract(position);
fv.Normalize();
fv.Multiply(500000);
b.WakeUp();
b.ApplyForce(fv, b.GetCenterPosition())
}
}
}