简体   繁体   中英

Centering map or camera 2D game

I've tried so many solutions that it's possible that my code is a bit mixed up, but whatever I try, it just won't work.

Basically I made a map with Tiled, where my player can run around and bump into stuff. I want the whole map to be visible for the whole time (it's 20 by 15, 64 pixels a tile). The camera doesn't need to move around or follow the player, it has to stay still at the center of the map.

The problem is that the map only shows in the upper right corner of the screen. When I centered the camera to the map itself it messed up the collission detection, (bumping into trees while they were not visible & walking through visible trees). So what I want to do is center the map to 0,0 where my camera also is (at least I think..).

Another thing I'd like to accomplish is that the size of the map gets resized to match different mobile phones. Tried to accomplish this with the stretchviewport, but haven't been able to test this.

public class PlayScreen implements Screen {
    TiledMap map;
    OrthogonalTiledMapRenderer mapRenderer; 
    OrthographicCamera cam;        
    float unitScale = 1 / 64f;
    OrthogonalTiledMapRenderer renderer = new OrthogonalTiledMapRenderer(map, unitScale);
    Viewport viewport;



 public void show() {       
     map = new TmxMapLoader().load("maps/map.tmx");             
     mapRenderer = new OrthogonalTiledMapRenderer(map); 
     cam = new OrthographicCamera(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
     cam.setToOrtho(false);
     viewport = new StretchViewport(1280, 960, cam); 

        bounds = new ArrayList<Rectangle>();
        for(int i = 0; i < 20; i++){
            for(int j = 0; j < 15; j++){
                TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers().get(1); 
                Cell cell = new Cell(); 

                Vector3 center = new Vector3(cur.getWidth() * cur.getTileWidth() / 2, cur.getHeight() * cur.getTileHeight() / 2, 0);
                cam.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
                cam.update();

                if(cur.getCell(i,j) != null){ //null = first layer   != --> if its not 
                    cell = cur.getCell(i, j);
                    System.out.println(i + ", " + j + ", " + cell.getTile().getId());
                    bounds.add(new Rectangle(i * 64, j * 64, 64 , 64)); 
            }
        }
   }    


public void render(float delta) {

        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        mapRenderer.setView(cam);
        mapRenderer.render();

        cam.position.set(0, 0, 0);
        cam.update();

        batch.setProjectionMatrix(cam.combined);

        batch.begin();
        batch.draw(player.getCurrentFrame(), player.getPosition().x , player.getPosition().y);
        player.update(); 

        for(int i = 0; i < bounds.size(); i++){
            if(bounds.get(i).overlaps(player.getBounds())){
                int x = (int)bounds.get(i).x / 64;
                int y = (int)bounds.get(i).y / 64;

                TiledMapTileLayer cur = (TiledMapTileLayer)map.getLayers().get(1);
                Cell cell = cur.getCell(x, y);

                if(cell.getTile().getProperties().containsKey("blocked")){
                    System.out.println("bush"); 
                }
                player.reAdjust();
            }
        }

        batch.end();

 }


        public void resize(int width, int height) {
        viewport.update(width, height); 
    }   

Nevermind, I deleted: cam.position.set(0, 0, 0); and everything seems to work just fine. Guess I already made some changes what caused it to work, just didn't see it cause this was still around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM