我正在以等距视角为班级做游戏。我正在使用libgdx和一个用平铺(tmx)制作的地图,我花了两个星期寻找信息,但无法找到知道我在地图上点击什么瓷砖的方式。我看到很多页面说我必须将屏幕坐标转换为地图上的坐标,但它不起作用。瓷砖是64x32。
我有一个地图演员和一个屏幕逻辑表。
这个演员:
public class ActorMapa extends Actor{
Game juego;
//elementos del mapa
TiledMap mapa;
IsometricTiledMapRenderer mapRender;
OrthographicCamera camara;
public ActorMapa(Game juego, OrthographicCamera camara) {
this.juego= juego;
this.camara= camara;
//instancio el mapa
mapa = new TmxMapLoader().load("data/mapa_plano.tmx");
//le indico la escala al mapa
mapRender = new IsometricTiledMapRenderer(mapa);
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.setProjectionMatrix(camara.combined);
mapRender.setView(camara);
mapRender.render();
super.draw(batch, parentAlpha);
}
// Convert the parsed coordinates back to map coordinates
public Vector2 convertScreenToMapCoordinates(float x, float y,
int tileWidth, int tileHeight) {
Vector2 result = new Vector2();
result.x = x / tileWidth;
result.y = y / tileHeight;
return result;
}
public Vector2 convertMapToIsometricCoordinates(float mapX, float mapY,
int tileWidth, int tileHeight) {
Vector2 result = new Vector2();
result.x = (mapX - mapY) * (tileWidth / 2);
result.y = (mapX + mapY) * (tileHeight / 2);
return result;
}
}
我把它放在表格的表格中:
camara.unproject(camara.position);
pos1.set(mapa.convertScreenToMapCoordinates(Gdx.input.getX(),Gdx.input.getY(),64,32));
pos2.set(mapa.convertMapToIsometricCoordinates(pos1.x, pos1.y, 64, 32));