[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Isometric Math



Jasper wrote:

-How do I do "picking," identifying the tile or character a user has
clicked on? I can imagine calculating it for a flat iso landscape, but
taking height into account all I've come up with so far is to save a
bunch of bounding rectangles and test them inefficiently.
Each hex and piece has an associated "mouseMap", which is the same shape with all opaque pixels converted to pure white. When reading what the mouse is pointing to, I draw one frame of the scene off screen using the mouseMaps, with each having a colorFilter that encodes a number (this way the hexes can all share the same texture, and varry only their colorFilter). So each hex mouseMap is drawn with an RGB color of (x,y,0), while game-piece mouseMaps are drawn with RGB = (0,0,id). Then I read the color of the screen pixel the mouse points to, and decode it to precisely determine what hex/piece is picked.

Right, you suggested this at about the same time I thought of it too -- so, good idea!


Aha! I was able to implement this "picking" trick in my isometric engine today. I haven't done it for the sprites yet, but it works for tiles. This is what the map looks like, drawn now on a hidden surface:
http://kschnee.xepher.net/pics/shiningsea060627-0s.png
I was using the color (0,x*10,y*10) for visibility, but changed it to (0,(x+1)*10,(y+1)*10) so that tile (0,0) would be a different color than the areas that aren't tiles. I used pygame.draw.polygon() to fill the iso-tiles.


Also I've got multiple-zone loading working now too, and loading from XML map files, so you can walk forever in any direction. I still want to display the edges of adjacent zones so there are no black areas, though.

Kris