Search code examples
c#xnapolygontile

Converting 2D Tile based shapes to simplified Polygons


Tile based world converted to polygon shapes

As seen in the image above, I have a 2D array of tiles, each with 4 points in my game world. I'm looking for a method to convert these shapes constructed from individual tiles into simplified (no unnecessary vertices, only those needed to form the outline) polygon shapes.

I have been looking around, here and elsewhere and have had very little luck. But maybe I don't know the correct terminology to search for. Any help is appreciated.

Extra Info: I'm looking to use this to optimize dynamic lighting. If someone has a different approach for accomplishing fast dynamic shadows in a tile based world, that will also answer the question.


Solution

  • I suggest next algorithm:

    1. Storing all edge positions into 2D array (edge position is center of edge).
    2. Counting duplicate edges in this array (1 is no duplicates, 2 is intersecting with another edge. Other values are implossible).
    3. Picking first unmarked edge from array with duplicating count 1 (no duplicates) and applying simple recursive fill algorithm in certaing direction (Clockwise for example) until first edge reaching. All this edges will form one simplified polygon. If unmarked edge has not been founded, then GOTO 5.
    4. Marking all this edges from step 3 as used. GOTO to step 3.
    5. End.

    For more intuitive representation of algorithm, I posted an image below.

    enter image description here