Convex shape decomposition

crazy-chris wrote:
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?


You've already figured out how to position the rectangles - I think positioning the circles will be easier than that. You just have to set the center of the circle at the vertex of the shape (the end-points of the rectangles). The radius would be half the width of the rectangles. I can't remember the exact syntax but a quick look at b2CircleDef should make it pretty obvious.
Okay, that approach is kind of working -- almost 🙂 needs some detail adjustments...
Image Image

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
Of course the point is valid: with the rectangle method we get a lot of shapes. Let's take this example from avocado-cad
Image

- With the triangle decomposition it's 7 polygon shapes (mostly triangles)
- With the rects it's 12 rectangles (and 12 circles = 24 shapes)

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
Here's some old fat line drawing function of mine. You could use it to calculate less shapes. I think the beveled left and right turns are actually convex polys too, so you should be able to get your calculations down to 1 shape per segment. I think I found that edge beveling algo originally from libart. The reason the loop goes from 0 to n+1 is that in order to create polygons you need the previous left and right point too. Also, precalculating the edge directions may be faster as each segment is normalize twice now.

On the other hand, this is really a case where you should use capsules. They should not be too hard to implement and should be way faster for collision detection than polygons.

Code:
static float Normalize(float& x, float& y)
{
   float d = sqrtf(x*x + y*y);
   if (d > 0.000001f)
   {
      x /= d;
      y /= d;
   }
   return d;
}

inline unsigned prev(unsigned i, unsigned n) { return (i+n-1) % n; }
inline unsigned next(unsigned i, unsigned n) { return (i+1) % n; }

static void DrawFatLine(const float* pts, unsigned npts, float width)
{
   const float   MITER_LIMIT = 1.20f;
   const float w = width/2;
   float plx, ply, prx, pry, d;
      
   glBegin(GL_LINES);
   for (unsigned i = 0; i < npts+1; ++i)
   {
      const float* v0 = pts + prev(i,npts)*2;
      const float* v1 = pts + (i%npts)*2;
      const float* v2 = pts + next(i,npts)*2;

      // Calculate direction and normal of both segments.
      float dx0 = v1[0] - v0[0]; float dy0 = v1[1] - v0[1];
      float dx1 = v2[0] - v1[0]; float dy1 = v2[1] - v1[1];
      // Normalize directions
      d = sqrtf(dx0*dx0 + dy0*dy0);
      dx0 /= d; dy0 /= d;
      d = sqrtf(dx1*dx1 + dy1*dy1);
      dx1 /= d; dy1 /= d;
      // Normals
      float nx0 = -dy0; float ny0 = dx0;
      float nx1 = -dy1; float ny1 = dx1;
      
      // Blended middle normal.
      float nmx = (nx0 + nx1) * 0.5f; float nmy = (ny0 + ny1) * 0.5f;
      float nmr2 = nmx*nmx + nmy*nmy;

      bool   bevel = false;
      if (nmr2 * MITER_LIMIT*MITER_LIMIT < 1.0f)
         bevel = true;

      if (nmr2 > 0.000001f)
      {
         float   scale = 1.0f / nmr2;
         // Limit the scale on really acute angles so
         // that the inner vertex does not explode.
         if (scale > 20.0f) scale = 20.0f;
         nmx *= scale; nmy *= scale;
      }
      
      if (bevel)
      {
         float   cross = dx1 * dy0 - dx0 * dy1;
         if (cross < 0)
         {
            // Turn left
            float lx = v1[0] + nmx * w; float ly = v1[1] + nmy * w;
            float rx0 = v1[0] - nx0 * w; float ry0 = v1[1] - ny0 * w;
            float rx1 = v1[0] - nx1 * w; float ry1 = v1[1] - ny1 * w;
            if (i > 0)
            {
               glVertex2f(plx, ply);
               glVertex2f(lx, ly);
               glVertex2f(prx, pry);
               glVertex2f(rx0, ry0);
               glVertex2f(rx0, ry0);
               glVertex2f(rx1, ry1);
            }
            plx = lx; ply = ly;
            prx = rx1; pry = ry1;
         }
         else
         {
            // Turn right
            float lx0 = v1[0] + nx0 * w; float ly0 = v1[1] + ny0 * w;
            float lx1 = v1[0] + nx1 * w; float ly1 = v1[1] + ny1 * w;
            float rx = v1[0] - nmx * w; float ry = v1[1] - nmy * w;
            if (i > 0)
            {
               glVertex2f(plx, ply);
               glVertex2f(lx0, ly0);
               glVertex2f(lx0, ly0);
               glVertex2f(lx1, ly1);
               glVertex2f(prx, pry);
               glVertex2f(rx, ry);
            }
            plx = lx1; ply = ly1;
            prx = rx; pry = ry;
         }
      }
      else
      {
         float lx = v1[0] + nmx * w; float ly = v1[1] + nmy * w;
         float rx = v1[0] - nmx * w; float ry = v1[1] - nmx * w;
         glVertex2f(plx, ply);
         glVertex2f(lx, ly);
         glVertex2f(prx, pry);
         glVertex2f(rx, ry);
         plx = lx; ply = ly;
         prx = rx; pry = ry;
      }
   }
   glEnd();
}
I just found this thread and judging by the number of posts, the convex decomposition method described here is probably way more advanced than what I have implemented.

At any rate, You can download my implementation here. Hit 'h' to turn on the debug display so you can see the convex decomposition. You can use the mouse to click and create holes in the terrain and see how the decomposition changes. It should be able to handle anything, but if you can get it to crash, definately let me know.
ewjordan,

How is your development on this module going?
I've started a new thread specifically for polygon tessellation and triangulation, which might be the most effective way to create non-overlapping convex polygons out of a concave one. I hope we can find / develop some good algorithms doing that. Please have a look, and maybe help with your experience and ideas!

Thread: Concave Polygon Tessellation (-> Convex)

[EDIT]I have seen ewjordan has been implementing the ear-clipping method. I would also love to hear updates about this and where the problems lie. Thanks! 🙂 [/EDIT]
Just to be clear, Box2D has no problem with overlapping convex shapes on the same body. So you could make a five pointed star out of 3 overlapping triangles.

However, it is probably best if edges don't overlap because this can cause the number of contact points to double. We could add some contact point culling code to handle that case.
Quote:
I have seen ewjordan has been implementing the ear-clipping method. I would also love to hear updates about this and where the problems lie. Thanks!


Posted a bit on that thread. The quick version?

The geometry itself works fine. The problem is picking an algorithm that usually outputs results that satisfy the geometrical restrictions coming from Box2d, and result in a minimal amount of discarded geometry when these can't be satisfied.

Speeding up the algorithms is pretty easy (as-is, a triangulation of most reasonable shapes will not take more than a split second), but the quality issues should be dealt with first.

Also, I can't find any info anywhere on constructing triangulations with added vertices (most Delaunay triangulation explanations assume the entire vertex set is given). I think this could help the quality of results considerably, so any references would be great!

Erin, I might as well ask you this: is there any possible way that we could loosen that "distance to centroid needs to be greater than b2_toiSlop" restriction, say by collapsing the core shape to the centroid instead of past it, or something like that? That's the main restriction that seems to cause problems during decomposition; would CCD work right with degenerate core shape vertices like that, or would it require too much special handling? (I would ideally see CCD as degenerating into a simple centroid raycast when the full shape is too small)
I think CCD could be extended to have per shape toiSlop. This would allow for a zero value on one shape. However, CCD wouldn't work if the sum was zero. So you probably should only have zero slop on static shapes.

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