5
\$\begingroup\$

I know this has been asked many times but my question doesn't quite fit the others. I have an IsometricTiledMap and I need to get the tile under a set of screen X and Y coordinates. Here is a picture of my game:enter image description here

I need to get the tile that the player is standing on. I need to convert screen coordinates to isometric tile coordinates and the way that seemed the easiest was by using a matrix as described here: http://www.alcove-games.com/advanced-tutorials/isometric-tile-picking/#comment-56390

Unfortunately I have no idea how to change that code to fit a 128x64 tile instead of a 1.0x.5 tile. Here is my code

isoTransform = new Matrix4();
isoTransform.idt();
isoTransform.translate(0.0f, 32f, 0.0f);
isoTransform.scale((float) (Math.sqrt(2.0) / 2.0), (float) (Math.sqrt(2.0) / 4.0), 1.0f);
isoTransform.rotate(0.0f, 0.0f, 1.0f, -45.0f);

invIsotransform = new Matrix4(isoTransform.inv());

//touch vector
touch = new Vector3();

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touch.set(screenX, screenY, 0);
    touch = gameCamera.unproject(touch);
    touch.mul(invIsotransform);

    pickedTileX = (int)touch.x / 64;
    pickedTileY = (int)touch.y / 32;

    debug(touch.x + " " +  touch.y);
    debug(pickedTileX + " " + pickedTileY);

    return false;
}

I think the problem is in this line isoTransform.scale((float) (Math.sqrt(2.0) / 2.0), (float) (Math.sqrt(2.0) / 4.0), 1.0f); but I'm not sure what to change.

\$\endgroup\$
  • \$\begingroup\$ See this for translating coordinates: gamedev.stackexchange.com/questions/79108/… \$\endgroup\$ – bazola Jun 27 '15 at 13:50
  • \$\begingroup\$ camera.unproject() is for converting pixel coordinates to game world coordinates. Not really the same as what I am trying to do. Unless you are saying that is why 0, 0 is in the wrong place in my game. Can you explain what you mean? \$\endgroup\$ – Vincent Williams Jun 27 '15 at 16:08
0
\$\begingroup\$

So I finally found a method on some Github repository (I can't find the link again), and this one actually works. It doesn't use matrices and it doesn't to measure the screen or any of that crap.

private Vector3 worldToIso(Vector3 point, int tileWidth, int tileHeight) {
    gameCamera.unproject(point);
    point.x /= tileWidth;
    point.y = (point.y - tileHeight / 2) / tileHeight + point.x;
    point.x -= point.y - point.x;
    return point;
}

You probably want to round the coordinates down but other than that this works fine. 0, 0 is at the left most tile.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.