Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Calin Leafshade on Sat 14/05/2011 22:22:37

Title: Finding if a pixel is visible in an iso grid.
Post by: Calin Leafshade on Sat 14/05/2011 22:22:37

(http://i.imgur.com/kCyOd.png)
(placeholder gfx)

Ok so lets assume there is a sprite sat on one of these iso tiles. How could i tell (mathematically if possible rather than drawing function) if an individual pixel of that sprite is visible and not hidden behind a higher tile in front of it?
Title: Re: Finding if a pixel is visible in an iso grid.
Post by: Wyz on Sun 15/05/2011 00:01:41
You could use height maps. I've made an isometric walk-behind system for Tier a while back that uses that method. In fact, it can be done with pixelshaders.
But if you just want to test a single pixel you can still use this method, you'd check the height of the pixel using the height map and compare it with the base pixel of the sprite. For a more advanced system you could also give the sprites a height map for even preciser rendering.
Title: Re: Finding if a pixel is visible in an iso grid.
Post by: Snarky on Sun 15/05/2011 00:35:00
So Wyz, are you saying that as well as rendering the on-screen graphic, Calin should render (in memory) a second version of the screen that just displays the elevation of each pixel (encoded as a color value)? And then when he wants to see if the pixel is visible, check the value in this height map and see if it matches the height of the tile?

I think another possible solution, which will work as long as the difference in elevation is never so high that a tile is obscured by another tile it's not adjacent to (including diagonally), is to just check those three tiles (down-left, down, down-right), and figure out if the pixel is covered up by one of them. If one level of elevation is A pixels tall on the screen, the only pixels that can be covered up by an adjacent tile N levels higher are those within N*A pixels from the bottom isometric edge (which is easily calculated from the coordinate of the pixel relative to the tile), but there are some other constraints I can't quite define off the top of my head. I think for the down-left and down-right adjacent tiles it's simply which side of the tile it's on, but the down-adjacent tile is trickier.

Edit: Wait, I think I've got it. Write a function y_coord() that gives you the Y-coordinate of the top edge of the tile for any X-coordinate (relative to the tile itself). Since the edge is a straight line of 2:1 ratio, you simply take the distance from the X-midpoint and halve it (but make sure it rounds correctly). Define the Y-height of the tile, tile_height, and the Y-height of an elevation level elev_height. Then for pixel (a,b) in the tile t, you can check whether it's covered by the tile down from it, t_down:

y_down = y_coord(a) + tile_height - (t_down.elevation - t.elevation)*elev_height; // Check how high up the next tile down covers at this X-coord
pixelIsCovered = (y_down <= b);


For the down-left and down-right tiles, you can do the same, but instead of calculating y_coord(a), you take y_coord(a + tile_width/2) and y_coord(a - tile_width/2).