0
\$\begingroup\$

I am working on making a falling sand simulation using libgdx. For drawing the individual elements I am using a ShapeRenderer and drawing rectangles.

I have begun to implement a dropdown menu to allow selection of elements to place. The functionality of the dropdown menu is working. I am able to set the inputProcessor as the stage and upon selecting the element type set the inputProcessor back to CreatorInputProcessor and I am able to successfully place elements again. The functionality is all working as expected.

Drawing as expected

However, when I set the stage with the dropdown as inputProcessor, the element rectangles become stretched. The resolution and pixel width of the window stays the same. It is not zooming in or anything. When the inputProcessor is set back to the CreatorInputProcessor the rectangles stay stretched.

Stretched appearance

Here are the things I have tried to fix this issue:

  • Use every kind of ViewPort passing in both a new OrthographicCamera and also the same camera used for the game view
  • Disposing of the stage after input processor is set back to CreatorInputProcessor
  • Setting the projection matrix on the camera every frame just before drawing or after drawing.

I found this stack exchange question which is similar to my issue but the marked answer does not solve my problem (setting the projection matrix every frame): LibGDX - why are the shapes from my ShapeRenderer resizing with my viewport?

I am looking for some advice on how I can resolve this issue. Thanks!

Here is the relevant code snippet: https://github.com/DavidMcLaughlin208/FallingSandJava/blob/master/core/src/com/gdx/cellular/InputManager.java#L357-L399

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I still am not sure exactly what this issue was. But to fix this I moved the drawing of the elements into a separate stage and actor with a custom draw function:

public class MatrixActor extends Actor {

    private ShapeRenderer shapeRenderer;
    private CellularMatrix matrix;

    public MatrixActor(ShapeRenderer shapeRenderer, CellularMatrix matrix) {
        this.shapeRenderer = shapeRenderer;
        this.matrix = matrix;
    }

    @Override
    public void draw (Batch batch, float parentAlpha) {
        batch.end();
        shapeRenderer.setProjectionMatrix(getStage().getCamera().combined);
        matrix.drawAll(shapeRenderer);
        batch.begin();
    }
}

The key part was to end the batch then draw with the ShapeRenderer and then start the batch again.

I don't know why this resolved the issue but it has. There must have been something conflicting with the batch and the shaperenderer.

\$\endgroup\$

You must log in to answer this question.

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