Advertisement
dermetfan

in libgdx-utils if concave PolygonShapes existed

Nov 12th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. /** @param concave the concave PolygonShape to triangulate
  2.      *  @return an array of PolygonShape triangles describing the given concave PolygonShape
  3.      *  @see EarClippingTriangulator#computeTriangles(float[]) */
  4.     public static PolygonShape[] triangulate(PolygonShape concave) {
  5.         tmpVecArr = vertices(concave);
  6.         ShortArray indices = new EarClippingTriangulator().computeTriangles(toFloatArray(tmpVecArr));
  7.         PolygonShape[] convexes = new PolygonShape[indices.size / 6];
  8.         Vector2[] tmpVecArr2 = new Vector2[3];
  9.         for(int i = 0, ii = -1; i < convexes.length; i++) {
  10.             convexes[i] = new PolygonShape();
  11.             tmpVecArr2[0] = tmpVecArr[indices.get(++ii)];
  12.             tmpVecArr2[1] = tmpVecArr[indices.get(++ii)];
  13.             tmpVecArr2[2] = tmpVecArr[indices.get(++ii)];
  14.             convexes[i].set(tmpVecArr2);
  15.         }
  16.         return convexes;
  17.     }
  18.  
  19.     /** @param concave the concave PolygonShape to decompose
  20.      *  @return an array of PolygonShapes describing the given concave PolygonShape
  21.      *  @see BayazitDecomposer#convexPartition(Array) */
  22.     public static PolygonShape[] decomposeIntoConvex(PolygonShape concave) {
  23.         Array<Array<Vector2>> convexPolys = BayazitDecomposer.convexPartition(new Array<Vector2>(vertices(concave)));
  24.         PolygonShape[] convexPolygons = new PolygonShape[convexPolys.size];
  25.         for(int i = 0; i < convexPolygons.length; i++) {
  26.             convexPolygons[i] = new PolygonShape();
  27.             convexPolygons[i].set((Vector2[]) convexPolys.get(i).toArray(Vector2.class));
  28.         }
  29.         return convexPolygons;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement