Wang 2-Corner Tiles
Tags: graphics, programming, tilesets, minimicro
Published: 2024-05-29 15:44:25

There is a trick commonly used to draw terrain in RPG games, walls in maze games, islands and continents in world-map type games, and much more.  It's a type of auto-tiling known as **Wang 2-corner tiles**, and until recently, the best explanation on the internet was found [here](http://www.cr31.co.uk/stagecast/wang/2corn.html).  But that website is now dead, alas.

You can still view it on the Wayback Machine [here](https://web.archive.org/web/20230425150015/http://www.cr31.co.uk/stagecast/wang/2corn.html), but perhaps it's time for a fresh explanation.  This blog post is meant to be that — a quick, concise explanation of this simple yet brilliant idea.

## Wang Tilesets

[Wang tiles](https://en.wikipedia.org/wiki/Wang_tile) were dreamt up by a mathematician named Hao Wang in the 1960s.  The basic idea of is this: make tiles that match their neighbors, either on the edges (_edge-matching Wang tiles_) or on their corners (_corner-matching Wang tiles_).  You need only a limited set of such tiles; if each corner has two possible variations — for example, each corner is either "land" or "water" — and 4 corners, then the minimum number of tiles is 2^4, or 16 tiles.

In computer games, corner-matching Wang tiles seem to be more common.  If there are 2 possible types for each corner, this is known as a Wang 2-corner tileset.  If there are 3 (for example: water, sand, and jungle), then you need 3^4 = 81 different tiles, which is considerably more work to make.  For this reason, 2-corner tilesets are the most common of all.

## Selecting the Right Tile

We can assign each of the 16 tiles in a 2-corner tileset an index number from 0 to 15.  Then, we can find the correct index by simply adding up a number assigned to each corner:

![Diagram showing corner values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnyp57z29k482mqlts29.png)

So here we're giving the northeast corner a value of 1, the southeast corner a value of 2, 4 for the southwest, and 8 for the northwest.  Add up the values of the corners where you want land (or whatever your tileset represents) to find the tile index.

![Three examples of tile index calculation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqo9r7tss82081v8d2lu.png)

Here are four examples of Wang 2-corner tilesets that follow this indexing scheme.  (These are from the now-defunct site mentioned at the top of this article.)

![Example 2-corner Wang Tile sets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rn3xt6xaewwqn7xkt25.png)

## Choose Your Thickness

The artist draws each tile so that the division between the two types (e.g. land/water) happens at the same location on the tile edge, so it will meet up perfectly with whatever tile is next to it.  On the interior of the tile, that dividing line can wander around however the artist wants, as long as it lands at the standard spot on the other edge of the tile.

That point on the edge where the dividing line lands determines how "thick" your tiles appear.  In the following examples, we have "bright" (green) and "dark" (black) corners.  Let's think of the dark corners as background, and the bright ones as foreground.  If the dividing line on the edge of each tile is very close to the foreground corner, then you get thin walls, suitable for something like a Pac-Man maze.  In a game, the player or enemy sprites would probably be playing mainly in the background area.

![Thin tiles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxzcsnfhiwggikikgc0m.png)

If the dividing line is right in the middle of each tile, then you have an even balance between foreground and background.  You might use this if the player can move equally well on both, for example, if your two types represent rock and grass, but this is mere decoration and doesn't affect gameplay.

![Medium-thickness tiles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nmmsddzoyydr2m27wb4.png)

If the dividing line is closer to the background corner, then you have a _lot_ of foreground area on each tile.  This makes fat, chunky foreground areas.

![Thick tiles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6jth6o57l784umi8w68.png)

These examples used [this Clean 2-Corner tileset](https://opengameart.org/content/clean-2-corner-wang-tileset).  But just to show that tiles don't need to be boring round-rects, I quickly banged out a little [island tileset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlsrfmaogg7zcz771sie.png) that looks like this (with indexes added):

![Island tileset with indexes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kn3t4gfivjhjxicmmlkp.png)

The same layout as above, but using this tileset, makes nice little islands.  Pirate game, anyone?

![Islands!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/37p22ypzhpesx36902bp.png)

## Auto-Tiling

The term "auto-tiling" has come to mean any scheme in which the player (or an algorithm) can easily paint with a tileset, and the appropriate indexes are automatically chosen, rather than the designer having to individually say "put tile 7 here, tile 13 there, and tile 5 next to that".

Auto-tiling with Wang 2-corner tiles is easy: you only need to realize that what you are painting are the _corners_, not the squares.  Because tiles are chosen based on the type of the four corners, what you do with the mouse is toggle the terrain type at the corners.  Then the code can recalculate the tile indexes for the four tiles around each corner.

**Try it!** [Mini Micro](https://miniscript.org/MiniMicro/) has a demo of this at /sys/demo/maze.ms, and you can [try it online right now](https://miniscript.org/MiniMicro/index.html?cmd=run%20%22%2Fsys%2Fdemo%2Fmaze.ms%22&info=maze).

## Tile Variations

If you use the bare minimum 16 tiles, and your tiles are meant to represent natural features like coastlines, grass, or rocks, then the tiling will become pretty obvious in large areas.  You can break up this obvious tiling by having multiple versions for each Wang value.  For example, you could have two versions of each, for 32 tiles total.  Then, when selecting tiles to display based on the state of the four corners, just add a random offset (0 or 16) after calculating your index as above.

## Other Types of Wang Tiles

As noted above, it's possible to have more than just two types for each corner.  3-corner Wang tiles would come in 3^4 = 81 variations; you could even go for 4-corner Wang tiles like [these](https://opengameart.org/content/wang-4-corner-tiles), which require 4^4 = 256 different tiles.

If your game uses a hexagonal map, you can still use corner-matching Wang tiles.  But in this case, 2 terrain types would mean 2^6 = 64 different hexagonal tiles.  (And let's not even think about what 3-corner or 4-corner variations would mean in this case!)

You could also consider edge-matching Wang tiles, where the two terrain types match up along the edge rather than the corner.  The trouble with this is, you have no choice about where the dividing line between the two terrain types falls; it _has_ to be right at the corner.  And if you have any visible "border" around your terrain, like the sand in my island tileset above, you won't be able to hide a visible notch at the tile corners.  So, edge-matching Wang tiles are not as generally useful, but they can work in special cases.

Finally, there's nothing that says Wang tiles have to be flat 2D images.  If you're working in a 3D environment, you can have fully modeled 3D tiles that fit neatly together, using this same Wang index calculation.

## Go Forth and Wangify

Wang tiles are a brilliant invention.  Now that you know what to look for, you will spot them in almost any game with editable or procedurally-generated maps.  And if you're a game developer (or would like to be one!), consider applying this technique to your own games!