Box2D Forums

It is currently Wed May 22, 2013 3:05 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Explosions
PostPosted: Thu Dec 20, 2007 3:30 pm 
Offline

Joined: Wed Nov 28, 2007 9:46 pm
Posts: 34
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())
         }
      }
      
   }


Top
 Profile  
 
 Post subject: Re: Explosions
PostPosted: Thu Dec 20, 2007 5:21 pm 
Offline

Joined: Tue Dec 11, 2007 3:05 am
Posts: 106
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?


Top
 Profile  
 
 Post subject: Re: Explosions
PostPosted: Thu Dec 20, 2007 11:24 pm 
Offline

Joined: Wed Nov 28, 2007 9:46 pm
Posts: 34
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'.


Top
 Profile  
 
 Post subject: Re: Explosions
PostPosted: Fri Dec 21, 2007 1:16 am 
Offline

Joined: Tue Dec 11, 2007 3:05 am
Posts: 106
Ok, thanks for sharing your conclusions.


Top
 Profile  
 
 Post subject: Re: Explosions
PostPosted: Wed Jan 30, 2008 5:17 pm 
Offline

Joined: Wed Jan 30, 2008 4:51 pm
Posts: 2
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 ? :cry:
Help would be great!

Greetings,
Chris 8-)


Top
 Profile  
 
 Post subject: Re: Explosions
PostPosted: Thu Jan 31, 2008 8:00 am 
Offline

Joined: Wed Jan 30, 2008 4:51 pm
Posts: 2
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 !


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group