4
\$\begingroup\$

I'm working around a simple rectangle game, It just split the rectangle in to a lot of polygon debris. I use Pixmap and Texture to draw the edges of each polygon.

...
 for (GraphEdge e: _edges) {
    pixmap.drawLine((int) e.x1, (int) e.y1, (int) e.x2, (int) e.y2);
 }
...
@Override//In an actor class
public void draw(Batch batch, float parentAlpha) {
    region = new TextureRegion(new Texture(pixmap));
    region.setRegionWidth((int)width+1);
    region.setRegionHeight((int)height+1);
    batch.draw(region, this.getX(), this.getY());
}

it works well,but I want to add some pictures to adjust those polygons to make it looks better than some tedious lines, how should I implement it?

\$\endgroup\$
1
\$\begingroup\$

Consider using PolygonRegions, they're a way of mapping a TextureRegion onto a polygon. That way you can define the initial rectangle as a quad made up of two triangles, and then you can split that anyway you like across any number of fragments.

private Texture texture;
private PolygonRegion region;
private PolygonSpriteBatch batch;
private PolygonRegionDebugRenderer debugRenderer;

@Override
public void create () {
texture = new Texture(Gdx.files.internal("someimage.png"));

    // Define 4 vertices; (0, 0), (100, 100) (0, 100) and (100, 0)
    // Define 2 triangles; (0, 1, 2) and (0, 3, 2)
    // This will make up a quad
    region = new PolygonRegion(new TextureRegion(texture), new float[] {0, 0, 100, 100, 0, 100, 100, 0}, new short[] {0, 1, 2, 0, 3, 2});
    batch = new PolygonSpriteBatch();
    debugRenderer = new PolygonRegionDebugRenderer();
}


@Override
public void render () {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(0f, 0f, 0f, 1f);

    // Setup camera, set camera onto batch etc, etc.

    batch.begin();

    batch.draw(region, 0, 0, 256, 256);

    batch.end();

    debugRenderer.setProjectionMatrix(camera.combined);
    debugRenderer.draw(region, 0, 0, 0, 0, 256, 256, 1, 1, 0);
}

It even comes with a handy PolygonRegionDebugRenderer for easy trouble shooting. There's an example in the github repository showing how to use them in more detail.

\$\endgroup\$

You must log in to answer this question.

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