I entered 2 isometric games in the recent Java4k competition
Falcon4kAssassins4kThe drawing routine starts at the back row and draws each column of blocks in turn in back to front order (painters algorithm).
This works 100% for drawing the blocks, but what do you do when you want to draw the player, which can overlap up to 4 columns?
The answer is to draw the player in every column it overlaps. You draw all the blocks below the player. Then draw the player. Then draw any blocks above the player.
However you need to mask out the pixels that fall outside the column. This can be achieved by clipping left and right to the column boundaries. Assassins4k uses this technique. You effectively draw the player 4 times. This only works if there are no blocks above the player.
If there are blocks above the player (i.e. you can go under an arch) or you want to draw shadows (as in Falcon4k), clipping left and right is insufficient. You need to mask for the hexagon shape of the isometric cube for the player, and for the quadrilateral floor tile shape for the shadow. You can use Graphics2D and AlphaComposite for this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| gc.setComposite(AlphaComposite.Src); gc.setColor(transparent); gc.fillRect(0, 0, IMAGEWIDTH, IMAGEHEIGHT); gc.drawImage( m<2?shadow[6+m]:shadow[8], bsx, bsy, bsx+tw, bsy+th, tx, ty, tx+tw, ty+th, null);
gc.setComposite( AlphaComposite.DstIn); gc.drawImage(mask, 0, 0, null);
if (mHeight>0) g.drawImage(composite, sbx, sby, null); |
The complicated stuff in the drawImage calls deals with the fact that the player shadow is offset w.r.t. the mask (which is aligned to the column we are drawing)