Question

I am assuming this is a reasonably common problem and am asking for a solution which might be more academic in nature or a general equation used for general problems like this, as apposed to me hacking out what looks to be a very big spaghetti mess.

I want to generate the edges of my tiled world. I have a basic 8 edges. I have an array of ints which represent my tiles. 0 is water and 1 is grass. I would like to add my edges which transition between water and grass. My edges are from 10 to 17.

int edges[3][3] =
{
    {10, 11, 12},
    {13,  0, 14},
    {15, 16, 17}
};

The first thing I think is that this resembles matrices. I know that I have a 3 by 3 matrices object with my graphics library. Are there any standard operations with matrices or with masks to cleverly apply my edges to my tiles?

Is there a standard formula for merging edges? For example if bottom right overlaps with a grass tile perhaps I would just not change its value, however if bottom right overlaps with a top left I want to change that one tile to a grass(0). There are many more individual scenarios and the problem becomes complex.

Since it would require look ahead functionality to add the edges when I generate the grass tiles, I have my edges generate as an additional parse of my tile array. So I am looking for a solution where I can add the edges on to the edges of my grass patches.

This also reminds me of anti-aliasing, I wonder if there is relevant formula in that field.

Ideally the solution would be optimal. For example I would have some kind of mask like the one above and by adding it on to every grass tile in the array I would then get unique values for corners and sides such that I could translate them to edge images.

Was it helpful?

Solution

First differentiate between logic and visualization. It probably makes sense to have an array where each cell is either grass or water for the game logic (let's call it a). From that you create a second array to store how that cell is rendered and consider edges (let's call that one v).


To compute v from a you can look at the problem like this: for each cell v_ij in v you want to compute how to render it depending on a_ij and the values from the neighbouring cells.

Example:

Theses are the values in a:

0 0 0
1 0 0
1 1 1

How would you render the cell in the middle?

Without considering symmetry you actually have 2^8=256 different cases.


A good and fast solution could be to build a lookup table where you map each of the 256 cases to one of the edges you choose for that situation.

You can get the index into that lookup table by going through the 8 cells, thus getting 8 times 0 or 1, and setting this as the bits of an unsigned integer.

In the example above you would for example get 000 100 111 = 39, thus the 39-th entry in your lookup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top