Search code examples
javalibgdx

LibGDX square stretches with resolution


I was under the assumption ShapeRenderer.rect draws in pixels but it stretches the square when I stretch the resolution and turns it into a rectangle.

        shapeRenderer.identity();
        shapeRenderer.begin(ShapeType.Filled);
        shapeRenderer.setColor(Color.BLUE);
        shapeRenderer.rect(0, 0, 100, 100);
        shapeRenderer.end();

Solution

  • I assume that you are using Orthographic Camera

    If you write

    camera = new OrthographicCamera(constant_Width, constant_Height);
    

    That will cause your rectangle to get stretched one method to solve this is to use ratio

    camera = new Orthographic Camera(Gdx.graphics.getWidth/Ratio, Gdx.graphics.getHeight/Ratio);
    

    This should solve your problem. Enjoy :)

    EDIT:

    How could I have forgotten this part

    after you declare your camera you need to do this

    camera.translate(Gdx.graphics.getWidth/Ratio/2, Gdx.graphics.getHeight/Ratio/2)
    

    If you miss that line, you (0, 0) will be at the center of your screen. I assume that you want your (0, 0) to be the bottom left corner of your screen.