Explosions

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.

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())
         }
      }
      
   }
Thanks! I guess you're using pixel unit? I had to tweak the measurements abit, but it works nice.

I guess an alternative approach would be to create debris at the point and have their projection affect the environment?
after experimenting with the debris method I figured out you'd have to create a lot of debris to get the same effect and that's sucks up a lot more performance. We still create some physical debris, but it's more like a few pieces of shrapnel. We use a combination the explosion phyiscs, some physical debris, and a purely visual effect for the 'ball of flame'.
Ok, thanks for sharing your conclusions.
Hello,
I am new to Box2D but , I managed to get a ball moving by keys like a char. Now I try to get the great explosion-script to work... well, I implanted it and the good thing is: there are no errors, but the effect doesn't show up. Nothing happens when I press the ENTER-Key (see in the code)!
here the important part of my code:

1. I put the World class in an World.as with package and stuff to import it.

2. Add that in my test class:

Code:

public function Test(){
...
EXWorld = new World(worldAABB, gravity, doSleep);
...
public var EXWorld:World;
...
      public function Update():void{
                          // It's a loop function :)
         ...
         
         if(Input.isKeyDown(Keyboard.ENTER)){
         EXWorld.explode(ball.m_position);
         }

         ...
      }
}


I'm using the "PhysTest.fla" by Matthew Bush
... why doesn't it work ? 😢
Help would be great!

Greetings,
Chris 😎
Ok...well now I got it... my fault was to create a new World; but I have to set the World function for my global "m_world"
like this:
Code:
m_world = new World(worldAABB, gravity, doSleep);
m_world.explode(ball.m_position);


The only thing I don't understand is that it only works in the debug mode, thats strange...maybe somebody knows why..?

CU

Edit: It works also in the release version, but if(Input.isKeyDown(Keyboard.ENTER)) has no effect...whatever...
THX for the code !

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