Building on some Delaunay Triangulation code by Paul Bourke, and using Eric Jordan (ewjordan)'s b2Polygon/b2Triangle classes, I've made a working method to decompose any 2-d polygon into convex components. The way that I've gone about doing it, it will work for complex, self-intersecting polygons, as well as polygons with an arbitrary number of holes, and even holes within holes (suspended solid areas inside holes -- think concentric circles).
Here is the method:
Currently it works perfectly except I do not have a good convex polygon triangle-merging method, and for certain bad shapes it'll still leave a bunch of small shapes not merged together.
One problem with this approach is that for each triangle that is split up along the middle, i.e., into a quadrilateral and a triangle, I automatically split the quadrilateral into two triangles. This was because my triangle merger only took triangles. The problem with this, however, is that it generates a large number of extra splits, and isn't good for performance.
I've found that there are certain types of concave polygons, which do not adapt well to this method because of the nature of the Delaunay triangulation.
A simple example:

This shape is fairly trivial and will not cause any problems on my method because all it has to do is split two triangles, because the delaunay triangulation is this:

However, if you have a shape that has perhaps 20 zig-zags, the triangulation might turn out having a triangle-edge crossing several of the polygon-edges, and this causes that one triangle to be split several times. The triangle is split into 3 sub-triangles with the first polygon-edge, and each subsequent polygon-edge can theoretically split ALL THREE of them, etc. This is exponential behavior, and although it would be fairly difficult to come up with a situation that calls for such a shape, it is very undesired. Generally, if you take your mouse and move it randomly while clicking, after about 30-40 vertices, the results will not be pretty.
Luckily there is a solution! I am certain that this aforementioned issue of there being too many triangle splits being done will not be a show-stopper for almost any implementation of box2d, but in the case that the triangulation turns out drastically different from the underlying polygon shape (which, once again, is quite rare and only occurs with the ugliest of polygons), we can split it up using this method described here: http://www.flipcode.com/archives/Merging_Polygons_And_Sub-Pixel_Gaps.shtml
This is an illustration of the algorithm:

For each concave vertex (notch, if you will) on the polygon, slice up one of the offending sides.
Avoiding unnecessary slicing, we'd only want to separate the polygon once at each notch:



This method should be very effective for splitting up the kinds of shapes that don't match the triangulation well (e.g. Long segments with many points close together). Shallowly concave shapes (which are ideal for the triangulation method), tends to cause it to make lots of unsightly cuts, so this should complement the triangulation-based method.
I have not discussed constrained delaunay triangulations yet, which involve changing the delaunay triangulation algorithm a bit to allow for segments to be included. If anyone can find some working code for that, I think I can simplify this much further. I'm pretty sure that a minimal-constrained-delaunay triangulator is perfect for this task.
Here is the method:
- Make delaunay triangulation out of the vertices of the polygon
- Find edges of the polygon for which there does not exist a triangle from the triangulation that shares an edge with it (in other words, a polygon edge that crosses some triangles)
- Split the triangles into 2 or 3 more triangles to allow for these edges
- Delete triangles which are not contained within the polygon (ray-cast test)
- Merge the triangles together into convex polygons
- Delete any bad polygons (too small, etc) and add them to b2Body
Currently it works perfectly except I do not have a good convex polygon triangle-merging method, and for certain bad shapes it'll still leave a bunch of small shapes not merged together.
One problem with this approach is that for each triangle that is split up along the middle, i.e., into a quadrilateral and a triangle, I automatically split the quadrilateral into two triangles. This was because my triangle merger only took triangles. The problem with this, however, is that it generates a large number of extra splits, and isn't good for performance.
I've found that there are certain types of concave polygons, which do not adapt well to this method because of the nature of the Delaunay triangulation.
A simple example:

This shape is fairly trivial and will not cause any problems on my method because all it has to do is split two triangles, because the delaunay triangulation is this:

However, if you have a shape that has perhaps 20 zig-zags, the triangulation might turn out having a triangle-edge crossing several of the polygon-edges, and this causes that one triangle to be split several times. The triangle is split into 3 sub-triangles with the first polygon-edge, and each subsequent polygon-edge can theoretically split ALL THREE of them, etc. This is exponential behavior, and although it would be fairly difficult to come up with a situation that calls for such a shape, it is very undesired. Generally, if you take your mouse and move it randomly while clicking, after about 30-40 vertices, the results will not be pretty.
Luckily there is a solution! I am certain that this aforementioned issue of there being too many triangle splits being done will not be a show-stopper for almost any implementation of box2d, but in the case that the triangulation turns out drastically different from the underlying polygon shape (which, once again, is quite rare and only occurs with the ugliest of polygons), we can split it up using this method described here: http://www.flipcode.com/archives/Merging_Polygons_And_Sub-Pixel_Gaps.shtml
This is an illustration of the algorithm:

For each concave vertex (notch, if you will) on the polygon, slice up one of the offending sides.
Avoiding unnecessary slicing, we'd only want to separate the polygon once at each notch:



This method should be very effective for splitting up the kinds of shapes that don't match the triangulation well (e.g. Long segments with many points close together). Shallowly concave shapes (which are ideal for the triangulation method), tends to cause it to make lots of unsightly cuts, so this should complement the triangulation-based method.
I have not discussed constrained delaunay triangulations yet, which involve changing the delaunay triangulation algorithm a bit to allow for segments to be included. If anyone can find some working code for that, I think I can simplify this much further. I'm pretty sure that a minimal-constrained-delaunay triangulator is perfect for this task.


