Idea for implementing explosions

One problem with simulating an explosion realistically is the way in which the blast should apply force to parts of an object rather than just its center of gravity. For example, in the really simple way to do it (apply impulse to center of mass of objects (inverse square) proportional to distance from explosion), if you place a bomb under one side of a long plank or some such object, the object will move diagonally and will never rotate, whereas realistically the long object will have the part lying over the explosive be sent upwards with great force, which results in torque and lots of rotation.

Something that I had been thinking about to provide support for this was to raycast radially outward from explosion point and apply forces based on the ray collisions. This however has to be tweaked and will miss small objects. If you want it to work on small objects it could end up running quite slow.

So since there is a way, built into box2d, that measures the penetration/separation of two shapes when they are intersecting, why not just create a circular sensor or shape when an explosion goes off, and for each shape that it intersects, calculate the "penetration" value, determine the location of intersection, and apply forces accordingly? Does anyone see anything wrong with this approach? It should be possible to do it, right?
No, sounds pretty good to me. Make sure nothing is overlapping the point of explosion though, or else you will get odd results.
BorisTheBrave wrote:No, sounds pretty good to me. Make sure nothing is overlapping the point of explosion though, or else you will get odd results.


Well my impression was that if you spawned a circle with its center intersecting another shape it will still be able to give a reasonable vector of maximal separation by comparing the center of the circle with the centroid/center of whatever shape it is.
Firstly, I don't think that's what would give the separation in that case. But my point is you wouldn't expect a shape containing the explosion to be blown away at all, and also, the depth of penetration would be more than the radius of the sensor, which is hard to interpret in a "how far away from the explosion" sense. That's all. But try it, I'm sure explosions are best sorted out qualitatively.
So the b2Distance function is quite helpful for doing what I wanted to do, but i didn't realize that just having the closest point to the explosion is not enough.

Suppose you have the explosion at (0,0), with the floor being the x-axis, and with a square block placed on the floor.

The closest point to the explosion would be at a bottom corner of the square, which means when the force is applied at that point the block will get an unrealistic amount of torque. I need to do something that finds the radial cross section of the shape in relation to the point of explosion. Also i hadn't thought of objects absorbing the explosion force so that something behind something else should not receive a force.

In order to get complete accuracy we'd have to do something like integrate over the edges of the shapes that are directly in the path of the explosion and then apply forces at each edge. That's too complicated so we can simplify that to just finding the arc along the explosion radius that a shape "blocks", evaluate the forces at either end and apply forces to the ends. To solve the overlap problem (Which shapes come in front of the others in terms of being hit by explosion) i guess we can have a datastructure that compares the distances of each arc and things that are closer will take precedence.

There was a topic on this earlier (viewtopic.php?f=3&t=1192) and Irresistible Force had some nice diagrams that he drew. I think i'll try doing something like that.
Thinking about it some more, the method I mentioned earlier (only using two points for each shape, etc) would still have problems if you tried to explode something near a large but simple shape. For example, you have a giant rectangular block and have an explosion near one end. The far end will be so far out of range that it receives no force while the other end receives all of the force from the explosion. Qualitatively the resulting rotation direction is still correct but there are major inaccuracies.

I think what I'm going to try next is the ray-cast method, with some stuff added in to "catch" any small objects which may have been missed by the rays, by making triangles between the rays and testing for collisions.
I have an idea how this could theoretically be accomplished relatively easy without using a circle.

First you do a query around the explosion point of all the objects you should hit. Then you go around the explosion point doing ray casting and crossing things off the list. After you do one through, if there is anything else on the list, you do a second pass through but this time you do it at a degree off set of half, and you continue this until you hit all the bodies.

An example might be say you cast rays every 15 degrees, next time you go through you offset by 15/2, next 15/4, and so on. If an object is very tiny, it might take a ton of passes, so what you do is you do this within a constrained range. For instance if you do 2-3 passes and haven't hit a particular object, you figure out what range it is in, and then do this method for the range only. So if you determine an object is between 15-30 degrees, then you would keep doing pass through from 15-30 halving each time. It might not yield perfectly accurate results, but it would be fast.

I believe this method is the newtonian algorithm, but don't quote me on that.
Yeah, that actually sounds like a really good idea, because ray casts will ensure that no extra force is applied to objects that are being blocked, and an AABB query plus possible full collision checking should do the trick for identifying objects which are within range.

The only potential problem I see is calculating just how much force to apply. Say your object only covers 3 degrees of arc relative to the explosion. Using this method I don't see a straightforward way to calculate this arc length value, which is going to be proportional to the force that should be applied. If that object happens to intersect with one of those 'every 15 degree' rays then it'll get 5 times the force it should. I guess this is where we'd introduce a fudge factor and be done with it.

I thought of it some more and i think there should be a simple method to find the points on each shape which are at the extreme points on the arc. A diagram would be nice but I have no time to make one now.
What you would do is look at each vertex in the shape and the ones that give an angle farthest in either direction are the extreme points. Now i don't really know exactly how to use these...
Can't you just find the nearest point on the segment/edge to the explosion point and then just apply force that? For bodies made up of lots of shapes, you can sort the result of all the found nearest points by distance to the centre of the explosion and use the closest to apply the force to.
Okay, I haven't fully thought this through yet. Let's say you get your two "extreme" angles. Then what you do is cast, say 10 rays evenly distributed over that range. Then... you need to figure out how much force to apply at each of those points, which should be proportional to the distance to the explosion. Since each object gets exactly 10 forces applied to it, would it be fair to make this number directly proportional to the distance, or is there another variable (like average distance) we need to throw into the mix? Also, even if we cast 10 rays in the direction of the object, there may be another object in front occluding some of the rays. In that case, part of the blast would be blocked, and the object would receive less force as expected. We have to make sure though that the blocking object does not receive the force instead though; it will get it's own set of 10 rays later. Thus, we have to check which object each of the 10 rays hit, and make sure its the same one as the one we are currently applying force to.

How's that solution sound? Just use an AABB query to find the objects and no firing rays in random directions, just towards object we know exist, and it wont miss small objects.

explode.gif
explode.gif (9.25 KiB) Viewed 3864 times


Actually... crap. I think there *is* a problem with this. Large objects would still receive less force than they should, because the furthest points may receive little or no force at all, whereas a small object would receive forces along all its points.

In that case, can we factor in average distance of each of the points, and if the average distance is large (because a few rays hit very far away) then we scale everything UP so that the closer rays are weighted higher? That doesn't sound quite right either.... it would be nice instead of using 10 rays, we could space them evenly across the object. Degree separation won't work for this... unless maybe we take the distance between the two extreme points on the object and make the number of rays relevant to that instead ?!

I dunno. Food for thought.

PS:
apathy wrote:Can't you just find the nearest point on the segment/edge to the explosion point and then just apply force that?
dc443 wrote:...just having the closest point to the explosion is not enough.

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