If you ignore the constants you have in your calculations (such as tile width, for example) the calculations for isometric to cartesian and back are usually;
float isometricX = cartesianX - cartesianY;
float isometricY = (cartesianX + cartesianY) * 0.5f;
And the reverse is
float cartesianX = (2.0f * isometricY + isometricX) * 0.5f;
float cartesianY = (2.0f * isometricY - isometricX) * 0.5f;
That's assuming square tiles of unit length.
I'm not sure how your MAP_HEIGHT
plays into this, but you should be able to scale that by your tile size to get the values need for your game.
Edit;
To use this to pick a tile the following method could be used (note that here tileSize
is the height of the tile, so in your case 32px);
private Vector2 screenToMap(float screenX, float screenY) {
float mapx = (screenX / tileSize + screenY / (tileSize * 0.5f)) * 0.5f;
float mapy = (screenY / (tileSize * 0.5f) - (screenX / tileSize)) * 0.5f;
return new Vector2(mapx - 0.5f, mapy + 0.5f); // -.5/+.5 because the drawing isn't aligned to the tile, it's aligned to the image
}
private Vector2 cartesianToIsometric(float cartesianX, float cartesianY) {
float isometricX = (cartesianX - cartesianY);
float isometricY = (cartesianX + cartesianY) * 0.5f;
return new Vector2(isometricX, isometricY);
}
The above doesn't take a viewport into account, so you'll have to do panning and zooming yourself, or you could rely on libGDX's viewport.
I knocked up a pure java example of this where I draw the map, get the map coordinate of the mouse and pick tiles; Isometric sandbox
(again, this isn't actually isometric but I think it's what you are looking for).
Edit:
When doing this in a libGDX application, under a OrthographicCamera
, you need to unproject screen coordinates to camera space, then call my screenToMap
function as that works on unprojected coordinates.
So at the beginning of the map's render you need to do something like this;
public Vector2 unproject(OrthographicCamera camera, Vector2 position) {
Vector3 unprojected = camera.unproject(new Vector3(position.x, position.y, 0));
return new Vector2(unprojected.x, unprojected.y);
}
private Vector2 screenToMap(Vector2 screen) {
float mapx = (screen.x / tileSize + screen.y / (tileSize * 0.5f)) * 0.5f;
float mapy = (screen.y / (tileSize * 0.5f) - (screen.x / tileSize)) * 0.5f;
return new Vector2(mapx - 0.5f, mapy + 0.5f); // -.5/+.5 because the drawing isn't aligned to the tile, it's aligned to the image
}
public void render(OrthographicCamera camera, Vector2 mousePosition) {
Vector2 mp = screenToMap(unproject(camera, mousePosition));
spriteBatch.setProjectionMatrix(camera.combined);
Fact that libGDX has a y-axis that increases as you go up on the screen doesn't matter, the unproject will fix that for you as you'll also render in the different direction.
For sake of completion, here's a link to a libGDX application of what I am explaining here: libGDX source. It will render a map and highlight the cell you're over with the mouse.
I assume you're doing this because you want to learn, because if you just want a isometric map in your game, libGDX comes with Tmx-classes that can load and render a isometric map created in for example Tiled.