[Solved] Scaling a display with libGDX

I’ve been making a game for Android in my spare time for the past couple of weeks. It was going well until I realised I was only testing on one device. Upon creating an emulator with a smaller resolution than my physical device I noticed the game didn’t scale correctly and as a result the gameplay wasn’t the same. I stumbled upon this tutorial. I implemented it but it’s not working as desired, namely it only seems to use the top right corner of the screen. I’m not sure if I’ve implemented Gemserk’s solution incorrectly or it’s something else but frankly I think I just need a fresh set of eyes. The entirety of my main code can be found here in case I miss anything out and you can find Gemserk’s implementation in the previous link but I’ll go through what I think are the most important methods below:

public void create() {
		gameState = states.menu;
		multipleVirtualViewportBuilder = new MultipleVirtualViewportBuilder(800, 480, 1280, 1024);  
      VirtualViewport virtualViewport = multipleVirtualViewportBuilder.getVirtualViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());  
        
		camera = new OrthographicCameraWithVirtualViewport(virtualViewport);
		camera.position.set(0f, 0f, 0f);
		
		font = new BitmapFont();
		font.setColor(Color.WHITE);
        		
		spriteBatch = new SpriteBatch();
		
		playerTexture = new Texture(Gdx.files.internal("data/cube.png"));
		playerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		diamondTexture = new Texture(Gdx.files.internal("data/diamond.png"));
		diamondTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		player = new Player((int) (Gdx.graphics.getWidth()/2 - playerTexture.getWidth()), 0);		

		screenWidth = Gdx.graphics.getWidth();
		screenHeight = Gdx.graphics.getHeight();
		
		playerSprite = new Sprite(playerTexture);
		playerSprite.setOrigin(0, 0);
		playerSprite.setPosition(player.getPosX(), player.getPosY());
		playerSprite.scale(scale);
		
		playerWidth = (int) (playerSprite.getWidth() * scale);
		playerHeight = (int) (playerSprite.getHeight() * scale);
		
		Gdx.input.setInputProcessor(new GestureDetector(this));
		
	}

As you can see here I’m instantiating the virtual viewport as well as the camera. I have a feeling that the parameters I’m passing when creating a new MultipleVirtualViewportBuilder might have something to do with it but any experimenting I’m having has proven fruitless.


public void render() {
		camera.update();
		Gdx.gl.glClearColor(0.204f, 0.255f, 0.255f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		spriteBatch.setProjectionMatrix(camera.combined);
		spriteBatch.begin();

//Draw things

                spriteBatch.end();

All I’m really doing here is updating the camera and drawing the player so I’m fairly certain my issue is in the create() method. Either that or I’ve implemented it wrong altogether.

Anyone able to shed some light on this problem?