简体   繁体   中英

libgdx rendering tiledmap at a certain X coordinate of camera

I'm trying to implement infinite scrolling in this Android game. To test this I'm just creating two tiledMaps with the same .tmx file, and then rendering them one after the other with respect to their x-values.

The problem is that I can't get this setView to work:

TiledMap tiledMap2 = new TiledMap();
TmxMapLoader loader2 = new TmxMapLoader();
tiledMap2 = loader2.load("data/map1.tmx");


OrthogonalTiledMapRenderer tileRenderer2 = new OrthogonalTiledMapRenderer(tiledMap2);
tileRenderer2.setView(camera.combined, 512, 0, 512, 512);
tileRenderer2.render();

Now I get this exact chunk of code to work earlier if I just use

tileRenderer2.setView(camera);

instead of

tileRenderer2.setView(camera.combined, 512, 0, 512, 512);

Is my problem one with the first argument of setView()?

just change the position of your camera and you are done. camera.position.x = ... and camera.position.y = ... With setView you just define the Camera that does define the View. It does not define the position of the camera. It just the view-bounds for the internal optimized rendering.

@Override
public void setView(OrthographicCamera camera) {
    spriteBatch.setProjectionMatrix(camera.combined);
    float width = camera.viewportWidth * camera.zoom;
    float height = camera.viewportHeight * camera.zoom;
    viewBounds.set(camera.position.x - width / 2, camera.position.y - height / 2, width, height);
}

Thats used inside of the RenderTileLayer method for getting the first and last tiles that are visible :

final int col1 = Math.max(0, (int) (viewBounds.x / layerTileWidth));
final int col2 = Math.min(layerWidth, (int) ((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));

final int row1 = Math.max(0, (int) (viewBounds.y / layerTileHeight));
final int row2 = Math.min(layerHeight, (int) ((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight)); 
//here does follow the rendering

Everthing from the sourcecode from libGDX

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