Reputation: 148
I'm trying to calculate the coordinates of individual tiles within a chunk on an isometric grid from the coord of a chunk that is in a standard x,y grid format of its own, there are 2 coord systems the first is the chunk coord this is a standard x and y grid however the chunk contains tiles that are in an isometric grid.
A little context for the question: I'm experimenting with infinite open-world generation using Python and the SDL wrapper pygame-ce. The game has an isometric style, meaning the world grid is isometric. The problem arises when finding the coordinates of individual tiles. When you generate a chunk, all that is passed in is the chunk's coordinates.
The chunk system works on a standard x and y grid. Each grid is 16x32. The spawn chunk is (0,0). The chunk to the left of that is (-1,0), and so on. We can calculate the in-game coordinates with the following calculation:
# Work out the top left in-game coordinate of the chunk
tile_size = 64
x = chunk_grid_x * (16 * tile_size)
y = chunk_grid_y * (32 * tile_size)
However, I'm pretty sure you have to add an offset to account for the isometric conversion. This is where my knowledge and skill fall short. I can't quite understand how to convert. I've watched a couple of videos and read articles, but I can't seem to find a solution to convert from grid to isometric and then to isometric.
Above is an example of an isometric grid where the middle is the origin (0, 0). However, when you generate a chunk, you only know the chunk's coordinates, which is the top left (which would be the very topmost cell in the diagram above).
as you can see this is no confusing, the center chunk is (0,0) and the center tile in that chunk is (0,0) no working that out isnt so difficult however working it out for a chunk like this (-19,33) is a bit harder
def generate_chunk(self, chunk_x, chunk_y):
chunk_x_px = CHUNK_SIZE_X * self.tile_size + self.three_quarters_tile_size
chunk_y_px = CHUNK_SIZE_Y * self.three_quarters_tile_size + self.three_quarters_tile_size
chunk = pygame.Surface(( chunk_x_px, chunk_y_px ), pygame.SRCALPHA)
rect = pygame.Rect(0, 0, self.tile_size, self.tile_size)
for y in range(CHUNK_SIZE_Y * 3):
for x in range(CHUNK_SIZE_X):
if y % 2 == 0:
tile_x = (chunk_x * CHUNK_SIZE_X) + (x * 2)
else:
tile_x = (chunk_x * CHUNK_SIZE_X) + (x * 2) + 1
tile_y = 0 #(chunk_y * CHUNK_SIZE_Y) + y - x
random.seed(f'{x}{chunk_x}{self.seed}{chunk_y}{y}')
if y % 2 == 0:
rect.x = ( x * self.tile_size )
else:
rect.x = ( ( ( ( x * self.tile_size ) + ( y * self.half_tile_size ) ) - ( math.ceil( ( y - 1 ) / 2 ) * self.tile_size ) ) )
rect.y = ( y * self.tile_size ) - ( y * self.three_quarters_tile_size )
if random.randint(0,10) == 0:
tile = random.choice(self.tilesets_list)
else:
tile = self.tilesets_list[0]
text = font.render(f'{tile_x}, {tile_y}', True, (255, 255, 255)) # Change the text and color as needed.
chunk.blit(text, (rect.x + 25, rect.y + 3))
#chunk.blit(tile, rect)
return chunk
One workaround is to use normal grids and then add a rotation effect to the screen to make it appear isometric. However, since pygame and SDL are not fast, this would incur a significant performance cost, as every frame would have to be rotated and all the new pixels would have to be calculated.
*This problem would probably be best asked on math exchange however in it of its own it is a programming problem as well
One question on Stackoverflow is nearly what I am after but just enough different to not work for me Getting isometric grid coordinates from standard X,Y coordinates
there is also a very similar question to get coords on an isometric grid with pygame, however it doesn't work with the chunk system:
Get isometric tile mouse selection in Pygame
code: https://github.com/NotReeceHarris/dungeon-mmo/blob/main/src/level/surface/world.py
Upvotes: 1
Views: 99