简体   繁体   中英

Coordinate Issue with Libgdx and TiledMap

I have a problem with coordinates between Libgdx and TiledMap. I created a map by TiledMap and on it I added a object layer(rectangle) and I want ,when I render in Libgdx the map,to add a font in the same position of rectangle. For this reason in render method I make this:

 @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        renderer.setView(camera);
        renderer.render();

        MapObjects collisionObjects = map.getLayers().get("Rectangle").getObjects();
        for(MapObject object : collisionObjects) {

            if (object instanceof RectangleMapObject) {

                RectangleMapObject rect = ((RectangleMapObject) object);

                batch.setProjectionMatrix(camera.combined);
                batch.begin();
                font.setUseIntegerPositions(false);
                font.draw(batch, "Test Position",rect.getRectangle().x,rect.getRectangle().y);
                batch.end();

             }
        }
        }

This is How I set camera and other in the create method:

 @Override
    public void create () {

        font = new BitmapFont();
        font.setColor(Color.BLACK);

        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        Gdx.input.setInputProcessor(this);

        camera = new OrthographicCamera(w,h);
        camera.translate(w/2, h/2);

        camera.update();

        batch = new SpriteBatch();

        map = new TmxMapLoader().load("TestRectangleMap.tmx");

        renderer = new OrthogonalTiledMapRenderer(map);

        }

My problem is that the font is drawn in incorrect position respect to the position of the rectangle that is in the tile map.

Could you help me to understand where is the problem. Thank you very much for the time you spent.

Coordinates output I done this:

System.out.println("X: "+rect.getRectangle().x);
System.out.println("Y: "+rect.getRectangle().y);

I get this:

X: 450.0
Y: 232.0

while the object's coordinates in TiledMap editor are:

X: 14,062
Y: 5,156

Let me try to adjust this just to clean things up a bit. I'm pretty sure you only need to begin one batch before drawing. So embedding then into you for loop I don't think is necessary.

There is a couple of things that should be on the console display to debug this further,

You can try to add the Gdx.app.log() method whenever you need something to output to the console.

@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.update();
    renderer.setView(camera);
    renderer.render();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    MapObjects collisionObjects = map.getLayers().get("Rectangle").getObjects();
    for(MapObject object : collisionObjects) {
        if (object instanceof RectangleMapObject) {
            RectangleMapObject rect = ((RectangleMapObject) object);
            font.setUseIntegerPositions(false);
            font.draw(batch, "Test Position",rect.getRectangle().x,rect.getRectangle().y);
            Gdx.app.log("My Game", "Rect X: "+rect.getRectangle().x+" Y : "+rect.getRectangle().y);
         }
    }
    batch.end();
}

Adding logs to your code will help out when your trying to find out where an object is drawn.

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