Professional Documents
Culture Documents
Page 31
Page 31
Box2D tutorials | Cocos2d-x resources | Box2D JSON loader | R.U.B.E Box2D editor | R.U.B.E free | My apps | Blog ! | 日本語はこちら | Box2D中文教程
FAQ / gotchas
Advanced topics
Conveyor belts
Projected trajectory
Sticky projectiles
Hovercar suspension
Top-down car physics
One-way walls
Buoyancy
Explosions
Physics-driven particles
Other topics
For this topic I did not get too technical with any formulas for how pressure (and therefore impulse) changes relative to the
distance from the blast point, but we know that the area of a circle increases relative to the square of the radius. An
https://www.iforce2d.net/b2dtut/explosions 1/6
9/4/2020 Explosions - Box2D tutorials - iforce2d
explosion causes a finite amount of gas to expand until the pressure with the surrounding air is equalized, which will be when
it takes up the same volume (area for 2d). So the pressure should decrease inversely to the square of the radius... okay
maybe I did get a little technical.
There are some other considerations to take into account, such as limiting the impulse to a maximum value etc, but these
have been left out here for clarity. Check the source code below for details.
Proximity method
The simplest approach is to find all bodies within a certain distance of the blast location. To define this a little more, we want
bodies with their center of mass within range. We can use an area query to efficiently find all bodies with a fixture in the area
around the blast point, and then do a simple distance check to see if their center of mass is close enough.
Now let's look at the results this gives us. Here is a screenshot showing which bodies in the test scene will be given an
impulse by this method:
This is a pretty good start, but there are a few problems. The most obvious is that the blast can travel through the platforms,
the ground, and other obstructing objects. Another problem is that the large body on the far left is in the blast range but it is
not affected because its center of mass is not close enough. How about this:
https://www.iforce2d.net/b2dtut/explosions 2/6
9/4/2020 Explosions - Box2D tutorials - iforce2d
The objects on each side of the explosion have the same mass, and they also present the same surface area to the blast, but
those on the right will collect four times as much impulse. One more:
These two frames are from just before and just after the blast impulse is applied. Because the impulse is applied at the center
of mass of the bodies, no torque is caused which looks odd. Still, a simple implementation like this would be fine for many
situations.
Raycast method
We can improve on this substantially by using raycasts to find which bodies to interact with instead of a simple distance
check. Check out the topic on ray casting and the related section in the world querying topic for implementation details.
Notice we divide the blast magnitude by the number of rays - this is just to make it easier to adjust the number of rays and
without changing the overall blast effect. Let's see how this goes in the test scene with 32 rays:
https://www.iforce2d.net/b2dtut/explosions 3/6
9/4/2020 Explosions - Box2D tutorials - iforce2d
Good, no magical blasting through solid walls anymore, so the boxes above the platforms are correctly not affected. The
second case is also much improved because the surface area of each side is taken into account:
Finally, because the impulses are being applied at non-symmetrical points where rays intersect the fixtures, they tend to
cause some torque to spin the bodies in a more interesting way.
The number of rays can be adjusted to allow the blast to reach between smaller gaps, and to balance CPU usage with the
quality of result.
Particle method
The last method we'll try in this topic is somewhat different to the first two. Instead of checking what bodies are around the
blast point, we'll just create a bunch of small bodies to simulate quickly expanding air particles, and let them fly. This is a
closer simulation of what actually happens in an explosion so we can expect it to look more realistic.
Not only does this method gives great results, but it's also easy to implement because most of the work is done by the
physics engine. On the other hand, more work done by the engine means more CPU time used. Here is a typical example of
how you might set up these particle bodies:
That's a lot of code just to create a body and add a fixture! But most of it is simply setting the necessary properties, because
unlike most situations we have seen so far, many of the default properties are not what we want. The comments there should
be self-explanatory... some things such as the friction and fixed rotation are not strictly necessary, but since there will be a lot
of these particles it may help to do anything we can to reduce the calculation required for each of them.
The effects of this are difficult to show in static screenshots, so here is an animated gif showing the trail left by each particle.
This shows 24 frames after the explosion, or about half a second:
https://www.iforce2d.net/b2dtut/explosions 4/6
9/4/2020 Explosions - Box2D tutorials - iforce2d
It's quite obvious that this method has all the advantages of the raycast method, plus a few more. Now we have a real blast
'wave' that reflects off obstacles, allowing the energy of the explosion to correctly get around corners, as in the case of the
underground tunnel, and the diagonal plate beside the upper platform. This scattering of energy helps give the impression
that the explosion is actually a physical space-occupying presence rather than the telekinetic impulse we have with the other
methods. It also gives us something to base rendering (smoke textures etc) on.
You can vary the weight of the particles, their initial velocity, linear damping, restitution, and of course the number of them,
to get just the right feel. The particles require some management to clear them after the explosion is finished, but it's not a
lot of work. The only real drawback to this method is the CPU time required. Mobile devices may struggle a little with multiple
explosions of a high number of particles, but for regular computers even 100 or more (128 in the screenshot below) is not a
problem (at least with C++).
Another nice side-effect is that the energy from the blast does not affect everything instantaneously - it takes a tiny bit
longer for the particles to reach objects that are further from the blast center. It's only a few milliseconds of difference but
those milliseconds do seem to add a touch of realism. For instance looking at the block stack immediately to the right of the
explosion center, we can see the blast work its way up the stack over a few timesteps.
Finally, this method ensures that in tight spaces the expected energy does not just disappear. The particles can bounce
around and hit many different surfaces before they dissipate.
https://www.iforce2d.net/b2dtut/explosions 5/6
9/4/2020 Explosions - Box2D tutorials - iforce2d
Source code
Here is the source code for those who would like to try it out for themselves. This is a 'test' for the testbed, based on Box2D
v2.3.0.
YouTube video
Not seeing the any comments below? Try changing the 'https' in the page URL to just 'http'
Click here: http://www.iforce2d.net/b2dtut/explosions
https://www.iforce2d.net/b2dtut/explosions 6/6