0
\$\begingroup\$

Currently working on my first game, though running into some problems with libgx and screen aspect ratio.

What I have is a Stage which contains things like menu buttons etc, and the rest of the game is pretty much sprites being drawn with via SpriteBatch. To avoid having multiple SpriteBatches and cameras, I have re-used the ones that are created when Stage is created.

    stage = new Stage(WIDTH, HEIGHT, true); // keep aspect ratio
    batch = stage.getSpriteBatch();
    camera = (OrthographicCamera) stage.getCamera();
    // move camera so 'active' screen is centred
    stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0);

Anything that is Stage/Actor related is drawn fine - all goes within the aspect ratio adjusted boundaries.

The problem I'm having is anything that drawn via SpriteBatch, seems to ignore this viewport that is defined by Stage and can be visible outside of the Stage area.

batch.begin();
...
sirWuffles.draw(batch);
...
batch.end();

For example, in the above, if Sir Wuffles is generated outside of the defined WIDTH/HEIGHT it might still appear in the "gutters" of the screen.

Tried to explain it in the below screenshot. It's an exaggerated screen ratio to make the gutters large. I've also covered most of the gutter area in the blue/cyan rectangle so they are very obvious.

screenshot

Does anyone know what is happening? and how to fix it?

Currently, my "fix" is to use ShapeRenderer to draw rectangles that correspond to the gutters on top of the sprites...

\$\endgroup\$
1
  • \$\begingroup\$ Could you show us the code that you use to set position of your sprites, because 0,0 is not the bottom left corner of your stage but of your screen. \$\endgroup\$
    – Ion Farima
    Jan 13, 2015 at 15:33

2 Answers 2

0
\$\begingroup\$

You need to manually apply the stage's projection, defined by the camera, to the batch before rendering as such:

batch.setProjectionMatrix(stage.getCamera().combined);

Also remember to call camera.update() after you change the camera's position, which will update the projection matrix.

You gotta think about the SpriteBatch as its own separate entity for Stage, in that you shouldn't expect anything you do to the Stage to have an effect on the SpriteBatch when used in another context, such as manual batch rendering in your case.

\$\endgroup\$
1
  • \$\begingroup\$ Initially had this and gave it another go. Still no good. Sprites are still being drawn "off camera" :( \$\endgroup\$
    – pyko
    Nov 23, 2013 at 0:10
0
\$\begingroup\$
public void resize(int width, int height) {

     Vector2 size = Scaling.fit.apply(FIXED_STAGE_WIDTH,FIXED_STAGE_HEIGHT, width, height);
        int viewportX = (int)(width - size.x) / 2;
        int viewportY = (int)(height - size.y) / 2;
        int viewportWidth = (int)size.x;
        int viewportHeight = (int)size.y;
        Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
        stage.setViewport(FIXED_STAGE_WIDTH, FIXED_STAGE_HEIGHT, true, viewportX, viewportY, viewportWidth, viewportHeight);
}

This should do the trick for you. Here the drawing outside the stage is not possible.

\$\endgroup\$
1
  • 6
    \$\begingroup\$ This answer could be improved with an explanation of why that works. \$\endgroup\$
    – Anko
    Mar 19, 2014 at 9:40

You must log in to answer this question.

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