[Solved] Convex Decomposition Problem

This is a consistent and reproducible bug with the decomposedPoly method - for some shapes, the convex decomposition creates shapes that fail the area < b2_epsilon assert and report this error:


Assertion failed: (area > b2_epsilon), function ComputeCentroid, file ../Box2D/Collision/Shapes/b2PolygonShape.cpp, line 115.

[object AlchemyExit]
at Function/<anonymous>()
at Function/<anonymous>()
at Box2DAS.Collision.Shapes::b2PolygonShape$/Decompose()
at wck::BodyShape/decomposedPoly()
at MyShape/create()
at misc::Entity/ensureCreated()
at Function/<anonymous>()

This can be reproduced by adding an instance of MyShape to a world with the following code for MyShape:

Code:
package {
   
   import wck.BodyShape ;
   public class MyShape extends BodyShape {
      public override function create():void {
         super.create();
         var coords:Vector.<Number> = new Vector.<Number>();
         coords.push(0,0,
         -0.05166666582226753,0,
         -0.11166666448116302,0.02666666731238365,
         -0.14166666567325592,0.05249999836087227,
         -0.1941666603088379,0.1158333346247673,
         -0.2241666615009308,0.18000000715255737,
         -0.23916666209697723,0.2316666692495346,
         -0.25,0.42250001430511475,
         -0.21250000596046448,0.4333333373069763,
         -0.13249999284744263,0.49000000953674316,
         -0.07999999821186066,0.5049999952316284,
         -0.03750000149011612,0.512499988079071,
         0.007499999832361937,0.5083333253860474,
         0.03583333268761635,0.4749999940395355,
         0.05833333358168602,0.4449999928474426,
         0.07333333045244217,0.4333333373069763,
         0.10000000149011612,0.36666667461395264,
         0.10750000178813934,0.33666667342185974,
         0.11166666448116302,0.15333333611488342,
         0.09666666388511658,0.07083333283662796,
         0.05999999865889549,0.02666666731238365,
         0.034166667610406876,0.011666666716337204,
         0.028333334252238274,0,
         -0.012500000186264515,0);
         decomposedPoly(coords);
      }
   }
}


Sorry for the weird shape, it was sent to me by a friend. Any ideas about why this shape (and some other strange shapes) break the decomposed poly function?

EDIT: Even though the shape seems small, that is not the problem - it is actually a decent size since my world scale is set to 1, but you can confirm it's not the scale of the object by just scaling it up. Adding this line before the decomposedPoly call changes the scale:

Code:
for(var i:uint=0; i<coords.length; i++) coords[i] *= 1000.0;
If I remember correctly, vertices have to be oriented clockwise. Try that or you can use:
b2PolygonShape: CheckVertexDirection and EnsureCorrectVertexDirection to make sure that isn't the issue.
Thanks for the suggestion. That is definitely the case for creating a single convex polygon - but since it is breaking the concave shape into several convex polygons, I figured the convex polygons it generated would be oriented the right way. I tried using EnsureCorrectVertexDirection with this code:

Code:
         var vertices:Vector.<V2> = new Vector.<V2>(); vertices.push(new V2(0,0),
         new V2(-0.05166666582226753,0),
         new V2(-0.11166666448116302,0.02666666731238365),
         new V2(-0.14166666567325592,0.05249999836087227),
         new V2(-0.1941666603088379,0.1158333346247673),
         new V2(-0.2241666615009308,0.18000000715255737),
         new V2(-0.23916666209697723,0.2316666692495346),
         new V2(-0.25,0.42250001430511475),
         new V2(-0.21250000596046448,0.4333333373069763),
         new V2(-0.13249999284744263,0.49000000953674316),
         new V2(-0.07999999821186066,0.5049999952316284),
         new V2(-0.03750000149011612,0.512499988079071),
         new V2(0.007499999832361937,0.5083333253860474),
         new V2(0.03583333268761635,0.4749999940395355),
         new V2(0.05833333358168602,0.4449999928474426),
         new V2(0.07333333045244217,0.4333333373069763),
         new V2(0.10000000149011612,0.36666667461395264),
         new V2(0.10750000178813934,0.33666667342185974),
         new V2(0.11166666448116302,0.15333333611488342),
         new V2(0.09666666388511658,0.07083333283662796),
         new V2(0.05999999865889549,0.02666666731238365),
         new V2(0.034166667610406876,0.011666666716337204),
         new V2(0.028333334252238274,0),
         new V2(-0.012500000186264515,0));
         b2PolygonShape.EnsureCorrectVertexDirection(vertices);
         var coords:Vector.<Number> = new Vector.<Number>();
         for(var i:uint=0; i<vertices.length; i++) coords.push(vertices[i].x,vertices[i].y);
         decomposedPoly(coords);


But the it caused the same problem - it is probably either creating counter-clockwise convex shapes or zero area convex shapes, regardless of the order of the points I send it.
I'm still a beginner at WCK so this may not be exactly right. Looking at CheckVertexDirection seem to check for "all" vertices but doesn't do individual vertex check.

Actually, your polygon has a slight overlap. If you look at :
(0,0) > (-0.05166666582226753,0) will overlap the line: (0.028333334252238274,0) > (-0.012500000186264515,0). Probably that's why the error occured 😉

If you remove (0,0) then decompose works fine. You can also remove (-0.012500000186264515,0), since the 4 vertices mentioned above form a straight line.
I don't really feel like fixing all the shapes manually... I guess I'll have to write some code that checks for overlapping lines and colinear points. But it seems like that will fix the problem - thanks for the help!
You're welcome 🙂

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