I'm currently working on taking a grid of tiles and creating large convex hulls out of contiguous "islands" of tiles in the tilemap.
This is primarily to optimize the physics engine's work. Each tile can have a unique collision polygon, and what I would like to do is merge all of the vertices for a contiguous group of tiles' collision polygons into one larger polygon. To accomplish
this I realize I need to identify the "islands" within my map, and then run one of the convex hull routines to shrink wrap a new convex hull around the point cloud consisting of all of the island's collision polygons.
To identify the "islands" I've begun working with the polygons from texture tool. I create a fake texture where every collidable tile in the map is an opaque pixel and every non-collidable tile is transparent and then I run the PolygonTools.CreatePolygon
method and pass it the fake texture. This returns a list of vertices that
almost identifies the regions perfectly. The problem is, it seems to have some sort of threshold for combining vertices that are close together. Here is a picture of the result:
The red lines in this image represent the polygons returned from the CreatePolygon call. You'll notice it works perfectly for the rectangular regions. It correctly outlines these islands. It
almost correctly outlines the more awkward shapes, but you'll notice it seems to merge some tiles together as if they are within a certain threshold. I've played around with the hullthreshold parameter with only minimal results. It did
greatly improve when I lowered it, but it seems to have gotten to a point where any lower doesn't matter.
If I can get this working, I would go through each polygon and add the collision polygon of each tile intersected by the outline to some big polygon soup. Then I would run the ShrinkWrap algorithm on this soup to produce a final, simplified convex
hull of each "island".
I was wondering if there is any way to improve the output from the CreatePolygon method. I need it to make a perfect outline of each of these islands (although it is so close to being perfect). One option I was considering is to "enlarge"
my texture by making every tile correspond to four pixels, but I am unsure if that would make any difference. Does anyone know of perhaps a better way to do this?
Thanks for your help and for a great physics library!