PolygonRegion missing vertice

Anything libgdx related goes here!

PolygonRegion missing vertice

Postby doctorevil » Sat Oct 19, 2013 10:16 pm

Hello.

I´m using the PolygonRegion class to draw several polygons for my game. Most of the polygons generate well, but in some cases there are polygons that have missing vertices.

For example, creating a polygon similar to a tetris piece with the following vertices as input:

[0, 0] [128, 0] [128, 128] [256, 128] [256, 256] [0, 256]

the resulting polygon has the [128, 128] vertice missing

Why is that happening?

Regards
Looking for a super talented 2D Artist to work with us!
doctorevil
 
Posts: 122
Joined: Tue Jan 29, 2013 8:32 pm

Re: PolygonRegion missing vertice

Postby NateS » Sun Oct 20, 2013 11:12 am

Can you provide an example showing the problem?
https://github.com/libgdx/libgdx/wiki/G ... ample_Code
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonRegion missing vertice

Postby doctorevil » Sun Oct 20, 2013 6:20 pm

Problem solved.

Turns out I was using an older version of the PolygonRegion class that makes the triangularization inside. The newest version seems to correct this problem, although it now requires to make the triangularization previous to the PolygonRegion class instantiation.

So the problem seems to have been on the triangularization method which somehow generated wrong triangle vertices, but it was corrected already in the latest nightly that I downloaded.

Thanks anyway

Regards
Looking for a super talented 2D Artist to work with us!
doctorevil
 
Posts: 122
Joined: Tue Jan 29, 2013 8:32 pm

Re: PolygonRegion missing vertice

Postby doctorevil » Mon Oct 21, 2013 1:25 am

mmmm looks like i´m still having some problems with the EarClippingTriangulator. I´m calling the computeTriangles method with the following set of vertices:

[3.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 2.0, 2.0, 3.0, 2.0]

and the method returns the folowing set of triangle indices:

2, 1, 0, 3, 2, 0, 0, 5, 4, 4, 3, 0]

Is it the polygon all right?. I read that the method doesn´t work with self-intersecting polygons, but this is not the case.

Regards.
Attachments
wrong polygon.png
This is the returning polygon
wrong polygon.png (2.39 KiB) Viewed 4882 times
correct polygon.png
This is the correct polygon
correct polygon.png (2.94 KiB) Viewed 4882 times
Looking for a super talented 2D Artist to work with us!
doctorevil
 
Posts: 122
Joined: Tue Jan 29, 2013 8:32 pm

Re: PolygonRegion missing vertice

Postby NateS » Mon Oct 21, 2013 2:35 pm

Seems fine to me:

Image

Code: Select all
public class EarClip extends ApplicationAdapter {
   private ShapeRenderer renderer;
   EarClippingTriangulator triangulator = new EarClippingTriangulator();

   public void create () {
      renderer = new ShapeRenderer();
      renderer.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      renderer.getTransformMatrix().translate(100, 100, 0);
   }

   public void render () {
      float[] points = { //
      300, 0, //
         200, 100, //
         100, 0, //
         0, 0, //
         200, 200, //
         300, 200};
      ShortArray triangles = triangulator.computeTriangles(points);
      System.out.println(triangles.size);

      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      renderer.begin(ShapeType.Line);

      renderer.setColor(Color.RED);
      for (int i = 0; i < triangles.size; i += 3) {
         int p1 = triangles.get(i) * 2;
         int p2 = triangles.get(i + 1) * 2;
         int p3 = triangles.get(i + 2) * 2;
         renderer.triangle( //
            points[p1], points[p1 + 1], //
            points[p2], points[p2 + 1], //
            points[p3], points[p3 + 1] //
            );
      }

      renderer.setColor(Color.GREEN);
      int last = points.length - 2;
      for (int i = 0; i < points.length; i += 2) {
         renderer.line(points[i], points[i + 1], points[last], points[last + 1]);
         last = i;
      }

      renderer.end();
   }

   public static void main (String[] args) throws Exception {
      new LwjglApplication(new EarClip());
   }
}
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonRegion missing vertice

Postby doctorevil » Mon Oct 21, 2013 5:09 pm

mmmm now that´s weird. I tested the exact vertices in a standalone class and returned the correct results:

Code: Select all
import com.badlogic.gdx.math.EarClippingTriangulator;
import com.badlogic.gdx.utils.ShortArray;

public class TestPolygonregion {
   
   public static void main(String[] args){
      float[] vertices = new float[]{3.0f, 0.0f, 2.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, 2.0f, 3.0f, 2.0f};
      EarClippingTriangulator triangulator = new EarClippingTriangulator();
      ShortArray result = triangulator.computeTriangles(vertices);
      System.out.println(result);
   }

}


