Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting 2D Tile based shapes to simplified Polygons

Tags:

c#

polygon

xna

tile

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.

like image 446
Marcos Alfonso Avatar asked Oct 23 '22 07:10

Marcos Alfonso


2 Answers

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

like image 192
Ivan Kochurkin Avatar answered Oct 30 '22 14:10

Ivan Kochurkin


The naive approach would be to simply step through every single tile and mark any transition edges into a polygon but you could reuse a common edge detection routine for better performance.

After that you'd probably be interested in tessellating those polygons so as to convert them into collections of triangles (making the later shadow/lighting/intersection math a lot simpler), the only problem in this case is if you end up with a concave polygon, but a decent tessellator should allow you to break it up into convex polygons. I don't think XNA has anything built in for tessellation so you might need to find a utility library to do it for you.

like image 39
ravuya Avatar answered Oct 30 '22 13:10

ravuya