How can I improve my isometric tile-picking algorithm?
- by Cypher
I've spent the last few days researching isometric tile-picking algorithms (converting screen-coordinates to tile-coordinates), and have obviously found a lot of the math beyond my grasp.
I have come fairly close and what I have is workable, but I would like to improve on this algorithm as it's a little off and seems to pick down and to the right of the mouse pointer.
I've uploaded a video to help visualize the current implementation:
http://youtu.be/EqwWcq1zuaM
My isometric rendering algorithm is based on what is found at this stackoverflow question's answer, with the exception that my x and y axis' are inverted (x increased down-right, while y increased up-right).
Here is where I am converting from screen to tiles:
// these next few lines convert the mouse pointer position from screen
// coordinates to tile-grid coordinates. cameraOffset captures the current
// mouse location and takes into consideration the camera's position on screen.
System.Drawing.Point cameraOffset = new System.Drawing.Point( 0, 0 );
cameraOffset.X = mouseLocation.X + (int)camera.Left;
cameraOffset.Y = ( mouseLocation.Y + (int)camera.Top );
// the camera-aware mouse coordinates are then further converted in an attempt
// to select only the "tile" portion of the grid tiles, instead of the entire
// rectangle. this algorithm gets close, but could use improvement.
mouseTileLocation.X = ( cameraOffset.X + 2 * cameraOffset.Y ) / Global.TileWidth;
mouseTileLocation.Y = -( ( 2 * cameraOffset.Y - cameraOffset.X ) / Global.TileWidth );
Things to make note of:
mouseLocation is a System.Drawing.Point that represents the screen coordinates of the mouse pointer.
cameraOffset is the screen position of the mouse pointer that includes the position of the game camera.
mouseTileLocation is a System.Drawing.Point that is supposed to represent the tile coordinates of the mouse pointer.
If you check out the above link to youtube, you'll notice that the picking algorithm is off a bit. How can I improve on this?