# Box2D Forums

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

 All times are UTC - 8 hours [ DST ]

 Page 1 of 2 [ 18 posts ] Go to page 1, 2  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Quick ExplosionsPosted: Mon May 11, 2009 3:16 pm

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();
var vMax:b2Vec2 = position.Copy();
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

 Post subject: Re: Quick ExplosionsPosted: Mon May 11, 2009 3:21 pm

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

 Post subject: Re: Quick ExplosionsPosted: Mon May 11, 2009 3:30 pm

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

 Post subject: Re: Quick ExplosionsPosted: Tue May 12, 2009 2:00 am

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

 Post subject: Re: Quick ExplosionsPosted: Tue May 12, 2009 7:28 pm

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

 Post subject: Re: Quick ExplosionsPosted: Wed May 13, 2009 1:12 am

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

 Post subject: Re: Quick ExplosionsPosted: Wed May 13, 2009 3:36 pm

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

 Post subject: Re: Quick ExplosionsPosted: Wed Aug 12, 2009 9:28 am

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

 Post subject: Re: Quick ExplosionsPosted: Wed Aug 12, 2009 1:58 pm

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

Top

 Post subject: Re: Quick ExplosionsPosted: Wed Aug 12, 2009 3:00 pm

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 2 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: