Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Struggling with Cameras and TileMaps in libGDX  (Read 956 times)
0 Members and 1 Guest are viewing this topic.
Offline phunni

Senior Newbie


Exp: 17 years



« Posted 2017-07-05 13:58:33 »

This is not my first game and I thought that I understood the way the camera was working.  It is, however, my first time making a game using TileMaps (created using Tiled in this case).  I did create a prototype before which works perfectly, but, for some reason, that code doesn't work in my new project.  As far as I can see, the only difference is that Tiled seems to force turning tilesets into a .tsx file whereas they were .png files before. I'm using libGDX.

So, here's my problem - using the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public class BaseScreen extends ScreenAdapter {

    public static final String TAG = "BaseScreen";

    protected Game game;

    protected OrthographicCamera gameCamera;
    protected Viewport gameViewport;

    public BaseScreen(Game game) {
        super();
        this.game = game;
        gameCamera = new OrthographicCamera();
        gameCamera.setToOrtho(false, Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT);
        gameCamera.update();
        gameViewport = new StretchViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT
                , gameCamera);

    }

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


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
public class GameScreen extends BaseScreen {

    private final String TAG = "GameScreen";

    private OrthogonalTiledMapRenderer mapRenderer;

    public GameScreen(Game game) {
        super(game);
        TiledMap map = Assets.getInstance().getMap();
        gameCamera.update();
        mapRenderer = new OrthogonalTiledMapRenderer(map, 1 / 32f);
        Gdx.input.setInputProcessor(new TigerGameScreenInputHandler());

    }



    @Override
    public void render(float delta) {
        super.render(delta);
        gameCamera.update();
        mapRenderer.setView(gameCamera);
        mapRenderer.render();
    }

    private class TigerGameScreenInputHandler extends InputAdapter {

        @Override
        public boolean keyDown(int keycode) {
            if (keycode == Input.Keys.ESCAPE) {
                GameScreen.this.dispose();
                Gdx.app.exit();
            } else if (keycode == Input.Keys.LEFT) {
                gameCamera.translate(+1, 0);
            } else if (keycode == Input.Keys.RIGHT) {
                gameCamera.translate(-1, 0);
            } else if (keycode == Input.Keys.UP) {
                gameCamera.translate(0, +1);
            } else if (keycode == Input.Keys.DOWN) {
                gameCamera.translate(0, -1);
            }
            gameCamera.update();
            return false;
        }
    }
}


I get a map rendering with its bottom left corner in the middle of the screen - i.e. width / 2 x height /2 and the moving around of the camera using the arrow keys works as expected. I want the map to have it's bottom left corner at 0,0.

If, however, I insert the following line of code into the render method after the call to super.render():

1  
gameCamera.setToOrtho(false, Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT);


Then the map is positioned at 0,0, but the keys make no difference.  I'm not massively surprised at how in the second instance the keys don't work since I'm assuming that the call to setToOrtho() repositions the camera at 0, but I can't understand why the setToOrtho() is required in render() in order to get the map to render at 0,0

Similarly, if I run the original code without the line

1  
mapRenderer.setView(gameCamera);
in the render() method then I get the same results as in the second instance above.

I'm assuming that I'm just forgetting or missing something stupid, but I've compared to the previously working code in my prototype and to online tutorials and  I can't see what I'm doing wrong.

Hope that all made sense!

Please put me out of my misery!  Thanks in advance.
Offline dfour
« Reply #1 - Posted 2017-07-05 14:10:19 »

What I do with TiledMap game is clamp the camera position to within the map area.

1  
2  
3  
4  
5  
6  
7  
float evw = cam.viewportWidth * cam.zoom;
float evh = cam.viewportHeight * cam.zoom;
float gutter = viewport.getWorldHeight() / (sh  / 80) ;
cam.position.x = MathUtils.clamp(cam.position.x, evw / 2f, model.currentMapWidth - evw / 2f);
cam.position.y = MathUtils.clamp(cam.position.y, evh / 2f - gutter , model.currentMapHeight - evh / 2f);
cam.update();                                            
mapRenderer.setView(cam);
Offline phunni_2

Junior Newbie





« Reply #2 - Posted 2017-07-05 18:52:57 »

Am OP - had to create a new account because I not only never received an email confirmation, but couldn't get the resend function to work.

@dfour - thanks for the reply.  That's an excellent idea, although I'm not sure it would help to implement that until I've figured out why my app is behaving this way - it might just be papering over the cracks... Especially since, it seems to be something else setting the position of my camera, although I can't figure out what.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (146 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (872 views)
2017-04-27 17:45:51

buddyBro (1024 views)
2017-04-05 03:38:00

CopyableCougar4 (1574 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!