xXZyzzXx
xXZyzzXx

Reputation: 13

Screen to staggered isometric grid algorithm

I'm working with Kivy and Pytmx, I need to get Grid Tile from Map Coordinates for staggered map.

mymap

My tile size is:

TILE_WIDTH = 256
TILE_HEIGHT = 149

When I used the isometric diamond grid, I calculated it like this:

def screen_to_isometric_grid(cartX, cartY):
    screenx = mh - cartY / (TILE_HEIGHT * SPRITE_SCALING) + cartX / (TILE_WIDTH * SPRITE_SCALING) - mw / 2 - 1 / 2
    screeny = mh - cartY / (TILE_HEIGHT * SPRITE_SCALING) - cartX / (TILE_WIDTH * SPRITE_SCALING) + mw / 2 - 1 / 2
    screenx2 = round(screenx)
    screeny2 = round(screeny)
    return screenx2, screeny2

Now I'm using staggered map and I dont know how to get tile coordinates.

I found algorithm in C++ here enter link description here

Upvotes: 1

Views: 333

Answers (1)

xXZyzzXx
xXZyzzXx

Reputation: 13

def subregion(px, py, r_x, r_y):
rx = int(r_x)
ry = int(r_y)
foo = px - py
bar = px + py
if foo < 0 and bar > 1:  # Top
    return [rx, ry]
elif foo < 0 and bar < 1:  # Left
    if r_y > 0:
        if py > 0.5:
            return [rx - 1, ry + 1]
        return [rx - 1, ry]
    else:
        return None
elif foo > 0 and bar > 1:  # Right
    if r_y > 0:
        if py > 0.5:
            return [rx, ry + 1]
        return [rx, ry]
    else:
        return None
elif foo > 0 and bar < 1:  # Bottom
    if r_y < 0:
        return [rx, ry]
    return [rx, ry + 1]

Upvotes: 0

Related Questions