Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(2 edits)

Camera Zoom

I was a bit tired of only working on the drawings, thus I worked a feature that could be useful to grasp the levels structure : camera zoom-in/zoom-out.

I one of the first posts of this devlog, I detailed my camera class, MyCamera.java, that follows the hero and don't go outside of the level limits.

I thought that a zoom feature was missing :

Thus, I added these lines to the displacement() method of the MyCamera.java :

//Zoom-in/Zoom-out
if (Gdx.input.isKeyPressed(Input.Keys.O)) {
    viewportWidth *= 1.01f;
    viewportHeight *= 1.01f;
    zoomLimit();
}
else if (Gdx.input.isKeyPressed(Input.Keys.P)) {
    viewportWidth *= 0.99f;
    viewportHeight *= 0.99f;
    zoomLimit();
}

You can zoom out by pressing the "O" key, and zoom in by pressing the "P" key.

Why not "+" and "-" keys ?

There is a problem of mapping with the "+" and "-" of the numerical keypad. The code line

Input.Keys.PLUS

receives the input from the "+" of the numerical keypad , which is good. BUT, the code line

Input.Keys.MINUS

receives the input from the "-" above the "P" key, which is not good.

Therefore, I preferred to use 2 keys that are close from each other, "O" and "P".

Why do I modify viewportWidth and viewportHeight instead of uzing the zoom field of the camera ?

Because I am lazy. If I used the zoom field of the camera, I would have to take into account the zoom value in the code that I created at the beginning of the jam, and I didn't want waste time on that.

What the hell is that zoomLimit() method ?

Ho yeah, I almost forgot to talk about that method ! zoomlimit() is a method that... hu... limits the zoom. You can't zoom out of the level limits, and you can't zoom to closely, that wouldn't be playable.

And here is this zoomlimit() method :

public void zoomLimit(){
        if(viewportWidth > GameConstants.LEVEL_PIXEL_WIDTH){
            viewportWidth = GameConstants.LEVEL_PIXEL_WIDTH;
            viewportHeight = viewportWidth * GameConstants.SCREEN_RATIO;
        }
        else if(viewportWidth < GameConstants.SCREEN_WIDTH/2){
            viewportWidth = GameConstants.SCREEN_WIDTH/2;
            viewportHeight = viewportWidth * GameConstants.SCREEN_RATIO;
        }
        else if(viewportHeight > GameConstants.LEVEL_PIXEL_HEIGHT){
            viewportHeight = GameConstants.LEVEL_PIXEL_HEIGHT;
            viewportWidth = viewportHeight / GameConstants.SCREEN_RATIO;
        }
        else if(viewportHeight < GameConstants.SCREEN_HEIGHT/2){
            viewportHeight = GameConstants.SCREEN_HEIGHT/2;
            viewportWidth = viewportHeight / GameConstants.SCREEN_RATIO;
        }
    }

And that's it ! Now you can zoom in/out at your convenience !