1

I have 2 problems with PolygonSpriteBatch on libGDX.

1) I try to render a simple polygon (in this case a rectangle) and I only see a triangle when drawn.

Picture: http://i.snag.gy/kHV3N.jpg

        texture=new TextureRegion(new Texture("texture.png"));
        texture.getTexture().setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
        polygonRegion=new PolygonRegion(texture,createVertices(),createIndices(createVertices()));

The polygon that i want to draw is this one

 private float[] createVertices() {
   float[] verts = new float[5 * 2];
   int i = 0;
   verts[i++] = 2;
   verts[i++] = 2;
   verts[i++] = 2;
   verts[i++] = 3;
   verts[i++] = 3;
   verts[i++] = 3;
   verts[i++] = 3;
   verts[i++] = 2;
   verts[i++] = verts[0];
   verts[i++] = verts[1];
   return verts;

}

 private short[] createIndices(float[] vertices){
        short[] indices=new short[vertices.length/2];
        for(short i=0; i<vertices.length/2; i++){
            indices[i]=i;
        }
        return indices;
    }

And when I draw it

 shapeRenderer.setProjectionMatrix(camera.combined);
    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.polygon(createVertices());
    shapeRenderer.end();
    polygonSpriteBatch.setProjectionMatrix(camera.combined);
    polygonSpriteBatch.begin();
    polygonSpriteBatch.draw(polygonRegion, 0, 0);

    polygonSpriteBatch.end();

I also draw the plain texture to see how it looks like

 batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(texture, 4f, 2.4f, 1f, 1f);
    batch.end();

As you can see, the polygon is drawn with the shaperenderer correctly, but not well textured with the polygonspritebatch.

The second problem: Instead of the problem above, polygonspritebatch works perfectly when using a normal camera size, like 800x480. When I use a camera that is 8 x 4.8 (for box2d world) the texture is zoomed a lot ..like in the picture out there.

Any solutions?

1

For the first issue, the problem is the createIndices() function. The third argument to PolygonRegion needs to contain a number of indices that is a multiple of three (the first three vertices form a triangle, etc).

So you should return { 0, 1, 2, 1, 2, 3 }. Also, why do you have 5 vertices? It seems four are enough.

For the second issue, you should clarify your question. From the screenshot, everything seems fine.

|improve this answer|||||
  • I've changed the way to draw them anyways. Thanks. Mersi. – Boldijar Paul Sep 13 '14 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.