Convex shape decomposition

Hm, no clue what that's about, at least from just looking at the numbers - I'll check it out today.
I know there is a way around it, I just started asking the poly if it was convex and if so not decomposing it, but I think there might be a problem with your function that merges the triangles into polys.
allanon wrote:
Okay this time I think I found a problem. I basically was blindly letting your algorithm handle all of our collision data, I never looked at it just got the verts in the poly and called DecomposePolygon on it. (I know its wastefull but .... meh). So today I passed it a completely convex shape and it returned 4 polys back to me. This is really only odd because we used to be using your old version and it worked fine on this very same shape. Anyways heres the vertex dump:
(snip)

Okay, I'm finally getting back to the decomposition code, and first step was looking at this example. Yeah, you're right, that's a little weird that it splits that polygon so much - the polygon does, in fact, need to be split (the max # of vertices per polygon is 8 by default), but 2 polygons would be optimal. It's also odd because convex figures used to work perfectly, as you noted. I traced the cause to the change in the triangulator, where I set it to always pick off the ear with the maximum minimum angle. Before, with a naive ear clip, a convex polygon is split like a fan; now it's hacked away at more haphazardly, so the polygonal reconstruction gets blocked at some point. There was another bug as well, in that when re-adding the triangles I have to run through a loop at least twice to get good results - when that's fixed, your convex polygon turns into 3, which is still not perfect.

In any case, the solution is simple - I can't get rid of that bias in the triangulator, because it was vital to get usable results in other cases, but I can just provide a hard coded method to decompose convex polygons (which just amounts to splitting them if there are too many points). That will be much faster, anyways.

Should be ready with the 2.0 update of this code in a few days, which will include a couple other improvements that should cut out a couple more problem cases and speed up TraceEdge a bit. Soon thereafter I'm going to try out a couple different triangulators, which should help results considerably - ear clipping is great for graphics, but is far less useful when you have volume and angle restrictions on the results.
Ah yes. I forgot that we turned up the number of verts / poly to 16. Though we've been using the check for convex-ness and just bypassing the decomposition stuff if its convex.
Hi,

I've also been playing around with decomposition and box2d, and found that attaching rectangles between the points works quite well. Here is a screenshot: Image

On drawing the polygon i maybe get 400 vertices. I reduce them first by distance (red line and dots), and then try to find straight lines by comparing the angles of the vectors. The code is in def reduce_poly_by_angle (http://trac2.assembla.com/elements/browser/elements/elements/tools_poly.py

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
i just encountered a problem with this approach, which is not unsolvable i bet 🙂

it leaves gaps between the rects:
Image

this ends up in that behaviour:
Image

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
I maybe have an idea...

(1) on creating: fill the gaps with triangle shapes, so that no strange collissions occur (edges)
(2) on drawing: on a change from counter-clockwise to clockwise (arrow on the image), take the outter line as reference, and add squares to the inside. change back on side change...
Image

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
If you put circles at each vertex they would fill in the gaps.
Right, that is the Crayon-Physics approach to doing shape decomposition, and it works quite well, though can result in a whole lot of shapes! If you can afford it, it's by far the simplest approach to this. There's a very raw implementation of this in the JBox2d testbed somewhere (http://www.jbox2d.org/v2demos/ - hit right arrow until you get to the "Shape Drawing Demo").

One thing I would suggest is to generate the rectangles so they are centered on the control points instead of on an edge, then you can use a circle at the control point as mayobutter suggested to fill the gap (or a triangle, though the problem there is that sometimes the generated triangles will be too small to pass through shape creation, and you'll get an assert due to either too small area or having an edge too close to the centroid).
thanks for the input! centering around the line appearently is a good step forward. now i would like to add circles to the body at the vertices, but don't know how to set the position for a b2CircleDef...

normally i would add a shape to the body with body.CreateShape(myShapeDef), but how could i do that with a circle and it's position?


----
Elements -- Box2D API for Python
http://elements.linuxuser.at

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