Reputation: 555
I'm developing a game resembling the popular 'Warlock' custom map from WC3. For the game I am using a tiledmap, however the player does not (and is not supposed to) move using the LibGDX tiledmap api. Instead the player moves by vectors. I'm trying to register which tile the player is standing on, by taking in the player's world coordinates and finding the respective Row and Column for that exact tile.
I seem to have it properly working for an orthographic tiledmap with the following piece of code:
private boolean checkIfOnLava()
{
for (Entity e : world.getEntities(PLAYER)) {
float height = currentLayer.getTileHeight() * currentLayer.getHeight();
float width = currentLayer.getTileWidth() * currentLayer.getWidth();
float playerX = e.get(Position.class).getX();
float playerY = e.get(Position.class).getY();
int tileRow = (int) (playerX / currentLayer.getTileWidth());
int tileCol = (int) Math.abs((playerY - (height / 2)) / currentLayer.getTileHeight());
System.out.println("Row: " + tileRow);
System.out.println("Col: " + tileCol);
if (currentLayer.getCell(tileRow, tileCol).getTile().getId() == 3) {
System.out.println("Walking on lava");
return true;
}
}
System.out.println("walking on ground");
return false;
}
However, when working with an isometric map it seems a bit more tricky to find the respective column and row of the tile. I think I'll have to use some math for Rombus' instead of squares, but I'm not sure if thats correct, and if so, which math to use.
Upvotes: 1
Views: 407
Reputation: 555
I found a solution to my problem, with the use of the following method, it can now identify the Row and Column of the tile the player is standing on, based on its position in pixels.
private boolean OnLava()
{
for (Entity e : world.getEntities(PLAYER)) {
float playerX = e.get(Position.class).getX();
float playerY = e.get(Position.class).getY();
int tileRow = (int) (playerX / currentLayer.getTileWidth() - (playerY / currentLayer.getTileHeight()));
int tileCol = (int) Math.abs((tileRow * currentLayer.getTileHeight() / 2 + playerY) / (currentLayer.getTileHeight() / 2));
if (currentLayer.getCell(tileRow, tileCol).getTile().getId() == 3) {
return true;
}
}
return false;
}
Upvotes: 1