Just thought I’d share the final functions I settled on for isometric coordinate calculations. Unlike most of the others I’ve seen around the web, these account for:
* Origin offset
* Tile-based position offset
* Varied scale
* Any aspect ratio (not just 2:1)
def GetScreenPositionForTile(self, map_pos, view, origin, offset, delta=None): if self.projection == Layer.Projections.BirdsEye: screen_x = origin[0] + map_pos[0] * self.tile_width screen_y = origin[1] + map_pos[1] * self.tile_height if delta is not None: screen_x += delta[0] screen_y += delta[1] elif self.projection == Layer.Projections.Isometric: map_pos = (map_pos[0] - offset[0] / self.scale, map_pos[1] - offset[1] / self.scale) screen_x = origin[0] - (map_pos[1] * self.tile_width / 2) + (map_pos[0] * self.tile_width / 2) - (self.tile_width / 2) screen_y = origin[1] + (map_pos[1] * self.tile_height / 2) + (map_pos[0] * self.tile_height / 2) if delta is not None: iso_delta_x = (delta[0] - delta[1]) / 2 * self.tile_aspect iso_delta_y = (delta[1] / 2) + (delta[0] / 2) screen_x += iso_delta_x screen_y += iso_delta_y return screen_x, screen_y
def GetTileAtScreenPosition(self, screen_pos, view, offset): origin_x, origin_y = self.GetOrigin(view) map_x_screen = screen_pos[0] - origin_x map_y_screen = screen_pos[1] - origin_y if self.projection == Layer.Projections.BirdsEye: map_x = map_x_screen / self.tile_width map_y = map_y_screen / self.tile_height map_delta_x = map_x_screen % self.tile_width map_delta_y = map_y_screen % self.tile_height elif self.projection == Layer.Projections.Isometric: map_x_raw = map_y_screen + int(map_x_screen / self.tile_aspect) map_y_raw = map_y_screen - int(map_x_screen / self.tile_aspect) map_x = map_x_raw / self.tile_height map_y = map_y_raw / self.tile_height map_delta_x = map_x_raw % self.tile_height map_delta_y = map_y_raw % self.tile_height map_x += offset[0] / self.scale map_y += offset[1] / self.scale return map_x, map_y, map_delta_x, map_delta_y
I could comment the code better of course, but I think it’s actually pretty straightforward and self-explanatory.
Full code: http://ohshitzombies.com/PallidumZ/trunk/game/board/Layers.py