0

I'm currently developing a game with my team on LibGDX and we're using a .tmx tile map. We're trying to get it to render, and it does render, but oddly in the corner of the screen instead of in the middle of the screen. As in, the bottom left corner of the map appears in the middle of the screen. I believe that at some point, we convince the program that the middle of the screen is x=0, y=0, because we set the initial position of our character (entity) to x=0, y=0, and it appears at the bottom left corner of the map (middle of the screen). I've worked on this problem for hours and I'm at a loss for what to do, especially since this is my first time using LibGDX.

Here's some of our code:

public class MyGame extends Game {

    public static MyGame INSTANCE;
    private OrthographicCamera cam;
    public Viewport viewport;
    
    public MyGame(){
        INSTANCE = this;
    }
    

@Override
public void create () {
            cam = new OrthographicCamera();
            cam.setToOrtho(false, 1130, 1130);
            cam.update();
            viewport = new Viewport(cam);
            setScreen(viewport);
            
}

That's the MyGame class.

public class Viewport extends ScreenAdapter{
    private OrthographicCamera camera;
    private OrthogonalTiledMapRenderer orthogonalTiledMapRenderer;
    private TileMapHelper tileMapHelper;
    
    public Viewport(OrthographicCamera cameraIn){
        camera = cameraIn;
        tileMapHelper = new TileMapHelper(this);
        orthogonalTiledMapRenderer = tileMapHelper.setupMap();
    
}
    
private void update(){
    world.step(1 / 60f, 6,  2);
    
    camera.update();
    
    batch.setProjectionMatrix(camera.combined);
 
}
    
@Override
public void render (float delta) {
            
            this.update();
       
            orthogonalTiledMapRenderer.setView(camera);
            orthogonalTiledMapRenderer.render();
            
            batch.begin();
            // render objects
            batch.draw(img, player.getXLocation(), player.getYLocation());
    
            batch.end();
            camera.position.set(new Vector3(0, 0, 0));
            
}

That's the Viewport class. There's obviously more in those classes than that but I just included the things that have (or might have) something to do with the tile map.

Lastly, here's part of the TileMapHelper class.

public OrthogonalTiledMapRenderer setupMap(){
    tiledMap = new TmxMapLoader().load("dungeon.tmx");
    parseMapObjects(tiledMap.getLayers().get("Wall").getObjects());
    return new OrthogonalTiledMapRenderer(tiledMap);
        
}

Before you ask about the parseMapObjects function, commenting it out changes nothing about how the code executes. So basically, what's supposed to happen is the TileMapHelper loads the .tmx in its function setupMap(), the Viewport class calls setupMap (which returns an OrthogonalTiledMapEditor object) to initialize the OrthogonalTiledMapEditor, Viewport then sets the view and renders it, and MyGame is the one that initializes the camera and gives it to Viewport. Somewhere in that process something is going wrong and both the character and map are appearing in the middle/top corner of the screen like I explained earlier. Also, the reason I put the values 1130 in the setToOrtho function is because that was the only way to get the whole map to appear on the screen, but it's still in the corner.

Any help would be greatly appreciated.

mcp662
  • 1

0 Answers0