0
\$\begingroup\$

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

first example 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. second example 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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – Victor T.
    Commented Jun 3, 2017 at 17:23
  • \$\begingroup\$ 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? \$\endgroup\$
    – mrcdnk
    Commented Jun 3, 2017 at 18:53

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .