mouse position to isometric tile including height

前端 未结 7 967
太阳男子
太阳男子 2021-01-30 22:11

Struggeling translating the position of the mouse to the location of the tiles in my grid. When it\'s all flat, the math looks like this:

this.position.x = Math.         


        
7条回答
  •  攒了一身酷
    2021-01-30 22:35

    Please don't judge me as I am not posting any code. I am just suggesting an algorithm that can solve it without high memory usage.

    The Algorithm:

    Actually to determine which tile is on mouse hover we don't need to check all the tiles. At first we think the surface is 2D and find which tile the mouse pointer goes over with the formula OP posted. This is the farthest probable tile mouse cursor can point at this cursor position.

    This tile can receive mouse pointer if it's at 0 height, by checking it's current height we can verify if this is really at the height to receive pointer, we mark it and move forward.

    Then we find the next probable tile which is closer to the screen by incrementing or decrementing x,y grid values depending on the cursor position.

    Then we keep on moving forward in a zigzag fashion until we reach a tile which cannot receive pointer even if it is at it's maximum height.

    When we reach this point the last tile found that were at a height to receive pointer is the tile that we are looking for.

    In this case we only checked 8 tiles to determine which tile is currently receiving pointer. This is very memory efficient in comparison to checking all the tiles present in the grid and yields faster result.

提交回复
热议问题