If I have a two-dimensional array with ones and zeros for water and land respectively how do I choose what shoreline tile to use according to the water and land tiles surrounding it? The shoreline can form straight lines, outside corners and inside corners and there are four possible orientations for all of them. I have all the necessary tiles in the sprite sheet numbered. I want it to look similar to the shoreline in Age of Empires 2.

Age of Empires 2 example

I could come up with a way to do this by comparing the eight surrounding tiles but this quickly became a mess as there are a lot of different possible combinations to take into account and manually assessing all of them with if statements seemed very unpractical and I'm quite sure there is a more elegant way of doing this. (I'm making this game with Slick2D)


1 Answer 1


You can accomplish this with bitmasking. Each tile determines if it is an edge tile, based on its neighboring tiles.

enter image description here

From the image above, we would assign a value to each neighbor. 1, 2, 4, or 8 based on its location. If the neighbor is a different tile Type than the one in the middle, you add the corresponding value.

These values map out as shown in the image. You can then assign textures based on these values.

In your case, if anything is not 15 -- then you know it is part of an edge tile.

Take a look at my blog post on Finding Neighbors and Bitmasing here for more information.

  • \$\begingroup\$ Using only cardinal neighbors you cannot distinguish inside corners from outside corners. If you include all eight neighbors (giving 256 possible combinations) you can distinguish corners by their inside neighbor. I couldn't think of a clever way to identify tiles, so after bit-encoding I just have collections for each tile type and test containment. \$\endgroup\$
    – kitsu.eb
    Commented Jul 18, 2016 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .