I was sitting around for a bit today trying to think of a way to make realistic shattering/breaking/crumbling objects using Box2d. I came up with the following idea; not sure how well it would work, so I present it for advice/impressions:
1) If contact between two breakable bodies passes a certain "hard impact" threshold, continue; else, don't bother.
2) Pick out the two colliding bodies.
3) Using some stored material properties, come up with a pseudo-random, but somewhat "nice" triangulation of each body. [triangles should all be roughly the same size, but not too regularly aligned, depending on your material properties - probably start with a regular triangle grid then deform it with some noise]
4) Create a new b2World.
5) Add all these triangles to the new world, and connect each triangle to its neighbors by some sort of joint.
6) Match the derived initial velocities from the original bodies.
7) Watch impulses on the joints - if they go too high, break them.
8) Simulate several tiny time steps in the new world (probably need to alter some settings for this to work right).
9) After sufficient time, check for islands of still-connected triangles and turn each one into a new polygon. Dump the newly created/used world.
10) Delete the old objects in the original world and replace them with the newly generated polygons.
I'm not sure if this has a reasonable enough chance of looking realistic and running without too much of a pause to be worth going through the effort of implementing. Thoughts? Better approaches?
You have the basic idea. We want to do some stress analysis on the shape. I think we can do this with a reduced overhead and possibly some simplifications.
On the subject body we collect all the contact and joint impulses. Then we install pseudo-joints between all the subshapes. With this we can perform a simplified stress analysis using a number of techniques. I'm guessing the problem will ususally be statically indeterminant, so an iterative technique like Gauss-Seidel would be useful. This can be optimized to be much faster than normal simulation.
This should be done outside of the physics step to avoid breaking the engine's data structures.
Also, breakables should be pre-broken (possibly with multiple permutations) to avoid the run-time hit.
Okay, I think that makes sense, you're really most concerned with the shock that travels through the object, so it's overkill to actually simulate the motions on such a tiny scale, better to just propagate the stress directly instantaneously. One thing - when you say it's good to pre-break the objects, are you suggesting to precompute a set of candidate breaks for each object and pick the one most similar to the actual impact that takes place, or just precompute the mesh you are going to break apart and handle the stress simulation on impact?
I'm curious, have you ever used such a method in practice? Can you actually achieve a decent variety of realistic breaking behaviors without going too nuts on mesh resolution?
Artists usually like to have some input on how the object breaks. For example, a block wall shouldn't break along angular lines. It may be necessary to change the art on the broken shapes to remap uv's and to show fine detail.
I did something really primitive as my first fooling around session with box2d. I added a cannon ball to the test framework to be launched from either side of the ground box. When it hit another body (and if that body isn't static), it would calculate the force of the contact (if that makes any sense). To do that, I added the velocity vectors of both bodies, scaled that by the combined mass of both bodies and took the length of the resulting vector.
What I really wanted to do was break the block into a number of polygons. Pick a random point in the middle of the block, a random number of "slices" and an angle for each slice. Or put the point at the site of contact with the cannon ball. Anyway, that was delving a bit too deep into box2d internals trying to get the geometry for the collided bodies so I just deleted the body and created a few "debris" bodies instead. These bodies had a velocity based on the cannon ball's velocity so it was kind of neat with little debris bodies flying around. I shot cannon balls at the pyramid until there were so many debris bodies, I hit more than 512 bodies and the program crashed.
Edit: Oh yeah, I had an amusing bug. Originally, the cannon ball could break debris bodies into my debris bodies. If the ball hit a debris body just right, there would be a brief "explosion" of bodies before I hit the max limit.
I've been thinking a bit more about the best way to do this sort of thing. My current thinking is along the lines Erin suggested, using a precomputed mask as an overlay to split the object - there could be several of these for different materials and the illusion of randomness. A good example would be just your usual spider-web window crack represented as a list of connected line segments (though also storing a lot of other good stuff, like connectivity and pairwise impulse propagation coefficients, computed from the shapes). For a material like glass, this would be used as a cookie-cutter on the polygon to be broken, centered at the impact point. For something like bricks, you would bind the overlay to the body itself, not the impact point, unless you also wanted to let the individual bricks shatter, which could, I suppose, also be done.
Most of the impact propagation could be handled off-line, since the geometrical details are all contained in the mask. The only special handling would be at the edges of the polygon where the mask pieces are not completely covered (the relative masses and connectivities might be altered). All that would be required would be to propagate the impulses through the list using the precomputed (vectorial) connections, checking the differences in impulse between neighbors against a breaking threshold, iterating a couple times. It's possible that multiple iterations could even be rolled into the precomputed connection weights so that the entire thing could be done in a single pass.
I'll probably give this a shot at some point, it's an effect that I would love to use - let you know if I get anything useful.