Box2D Forums

It is currently Sat May 18, 2013 10:56 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Tue Jul 29, 2008 12:16 pm 
Offline

Joined: Mon Sep 17, 2007 8:31 pm
Posts: 40
I'm trying to simulate an explosion force by applying force to any objects that are near the point of explosion. I use a world query to find shapes that are nearby, then apply force to each using the following code:
Code:
shapes[i]->GetBody()->ApplyForce(amount, bomb->body->GetWorldCenter());

It successfully throws the bodies around, but they do not rotate. If the explosion is to one side, it should cause the affected bodies to spin around a bit, right? Am I doing something wrong here? :?:
Thanks. :)


Top
 Profile  
 
PostPosted: Tue Jul 29, 2008 12:53 pm 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
The code you've got there is the standard way, and it doesn't cause any rotation. You might expect some from an explosion but it's not obvious how something should rotate. It depends quite a bit on the actual shape of the object.
Perhaps you should apply a force to each vertex of a polygon, instead of one to it's center. If the forces vary according to the individual distances, that should at least make things rotate in the right direction most of the time.


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 10:27 am 
Offline

Joined: Fri May 16, 2008 10:09 am
Posts: 337
BorisTheBrave wrote:
Perhaps you should apply a force to each vertex of a polygon, instead of one to it's center. If the forces vary according to the individual distances, that should at least make things rotate in the right direction most of the time.


This actually seems like a pretty complicated problem since you can't just apply forces directly on vertices. What if you had a long stick with one end rounded out and one end flat, then an explosion near the center would propel the curved part with more force, which doesn't make any sense.

I believe a somewhat more realistic way to do it is to project lines from the explosion radially and apply appropriate forces for each collision. This way if you had say a block sort of lying at the very edge of a cliff and a large explosion goes off in the cliff, the block could actually behave correctly by having only the part overhanging be pushed upwards.

It would be interesting to apply this to the drawing of visual effects related to the explosion as well. If it is blocked by some solid object, the shockwave and dust should not penetrate. Sounds awfully complicated just for something thats rather subtle though.


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 12:57 pm 
Offline

Joined: Sun Sep 23, 2007 2:35 pm
Posts: 803
The "best" solution (i.e. most realistic) would be to create a large number of raycasts from the explosion point and apply a constant impulse for each raycast that hits an object. No radial falloff is necessarily needed, as you'll automatically get a 1/r falloff in 2D based on the spreading of the rays (in 3D you'd want a 1/r^2 falloff, so to simulate that in 2D you'll need to add an additional falloff to the impulse strength).

To go further, and get "pressure" effects (an explosion is more powerful per unit of area if it's completely enclosed), you'd want to bounce the rays after each hit, probably stopping after some distance to avoid infinite recursion.

Another refinement would be to add some friction to the impulse (rather than merely pushing against the normal, factor a small amount of tangent force).

This could be all add up to a pretty computationally intensive method, so I'm not sure if it's feasible in real time - maybe for an occasional explosion it could work, though. You'd have to play around, see if you can manage several dozen raycasts without a visual glitch. If you try this, make sure to bound the explosion by an AABB and only TestSegment against objects contained in that AABB, you definitely don't want to check each ray against the whole world.


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 1:49 pm 
Offline

Joined: Fri May 16, 2008 10:09 am
Posts: 337
I have just implemented reflecting laser beams (with non-infinite velocities) based on raycasts (each physics step, perform a ray cast from position to position + velocity * phys_timestep), and I am currently still using the naive check-against-all-bodies method to test for collisions. It runs remarkably fast but that is likely to change once the number of b2shapes increases. I can currently spawn 5000 light beams in an enclosed area, each traveling at over 500 units/sec velocity and not one collision with static bodies will be missed -- it runs at 30 frames per second.

This is interesting to me because I can easily simulate an explosion like ewjordan suggests by just using very slight modifications to my light beam code which is incredibly robust and hopefully decently fast once the testSegment code is optimized.

Also it seems to me that using a sort of ray-cast style device can help create something like a particle system effect, without requiring the instantiation of additional physics entities while still hopefully being able to query the physics state to enable collisions with geometry. So while an explosion is happening, a couple of extra "randomized" beams could be generated which might not necessarily generate any impulses but could be used to draw high speed "sparks". A gravity force can easily be incorporated as well.

this could also be great for drawing explosion effects which realistically reflect the shockwave as it reaches solid objects. Could make some very neat looking things with pixel shaders (I am writing some generalized code for converting texture data into per-pixel distortion values)


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 2:15 pm 
Offline

Joined: Thu Mar 27, 2008 7:53 pm
Posts: 53
Location: WA, US
You COULD just let the physics engine handle it, and create a dozen or 2 dozen bodies at the explosion, distribute their initial impulses around a 360 circle, randomize body size and impulse within some reasonable range, and let the .... crap hit the fan.

After a few frames delete the objects.

Or, set the m_userData object to a class that has a method that gets stepped every frame, and after some frame count, have the bodies delete themselves.


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 2:19 pm 
Offline

Joined: Fri May 16, 2008 10:09 am
Posts: 337
I've done that in the past, it's pretty good since you get free "particle effects" by doing that, but the performance is kinda yucky. Plus there's the chance that there's a "hole" and your explosion might "miss" sometimes.


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 3:14 pm 
Offline

Joined: Thu Mar 27, 2008 7:53 pm
Posts: 53
Location: WA, US
I think with the right settings it can look great!

Here's a little demo...

http://www.brainblitz.org/BoxCAD/?load=bomb-43466
(if it doesn't load, click file, and paste "bomb-43466" into the load field).

THAT demo isn't all that great, because I don't have time to make lots of shrapnel, or perfect the particle engine settings, or arrange several partical engines in a radial configuration...

But, there are about 100 particles, and it runs great. Also, in his case, he won't even be displaying them - so it should hardly cause a performance hiccup.

Note - there is also an object there with negative gravity to produce a more "uniform pressure."


Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 7:29 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
It shouldn't take any more than two forces to get the body to spin reasonably. I haven't got to this yet, but here's how I am thinking to do it.
Image
Most likely the pic is explanatory enough, but just in case: lines a and b are taken from the two vertices which are farthest from the line between the explosion point and the center of mass. Then the explosion force is applied at each of these points, proportional to how far it is from that line.
Hmm... in the diagram above I have drawn the resultant forces as remaining parallel to the original line through the CoM, but I was thinking, you could also apply them to go away from the explosion point:
Image
Now this wouldn't make any difference to the way the body reacts to the explosion, but if you wanted the explosion to break bodies apart, it would give you a reasonable measure to use in deciding if this particular shape has enough conflicting force on it to break, since these two forces will be more opposed the closer the shape is to the explosion point.


Last edited by irresistible force on Tue Aug 05, 2008 9:54 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Aug 05, 2008 7:37 pm 
Offline

Joined: Fri May 16, 2008 10:09 am
Posts: 337
Wow, thats actually really clever and would probably be a good amount more efficient than ray tracing. Plus it wouldn't get less accurate with smaller objects (which might not end up lying in the path of a ray)


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 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