kevvex
kevvex

Reputation: 315

Scaling renderered shapes in libGDX using ShapeRenderer

I've made an Asteroids game and am having trouble with different screen sizes. Yes, I know about viewports but in my case I don't think that can work. I've tried to use it but my objects are not images. Instead they're rendered by a ShapeRenderer and therefore the viewports probably not affect them I guess. I've solved how to fit the text for most types of smartphone screens but not the rendered objects like the player and asteroids. On some screens they're perfect size and other screens either too small or too big.

Is there a way to scale rendered (ShapeRenderer) types in libGDX so that they will fit the screen size / resolution? Or must I try to implement a class for common resolutions that can be used?

Here is two pictures that illustrates the problem. The text in the images are aldready fixed by the way.

Too large game objects

Too small game objects

EDIT:

It does work to create and apply the viewport for the ShapeRenderer now, but I'm still having the same problem. The game objects inside the viewport aren't scaled to fit the current screen. I need a way to scale down the game objects s using a viewport (if possible). The way the game is going to be implemented is that the size of the smartphone screen is the game world size. Therefore, there are little space to move the ship if the screen is pretty small. I want the objects to be moving around in an area that fits the viewport size. The game should not get more difficult or easier depending on the screen size.

Example on left: The game on a large smartphone screen

Example on right: The ShapeRenderer viewport is working

enter image description here enter image description here

Upvotes: 1

Views: 563

Answers (1)

dfour
dfour

Reputation: 1376

Viewports were created in order to reduce the effort needed to render on different sized screens. So are ideal for situations like this.

In order to use a Viewport with a ShapeRenerer you simply need to set the ShapeRenderer's projection matrix to the one in the viewport's camera.

shapeRenderer.setProjectionMatrix(veiwport.getCamera().combined);

This means the shapeRenderer will render the way the viewport is configured.

Upvotes: 1

Related Questions