1
votes

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
1
If your getting a rectangle map object from a tiled map you need to know that the x and y values can be tile position not actual x and y. To debug this you need to output the x and y values of that rectangle map object. What you may have to do is multiply the x and y values by tilesize. For example : the x and y value of the rectangle could be x = 6 y =5. That means that the map object is 6 tiles over and 5 tiles upward. You need to convert that into actual x and y values for your game. - compulsivestudios
What do you mean for "you need to know that the x and y values can be tile position not actual x and y". I noticed that with regard to the X axis font is well aligned while it is not aligned with respect to the Y - user3904861
I apologize for the poor typing. I'm in a boring meeting and typing on my tablet. What you need to do is output your x and y of the rectangle to debug this issue. - compulsivestudios
If I do like you say me I don't see the font. Why? - user3904861
I made : font.draw(batch, "Test Position",rect.getRectangle().x*32 ,rect.getRectangle().y*32); 32 is tile width and height - user3904861

1 Answers

0
votes

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.