What causes undefined geometry to be drawn?

I am currently using the polygonSpriteBatch of libgdx to draw triangles, rectangles and a concave polygon as border. After adding the concave polygon for the colored borders everything seems to break and random geometry appears (sometimes even with a gradient).

Some Examples

Here you can see on the left everything looks good and after drawing the additional rectangle with its border and removing the other borders you can still see some weird red regions. This is even more extreme. On the left part of the concave polygon is drawn with a different color, and after it despawns (on the right) there is still part of it left and other triangles not created by me are drawn.

I am quite confident that all convex polygons are defined in counter-clockwise ordering.

In the past I had trouble with providing the correct TextureRegion and it still doesn't feel like my current implementation is how it should be, but it worked for simple rectangles and triangles. Currently I have a (3px)*(3px) Region and all polygon vertices are between (0f,0f) and (3f,3f).

Because I define all triangles of the polygons myself the short array given to the PolygonRegion for a rectangle looks like this: (0, 1, 2, 3, 4, 5) and the corresponding Vector2 polygon array like this: ((0.0, 3.0), (0.0, 0.0), (3.0, 0.0), (0.0, 3.0), (3.0, 0.0), (3.0, 3.0))

I experienced even more artifacts when executing this on android and for some reason drawing a small rectangle for each vertice (for debugging) fixed some of the artifacts above.

This is quite a complex problem but I hope somebody knows what could cause such a behaviour.

• This could be due to an index array that's not quite matched up with the vertex array. In the bottom example, the extra triangles seem to be using verts and colors from some of the "expected" triangles. Could there be extra indices left in the array for the polygons that are no longer being drawn? – Victor T. Jun 3 '17 at 17:23
• Yes, this seems to be possible I just checked the length of the polygon and triangle arrays I'm using and sometimes they have the same length and sometimes the triangle array has half the length of the polygon array. I'd assume that it should have half the length and not the same right? – mrcdnk Jun 3 '17 at 18:53

I fixed it, the problem was the missing /2 in this function:

public static short[] getDefaultTriangles(float[] polygon) {
short[] triangles = new short[polygon.length/2];
for (short i = 0; i < triangles.length; i++) {
triangles[i] = i;
}

return triangles;
}


Which resulted in my triangle arrays having the same length as my polygon arrays.