I’ve run into trouble with enlarging polygons.
I am developing a map creator for a game. One of the map creator’s core functionalities is that it registers and stores the user’s touch coordinates and then builds a polygon using those coordinates as its vertices. My intent is to use this primary polygon as the area where the player can move in.
This means that I then need to build my primary polygon a set of walls to keep the player within the designated area. The “easiest” way to do this, in my mind, is to create a larger secondary polygon that uses the same vertices as the primary polygon, but those vertices must be offset by the desired thickness of the walls.
If everything went according to my plans, then I should have two polygons like in the image below. The primary polygons vertices would be {x1,y1,x2,y2,x3,y3,x4,y4} and the secondary polygons vertices would be {x5,y5,x6,y6,x7,y7,x8,y8}.
Then I should be able create walls for each side of the polygon by using the vertices of both polygons combined, for example, the top wall would be created by using the vertices {x5,y5,x6,y6,x2,y2,x1,y1}.
My problem, however, lies in actually creating the larger secondary polygon. In the images below,
-
I create a green polygon,
-
I then create a larger secondary red polygon (which is on top of the green polygon, turning it brown). At a first glance, the larger secondary polygon does seem to be created as intended, but a closer inspection reveals that it is actually slightly off.
-
When I create a larger polygon or a non-square polygon, then the error is multiplied to an extent that it can easily be spotted with the naked eye.
-
When creating a concave polygon, things turn for the worse and the larger secondary polygon no longer remotely resembles the desired outcome.
The best help I’ve managed to find so far has been from http://csharphelper.com/blog/2016/01/enlarge-a-polygon-in-c/ but I am not fluent in C# so I am not able to turn that solution to lua. I believe that I have an idea for how to accurately manage convex polygons, but most of the polygons in the game are likely to be concave, and I have no idea on how to create an algorithm that manages enlarging both types of polygons… so I just decided to ask here if anyone smarter than myself could help me out.