Search code examples
libgdxbordershapes

Libgdx - how to zoom a rectangle/line (shaperenderer)


Let's say that I have an Actor which has a draw method:

@Override
public void draw(Batch batch, float parentAlpha) {
        super.draw(batch,parentAlpha);

        sr.begin(ShapeRenderer.ShapeType.Line);
        sr.setColor(Color.RED);
        sr.rectLine(getX(),getY(),getX() + getWidth(),getY(),3);
        sr.end();
}

As you can see, the actor also draws a red line below itself. Now, when I zoom the camera on a stage, where the actor is, he becomes bigger - it works but his red rectangle doesn't.

When I set debug() on the Actor, it shows the debug lines and zooms them aswell.

What I'm trying to achieve?

I need to zoom the line too, along the actor.

If there's anyone who knows how to achieve this, I would be grateful ;)


Solution

  • Shaperenderer is similar to batch. It has own coordinate matrix. So you need to set projection matrix on shaperenderer too.

    shaperenderer.setProjectionMatrix(camera.combined);
    

    It will probably work for you.

    But while shaperenderer drawing via vertexes, lines' width may not change even when you zoom in/out.

    You can change line width with openGL function.

    Gdx.gl.glLineWidth(width);
    

    And additionaly im suggesting not to use Shaperenderer because its quite expensive class and only recommended for debug drawings.