Libgdx挑剔渲染多边形形状

时间:2016-11-12 16:18:09

标签: java libgdx 2d polygons

我可能只是做错了,因为我还不熟悉Libgdx。

我想要做的是显示多个六边形(最好在我弄清楚如何均匀地绘制它们之后在网格中)。在下面的代码中,我尝试使用不同但接近的y值绘制彼此相邻的四个六边形。如您所见,4个六边形中只有2个被绘制。我该怎么做才能解决这个问题?

屏幕截图:http://i.imgur.com/w9QSucE.png?1

Hex#draw方法

public void draw(PolygonSpriteBatch batch) {
    // System.out.println(x + ", " + y);
    // some of the code was copied from another SO thread but I'm pretty sure I understand it
    // Creating the color filling (but textures would work the same way)
    Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
    pix.setColor(0xDEADBEFF); // DE is red, AD is green and BE is blue.
    pix.fill();
    textureSolid = new Texture(pix);
    PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid),
            new float[] {               // Four vertices
                    0, 0,               // Vertex 0        /2--4
                    30, -ROOT/2,        // Vertex 1       / |\ | \
                    30, ROOT/2,         // Vertex 2      0\ | \| /5
                    90, -ROOT/2,        // Vertex 3        \1--3/
                    90, ROOT/2,         // Vertex 4
                    120, 0
            }, new short[] {
            0, 1, 2,          // Two triangles using vertex indices.
            1, 3, 2,          // Take care of the counter-clockwise direction.
            3, 4, 2,
            3, 5, 4
    });
    poly = new PolygonSprite(polyReg);
    poly.setOrigin(0, 0);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.draw(polyReg, x, y + ROOT/2);
}

主类

public class Game extends ApplicationAdapter {

    private PolygonSpriteBatch polyBatch;
    private static Set<Hex> renderables = new HashSet<Hex>();

    @Override
    public void create() {
        polyBatch = new PolygonSpriteBatch();
        //new HexGrid(5, 2).generate();
        renderables.add(new Hex(0, 0));
        renderables.add(new Hex(120, 120));
        renderables.add(new Hex(240, 60));
        renderables.add(new Hex(360, -50));
    }

    @Override
    public void render() {
        polyBatch.begin();
        for (Hex hex : renderables) {
            hex.draw(polyBatch);
        }
        polyBatch.end();
    }

    @Override
    public void dispose() {
        polyBatch.dispose();
    }

    public static void addRenderable(Hex hex) {
        renderables.add(hex);
    }
}

如果还有其他问题,我应该补充一下,然后继续询问。正如我所说的,我是Libgdx的新手,但看起来非常好,而且我愿意了解它。

编辑:我希望我现在没有违反规则但是碰撞碰撞。

0 个答案:

没有答案