and the return is [2, 1, 0, 3, 2, 0, 0, 5, 4, 4, 3, 0], which forms the correct triangles. But in the game class it returns the wrong triangles with THE EXACT SAME VERTICES. How can this be possible?

Here´s the piece of code that has problems:

Code: Select all
for (int i = 0; i < piecesJson.size(); i++){
         EarClippingTriangulator triangulator = new EarClippingTriangulator();
         JSONObject pieceJson = (JSONObject)piecesJson.get(i);
         JSONObject pieceColor = (JSONObject)pieceJson.get("color");
         JSONArray pieceVertices = (JSONArray)pieceJson.get("vertices");
         float r = Float.valueOf((String)pieceColor.get("r"));
         float g = Float.valueOf((String)pieceColor.get("g"));
         float b = Float.valueOf((String)pieceColor.get("b"));
         Piece piece = new Piece();
         piece.color = new Color(r, g, b, 1);
         FloatArray vertices = new FloatArray();
         FloatArray scaledVertices = new FloatArray();
         
         float origX = 1000;
         float origY = 1000;
         
         for (int j = 0; j < pieceVertices.size(); j++){
            JSONObject vertice = (JSONObject)pieceVertices.get(j);
            float verticeX = Float.valueOf((String)vertice.get("x"));
            float verticeY = Float.valueOf((String)vertice.get("y"));
            if (verticeX < origX)
               origX = verticeX;
            if (verticeY < origY)
               origY = verticeY;
         }
         
         for (int j = 0; j < pieceVertices.size(); j++){
            JSONObject vertice = (JSONObject)pieceVertices.get(j);
            float verticeX = Float.valueOf((String)vertice.get("x"));
            float verticeY = Float.valueOf((String)vertice.get("y"));
            vertices.add(verticeX - origX);
            vertices.add(verticeY - origY);
            scaledVertices.add((verticeX - origX) * 128);
            scaledVertices.add((verticeY - origY) * 128);
         }
         
         ShortArray scaledIndices = triangulator.computeTriangles(vertices);
         
         piece.virtualPolygon = new Polygon(vertices.toArray());
         piece.triangleIndices = scaledIndices.toArray();
         PolygonRegion polyReg = new PolygonRegion(polygonTextureRegion, scaledVertices.toArray(), scaledIndices.toArray());
         piece.onScreenPolygonSprite = new PolygonSprite(polyReg);
         piece.onScreenPolygonSprite.setOrigin(0, 0);
         piece.onScreenPolygonSprite.setColor(piece.color);
         piece.stillPolygonSprite = new PolygonSprite(polyReg);
         piece.stillPolygonSprite.setOrigin(0,  0);
         piece.stillPolygonSprite.setColor(piece.color);
         piece.status = PieceStatus.STILL;
         stageData.pieces[i] = piece;
      }


the code is very simple. It iterates over a JSON file to extract all the polygon information contained on it and create the game objects with this information.

The triangulation is called on each iteration.

Regards.
Looking for a super talented 2D Artist to work with us!
doctorevil
 
Posts: 122
Joined: Tue Jan 29, 2013 8:32 pm

Re: PolygonRegion missing vertice

Postby BurningHand » Mon Oct 21, 2013 5:27 pm

I actually saw similar behavior but assumed I was doing something wrong. It was only coming up wrong while I was active rendering a changing polygon per frame. As soon as I "committed" a polygon it triangulated properly. Never did find the real cause, but I worked around it by manually closing the polygon by duplication of the first vertex.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: PolygonRegion missing vertice

Postby NateS » Mon Oct 21, 2013 8:27 pm

Possibly EarClippingTriangulator has a bug where it keeps state from a previous triangulation. I'm afraid I need to reproduce it before I can fix it. :(
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: PolygonRegion missing vertice

Postby BurningHand » Mon Oct 21, 2013 8:39 pm

I have a class I can use to reliably reproduce with. I'll do some debugging to see what is going on and of I can't I'll whittle it down to let you take a stab.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: PolygonRegion missing vertice

Postby doctorevil » Tue Oct 22, 2013 12:36 am

Got an update of this issue. I tried changing the call to the computeTriangles method sending it an array of floats instead of a FloatArray class and it works fine now!.

So, the problem seems to be in the computeTriangles method that receives a FLoatArray class.

Regards.
Looking for a super talented 2D Artist to work with us!
doctorevil
 
Posts: 122
Joined: Tue Jan 29, 2013 8:32 pm

Next

Return to Libgdx

Who is online

Users browsing this forum: MSN [Bot] and 1 guest