Box2D Forums

It is currently Sat May 25, 2013 11:19 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Quick Explosions
PostPosted: Mon May 11, 2009 3:16 pm 
Offline

Joined: Wed May 06, 2009 11:14 am
Posts: 62
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!


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Mon May 11, 2009 3:21 pm 
Offline

Joined: Fri May 16, 2008 2:52 pm
Posts: 280
I am actually still using this in my game!

The most horrible thing is that it always applies the force to the center of gravity. There was a thread a while ago about making real explosions (involving lots of raycasts), and I think a more realistic, albeit slower, method came out of that.


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Mon May 11, 2009 3:30 pm 
Offline

Joined: Wed May 06, 2009 11:14 am
Posts: 62
Ah! Is this your code then? I tried re-searching for the thread but couldn't find it.

The square bounding box is one of my biggest complaints, but once that is solved the center-of-mass-bug is the next biggest complaint I have.

I noticed the raycasting thread but I believe the author ended it with "I'll post the code once it's stable" and left it at that. I was posting this in the meantime, while we wait, but if I recall the last post was in October so I lost hope :)


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Tue May 12, 2009 2:00 am 
Offline

Joined: Fri May 16, 2008 2:52 pm
Posts: 280
Nope, this isn't my code. I found it on a forum thread somewhere I think.

Even the square bounding box isn't really a problem for me. I guess you could notice it if you have a very large area and objects that are uniformly spread out over that area :).


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Tue May 12, 2009 7:28 pm 
Offline

Joined: Wed May 06, 2009 11:14 am
Posts: 62
One thing I'm noticing with this code - or perhaps it's all in my head? The explosion strength is uniform throughout the radius. I don't think the normalize function is behaving the way I think it is (and the code I stole thinks it is).

Am I crazy or is that true?


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Wed May 13, 2009 1:12 am 
Offline

Joined: Fri May 16, 2008 2:52 pm
Posts: 280
Well, you are contradicting yourself. The force is being normalized, so the strength is uniform at any range within the explosion ^_^. If you would *not* normalize, the force would be greater further away from the center. So to get a greater force near the center you would have to somehow invert this.


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Wed May 13, 2009 3:36 pm 
Offline

Joined: Wed May 06, 2009 11:14 am
Posts: 62
Hm. Someone had posted to a thread earlier that "Normalize" records the original number internally and the multiply function acts on the old digits.

This doesn't make any sense at all and of course all the b2Vec2 routines aren't documented at all in the API. :(

Oh well! Here's the new code that applies a larger force to closer objects and linearly reduces as it extends out:

Code:
         var forceVec:b2Vec2 = new b2Vec2(0,0);
         var distance:b2Vec2 = new b2Vec2(0,0);
         for (var i = 0; i < shapes.length; i++) {
            b = shapes[i].GetBody();
            if (b.GetWorldCenter() != position) {
               distance = b.GetWorldCenter().Copy();
               distance.Subtract(position);
               if (distance.x >= 0)
                  forceVec.x = -distance.x + radius;
               else
                  forceVec.x = -distance.x - radius;
               if (distance.y >= 0)               
                  forceVec.y = -distance.y + radius;
               else
                  forceVec.y = -distance.y - radius;
               forceVec.Multiply(strength);
               b.WakeUp();
               b.ApplyForce(forceVec, b.GetWorldCenter());
            }
         }


If you prefer a less linear slope in the decrease you can throw in square roots, but for the sake of my processor I think this does the trick.


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Wed Aug 12, 2009 9:28 am 
Offline

Joined: Wed Oct 01, 2008 2:59 pm
Posts: 32
I like this quick and dirty solution to creating explosions! Works great...except for ( like the previous posts talk about ) the force only being applied to the center of mass for each body. Anyone have ideas for how to apply it elsewhere ( I'm guessing the closest point on the body to the explosion origin maybe ) in order to give the bodies some rotational force? Right now when you "explode" something it won't rotate ( of course ), so it doesn't look quite right. But other than that, it works very well.

Is there a quick way to get the closest point on a given body to another point in the world? If so, then we could just apply the force at that point and hopefully that would give the effect we're looking for? Any ideas?


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Wed Aug 12, 2009 1:58 pm 
Offline

Joined: Wed Jan 21, 2009 11:45 pm
Posts: 50
could somebody upload a demo?


Top
 Profile  
 
 Post subject: Re: Quick Explosions
PostPosted: Wed Aug 12, 2009 3:00 pm 
Offline

Joined: Wed Oct 01, 2008 2:59 pm
Posts: 32
I don't have a demo ( I implemented it into a bigger project ). It's pretty simple though, instead of extending this from b2World I simply added it as a function somewhere and replaced "this." in the Explode function with my world object...everything else can stay the same.

I was also thinking: if it's too much trouble to figure out the closest point on the body to the explosion origin, maybe we can take a look at the delta between the explosion origin and the body center of mass and use those somehow in order to fake / force a torque value. Not sure exactly how that would work ( and of course it wouldn't be realistic ), but I think that might be another "quick and dirty" way to get things to rotate away from the explosion.

Any ideas?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: psbot [Picsearch] and 6 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