Idea for implementing explosions

dc443 wrote:Just finished the bit that calculates the max and min arc coverage on a per-shape basis

I think this would be a really useful addition to Box2D as well, please consider publishing. I wanted it to do dynamic lighting effects rather than explosions, but it's all good.
Yeah, I'll get back to this in a little bit. Have been messing with my convex decomposition and dynamic breakage code. Ever since I started coding on linux I discovered there were many things my program was doing wrong. I got it all working great now though.
Okay, the code is now almost done, all that remains is the bit to calculate exactly how much of an impulse each raycast applies.

Image

Notice that the big C-shaped object has 20-something raycasts... this is because of a maximum-angle parameter.

I ended up doing something a little more involved than originally planned. Here's some details:

After locating the max and min angles of each shape within range, I put all of these values (both min and max) into an array and sort the array based on angle. I may change this to only include the min/max values for the b2Shapes, though this could introduce incorrectness if you have any bodies made up of shapes that aren't "connected".

Then I do a ray cast (using b2World::RaycastOne) in the direction of each midpoint in the resultant angle-sorted list of boundaries. This means exactly two raycasts are performed for each b2Shape. What this allows for is if a long object is partially obscured by a shorter object, it will result in the long object having more than one angular "segment" on which to apply forces (because the smaller object has blocked a section of it). For segments that are adjacent but raycast to the same shape, we know that the shape is continually exposed to the explosion point along that edge, and can then treat the two sections as one later on.

There is some weird stuff done on the edge case (the boundary between -pi and pi radians), but its simple enough.

Then for each "segment" (I used a std::vector to buffer these), each of which represents a projected angular cross section that belongs to a single b2Body, we figure out how many raycasts to do on it based on parameters such as:
  • maximum angle between raycasts
  • minimum angle from edges of segments to beginning and ending raycasts (and a ratio associated with this)
  • minimum number of raycasts on any one segment

then we do the final raycasts, this time just using b2Shape::TestSegment on the appropriate shapes. Since I grouped the segments according to which b2Body they belong to, this does involve looping through all shapes in the relevant body.
Out of curiousity what method are you using to calculate the minimum and maximum points that rays hit each shape?

I'm implementing my own version using a variation of Binary Search (http://en.wikipedia.org/wiki/Binary_search) but I think this is a slow way to do it and it only ever returns an approximation.

I'll post the code once it's fully tested and working.
bedroomlab wrote:Out of curiousity what method are you using to calculate the minimum and maximum points that rays hit each shape?


AABB query on a box containing the explosion area, then loop thru all shapes and all vertices of the shapes returned. It's really not any kind of performance bottleneck.

I have no idea what binary search would accomplish since that requires stuff to be already sorted.
Cunning, your method sounds much quicker and also sounds like it doesn't even use the inbuilt Raycasting functions. I think that's where I was going wrong...

I was using the RaycastOne function to sweep an area from the Shape's midpoint outwards. It wasn't strictly Binary search but a similar technique where I was halving the search area each time to find the closest point that the raycast falls upon.
It works but it's much faster to do some trig on the shapes instead.
my much simpler solution to explosions:

randomly spawn a lot of small circle bodies that die after a few seconds and fire them out in random directions from the explosion, you can even render them as particles to get a nice smoke effect
I also wrote in a routine that figures out the outline of the explosion for drawing purposes but I still have to tweak that... right now I'm trying to come up with a way to use this information to make some nice looking effects but this may prove to be harder than it sounds.

It should definitely be possible to use this for lighting effects. Where objects block forces from going through them, they can block light as well so the application is obvious.

Cunning, your method sounds much quicker and also sounds like it doesn't even use the inbuilt Raycasting functions.

The initial part of determining max/min angles for each shape does not use raycasting. However I do have a step after that of raycasting 2n times for n shapes found, at the midpoints of the sorted boundary list (the one with the max/min angles) to determine which arc sections "belong" to which body because there is no other way to determine occlusion. Then, adjacent sections involving the same body are merged together into another list, and the final force application rays are applied. Since the body is already known, the force application raycasts are done using TestSegment on the appropriate b2body and this should be relatively quick. The actual distance from the origin location to the "point of contact" is not calculated until this final step, and when it is done it is done a number of times (as defined by various parameters that can be set) for each object affected in order to capture the objects' shape.

thesleepless wrote:my much simpler solution to explosions:

randomly spawn a lot of small circle bodies that die after a few seconds and fire them out in random directions from the explosion, you can even render them as particles to get a nice smoke effect

This can be nice and I have actually done things like this in the past, but it's only really feasible if you're not doing tons of explosions all at once. Explosions are things that don't need full blown rigid body dynamics to simulate.

I'll post the code once I'm reasonably sure there's no bugs.
only one thing to say:
we don't need to simulate the perfect explosion because that would be slow.
I think that targeting the force to the center of the closest edge of a polygon should look cool enough.
Maybe someone can check this as I'm too lazy.. 🙁
snake5 wrote:only one thing to say:
we don't need to simulate the perfect explosion because that would be slow.
I think that targeting the force to the center of the closest edge of a polygon should look cool enough.
Maybe someone can check this as I'm too lazy.. 🙁

Yes, there are a lot of simple approximations that you can make, but I'm pretty sure earlier in this thread we went into the various ways these approaches all break down, and for most of the approximations there are some edge cases where you actually miss out on the important dynamics of the explosion.

I'm guessing that dc443 really wants to build a good part of his game mechanic around this, so it makes a lot of sense to fight through the difficulties and do it absolutely right.

As far as speed goes, I think his approach will do just fine as long as there aren't too many explosions happening at once and the ranges are capped at reasonable values; you'd be surprised how much complexity you can get away with when you're calculating one-time events, it's usually the every frame calculations that cause noticeable lags (for instance, even if calculating an explosion reduces your frame rate to half its original value, the explosion only happens once, and is likely accompanied by a lot of graphical stuff that will make a lagged frame or two almost imperceptible).

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