1
\$\begingroup\$

I am quite new to LibGDX. I am trying to make a game, where the character is an Actor, and it's controlled by a Touchpad(included in the main Stage as an Actor too).

My problem is that I am trying to put the stage's camera to be always centered on the character, but I have no idea of how to solve this, I've been searching and it seems I have to use 2 cameras, 1 to show the controllers (the touchpad), and another to show the actors and that stuff, so while the actors are moving, the touchpad will keep on the screen, but I do not really know how should I do this, my code is this:

public class GameScreen extends BaseScreen {

    Stage stage;
    Character character;//it extends from actor
    Touchpad joystick;
    int screenWidht, screenHeight;
    Skin joystickSkin;

    public GameScreen(AwesomeTanks game) {
        super(game);
        screenWidht = Gdx.graphics.getWidth();
        screenHeight = Gdx.graphics.getHeight();

        joystick = new Touchpad(0,getTouchPadStyle());
        joystick.setColor(joystick.getColor().r, joystick.getColor().g, joystick.getColor().b, 0.5f);
        joystick.setBounds(10, 10, screenHeight / 2.5f, screenHeight / 2.5f);

        character = new Character();

        stage = new Stage();
        stage.addActor(character);
        stage.addActor(joystick);

        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void hide() {
        stage.dispose();
        joystickSkin.dispose();
        character.detach();

    }

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

        character.moveBy(joystick.getKnobPercentX() * character.velocity * delta, joystick.getKnobPercentY() * character.velocity * delta);

        if(joystick.isTouched())
            character.setRotation((float) ((180/Math.PI)*Math.atan2(joystick.getKnobPercentY(),joystick.getKnobPercentX())));

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You need to create another "UI" Stage and call its act and draw methods in the render method. You also need to use a InputMultiplexer to process the input of both Stages.

The "UI" Stage should use a ScreenViewport as well, to support multiple screen sizes and densities.

More info about Viewports here:

\$\endgroup\$
0
\$\begingroup\$

You can make two view port and each time call batch.setProjectionMatrix and then draw textures in the camera you want

mainCamera = new OrthographicCamera(Constants.CAM_WIDTH / Constants.PPM, Constants.CAM_HEIGHT / Constants.PPM);
    mainViewport = new FitViewport(Constants.CAM_WIDTH / Constants.PPM, Constants.CAM_HEIGHT / Constants.PPM, mainCamera);
    mainCamera.position.set(Constants.CAM_WIDTH / (Constants.PPM * 2f), Constants.CAM_HEIGHT / (Constants.PPM * 2f), 0);
    mainCamera.update();
    secondCamera = new OrthographicCamera(Constants.CAM_WIDTH / Constants.PPM, Constants.CAM_HEIGHT / Constants.PPM);
    backgroundViewport = new FitViewport(Constants.CAM_WIDTH / Constants.PPM, Constants.CAM_HEIGHT / Constants.PPM, secondCamera);
    secondCamera.position.set(Constants.CAM_WIDTH / (Constants.PPM * 2f), Constants.CAM_HEIGHT / (Constants.PPM * 2f), 0);
    secondCamera.update();

And then in the render method use:

    //For the bacground cam
    batch.begin();
    batch.setProjectionMatrix(secondCamera.combined);
    backgroundManager.render(delta);
    //For the main cam
    batch.setProjectionMatrix(mainCamera.combined);
    platformManager.render(delta);
    doodleGuy.render(delta);
    batch.end();
\$\endgroup\$

You must log in to answer this question.

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