1
votes
  • The basic question here is: how to ALWAYS keep your sprites within the Fitviewport? How to keep a reference to the view in order to have the proper coordinates as to where to draw?

I'm trying to spawn enemies into the gameplay screen. But this is handled by a FitViewport, and enemies and even the player can move outside the FitViewport on certain screen resolutions. So far the problem seems to be in the Y axis.

The FitViewport is made like this:

gameCamera = new OrthographicCamera();
gameCamera.setToOrtho(false);
gameViewport = new FitViewport(MyGame.WORLD_WIDTH,MyGame.WORLD_HEIGHT,gameCamera);
gameViewport.setScreenBounds(0,0,MyGame.WORLD_WIDTH,MyGame.WORLD_HEIGHT);

Then the camera position gets updated like this at the resize() method:

gameViewport.update(width,height);  //not used when using the virtual viewport in the render method.
gameCamera.position.set(player.position.x + 200,player.position.y, 0);

Then the update() method calls the Player's own update() method which includes these lines:

//POSITION UPDATE
if (this.position.x<0) this.position.x=0;
if (this.position.x>Gdx.graphics.getWidth() - width) this.position.x= Gdx.graphics.getWidth() - width;
if (this.position.y<0) this.position.y = 0;
if (this.position.y>PlayScreen.gameViewport.getScreenHeight() - height) this.position.y = PlayScreen.gameViewport.getScreenHeight()- height;

Notice for the X axis I'm still using Gdx.graphics dimensions because I'm yet to make it work with PlayScreen.gameViewport.getScreenHeight() (gameViewport has been set to static for this purpose).

Also on enemy spawn (the problem related here is that they spawn outside of the screen Y in terms of what I see) I have this code inside the update() method of the Screen implementing all these viewports:

//Alien Spawn
    if (System.currentTimeMillis() - lastZSpawn >= SpawnTimer){
        count++;
        lastZSpawn= System.currentTimeMillis();
        for (int i=0;i<count;i++){
            int x = Gdx.graphics.getWidth();
            int y = random.nextInt((int)gameViewport.getScreenHeight() - Alien.height);
            if (entities.size()<6){
               entities.add(new Alien(new Vector2(x,y),1, alienImages,(float)((0))));
            }
        }

    }

Also using gameViewport.getScreenHeight() here cause Gdx.graphics wasnt giving the correct result (it gave me the same issue really).

The render() method is correctly implemented in terms of the batch and applying the viewport:

MyGame.batch.setProjectionMatrix(gameCamera.combined);
gameViewport.apply();
MyGame.batch.begin();

for (int i = entities.size()-1; i>=0;i--){
    entities.get(i).render();
}
2

2 Answers

0
votes

You should never change the position of your player or enemies when resizing, that's why a viewport is for, remove all the code that do that first, to make your viewport work as you expected you need to create a new instance of camera passing the new viewport width and height when you resize, i prefer to make my camera static so i can acess its atribbutes from everywhere i want, you should do something like this:

public static OrthographicCamera update(int width,int height){
        instance = new OrthographicCamera(width, height);
        instance.setToOrtho(false);
        return instance;
    }
0
votes

The answer to my problem is posted by myself in another question of mine which was also driven by a confusion in the implementation of FitViewports and using WorldWidth and WorldHeight properties as coordinates of reference when drawing objects into the game, and also correctly setting the camera position taking these values into consideration aswell. The answer is here even though its text and not code and its mainly what i already wrote in this very post. FitViewport doesnt scale properly Libgdx