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