/** * Separates a non-convex polygon into convex polygons and adds them as fixtures to the <code>body</code> parameter.<br/> * There are some rules you should follow (otherwise you might get unexpected results) : * <ul> * <li>This class is specifically for non-convex polygons. If you want to create a convex polygon, you don't need to use this class - Box2D's <code>b2PolygonShape</code> class allows you to create convex shapes with the <code>setAsArray()</code>/<code>setAsVector()</code> method.</li> * <li>The vertices must be in clockwise order.</li> * <li>No three neighbouring points should lie on the same line segment.</li> * <li>There must be no overlapping segments and no "holes".</li> * </ul> <p/> * @param body The b2Body, in which the new fixtures will be stored. * @param fixtureDef A b2FixtureDef, containing all the properties (friction, density, etc.) which the new fixtures will inherit. * @param verticesVec The vertices of the non-convex polygon, in clockwise order. * @param scale <code>[optional]</code> The scale which you use to draw shapes in Box2D. The bigger the scale, the better the precision. The default value is 30. * @see b2PolygonShape * @see b2PolygonShape.SetAsArray() * @see b2PolygonShape.SetAsVector() * @see b2Fixture * */ public b2Fixture[] Separate(b2Body body, b2FixtureDef fixtureDef, b2Vec2[] verticesVec, float scale = 100, float offsetX = 0, float offsetY = 0) { int i; int n = verticesVec.Length; int j; int m; List <b2Vec2> vec = new List <b2Vec2>(); ArrayList figsVec; b2PolygonShape polyShape; for (i = 0; i < n; i++) { vec.Add(new b2Vec2((verticesVec[i].x + offsetX) * scale, (verticesVec[i].y + offsetY) * scale)); } if (!getIsConvexPolygon(vec, vec.Count)) { int validate = Validate(vec); if (validate == 2 || validate == 3) { vec.Reverse(); //add by kingBook 2017.2.14 } } //Debug.Log(string.Format("Validate:{0}",Validate(vec))); figsVec = calcShapes(vec); n = figsVec.Count; b2Fixture[] fixtures = new b2Fixture[n]; for (i = 0; i < n; i++) { vec = (List <b2Vec2>)figsVec[i]; vec.Reverse();//add by kingBook 2017.2.14 m = vec.Count; verticesVec = new b2Vec2[m]; for (j = 0; j < m; j++) { verticesVec[j] = new b2Vec2(vec[j].x / scale, vec[j].y / scale); } polyShape = new b2PolygonShape(); polyShape.SetAsArray(verticesVec, m); fixtureDef.shape = polyShape; fixtures[i] = body.CreateFixture(fixtureDef); } return(fixtures); }