Figured I'd weigh in here, as I've done a bit of work on this (somewhere in the convex decomposition thread there's the C++ code, which actually mostly works, though the version there is not 2.0 ready - I have to upload that still).
There are standard algorithms for dealing with all this stuff, and they work okay. I've gotten triangulation + recombination into polygons working pretty well, and I will check that in to SVN once I take another pass at it (soon - I'm on vacation right now, but I'm getting back on Friday, so soon thereafter). But it's not ideal.
First problem: convex decomposition
This is easily achieved when starting from a simple polygon by 1) triangulating (ear clipping, Seidel's algo, some sort of Delaunay triangulation, etc.), and 2) recombining triangles into polygons (a bunch of different ways to do this). While it's easy to emit a mathematically legitimate set of convex polygons, it's a lot tougher to constrain these polygons to conform to Box2d's requirements, which include:
- Edges can't be too close to parallel
- Area must be greater than a minimum value
- Each edge must be more than b2_toiSlop away from the centroid
In particular, the third one causes a lot of problems, as a normal triangulation often creates thin slivers. Navigating around this stuff, in fact, turns out to be
much harder than just getting the decomposition to work right. Currently what I've been doing is 1) biasing the triangulator towards "nicer" triangles, at a slight speed hit, and 2) just throwing away polygons that don't satisfy all these requirements at the end of the process. This works, but it leaves geometry gaps in some cases, which is far from ideal.
I've asked around, and I can't find any algorithms that are designed with these types of restrictions in mind. Constrained conforming Delauney triangulations seem pretty close to what we want, but still end up producing some small vertices (plus, I have no idea how to add vertices - there seem to be no decent explanations online of the "standard" way to do this!). So it's probably worth thinking about what the best thing to do is when you come across a "bad" polygon. Lately I'm thinking that maybe the offending polygons should just be split along near-parallel angles and expanded away from the centroid until they satisfy all these restrictions, as at least this would not leave gaps.
What do you think? Is it better to have gaps, or cover areas that are supposed to be empty (and possibly double cover other areas)? Or should we just fail? (I don't like that option, because in many cases this utility will be used to sanitize user input)
The other subtlety that must be dealt with is how to handle sequential or non-sequential identical vertices (or at least near enough to identical so that precision issues cause trouble), which usually crash triangulators - in this case I split the polygon or merge the vertices, depending. This works well, but it makes the problem a lot messier bookkeeping-wise because you all of a sudden have no certainty when you enter triangulation as to how many triangles you'll end up with. If I get a chance to re-write everything, I'm going to do it with this in mind, as it's messy to wedge this in when you started with the n-2 triangles assumption.
Second problem: dealing with non-simple (i.e. self-intersecting) polygons
This is actually a bit more straightforward. The simplest algorithm (the one that I use currently) is to create a list of all the segment intersections and generate a graph with connections between these nodes, and then walk along the edge of the graph tracing out the outline, which will then be a simple polygon. So far I haven't seen any problems with this algorithm (once I worked out the kinks, esp. dealing with very close vertices and merging them and whatnot), other than the fact that it's not incredibly fast as written.
I can significantly improve the performance characteristics of all of these algorithms in my implementation. But it seems foolish to do so if the overall approach itself going to need reworking to adapt better to the type of output that we prefer for use in the physics engine.
So here's what I'd really love to find, that I think would really help the quality of the results: a good triangulation algorithm that automatically adds internal vertices to a non-convex polygon in a way that makes as many triangles as possible satisfy Box2d's requirements. Right now I only work with existing vertices, which I
think is a bad idea (I was about to switch from ear-clipping to Seidel's algorithm for speed purposes, but Seidel's algorithm still produces slivers, so the quality of results is not any better, and at least I can bias ear clipping towards "fat" triangles - this is harder with Seidel's algorithm). But I just can't find any info on doing this stuff - all the constrained Delaunay triangulation algorithm explanations seem to assume that the set of points is already set. If anyone knows anything about this stuff, please let me know!
Edit: For non-simple polygons there is a pretty good set of algorithms at
http://geometryalgorithms.com/Archive/a ... 0Algorithm that could be used to quickly split them into simple pieces. I was actually planning to switch my naive segment intersection algo to Bentley-Ottman, which has nice usual-case running time, so it might be convenient to start from that implementation. The implementation is offered under the following license:
// Copyright 2001, softSurfer (
http://www.softsurfer.com)
// This code may be freely used and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.
I'm a little nervous about this, because it seems that it
might obligate users of the library to include the copyright notice even if they don't distribute source - anyone have any thoughts on that? I'm no legal expert, the "included with
it" part might only mean that you have to include the notice with the code, not the compiled version...