I have an isometric world that has stuff on multiple levels, i.e. it has an XYZ coordinate system, and tiles can be stacked on each other, accumulating screen Y offset as they go. I currently have an algorithm in place that converts the screen coordinates to world XY coordinates, but it doesnt take into account that there might be e.g. a hill under the mouse and selects the tile behind it instead.
Is there a generalized form algorithm for this? I need a generalized form because all the variables are unknown (I'm writing a game engine):
- Tile width (as in the width of a single tile rectangle)
- Tile height (as in the height of a single tile rectangle)
- Tile depth (as in how much Y offset each level contributes)
I suppose one option is run down the map with something like this (pseudo code):
p = screenToMap(screenPos)
bestMatch = null
for (x=p.x, y=p.y, z=0; x<mapWidth && y<mapHeight; x++, y++, z++) {
b = highestPlacedBlockInPos(x, y)
if (b.z === z) bestMatch = [x, y, z]
}
But I'm wondering if there's a better way? This seems pretty ineffective especially if you have a big map.