Generating a Random Polygon

May 18, 2015 - .NET, C#

I’ve been, as I mentioned previously, fiddling with BASeBlock recently and I’ve find a few little gems that are worth sharing discussing here.

One of the effects of using GDI+ itself for drawing the game is that all physics and game logic has to be handled by the game. Unlike modern game development frameworks, we don’t get to take shortcuts for things like physics.

This made the creation of the “Polygon” Block a rather tall order, primarily because the math involved is rather advanced, In my opinion. But, I managed to get it working. I also make use of polygons for the purpose of block debris- when a block is destroyed, a number of random polygon debris “breaks” apart the block. I had some fun with that and some blocks break apart differently, such as the “Brick” block which attempts to mimick the classic Super Mario Brothers Brick breaking.

But that isn’t what this is about. One of the requirements of the standard debris (as I call it) is the creation of randomized polygons within a given range of sizes and number of points. These are randomized to prevent them appearing to “regular” since they are used for debris particles.

For generating the polygons themselves, I take a rather straightforward approach. Effectively with a chosen number of points, I use the same logic that is used to generate a regular polygon but I also vary the radius used for each point, which creates a polygon that looks very much arbitrary.

Relatively straightforward, to be honest. It does go to show that trigonometry certainly has uses.

Have something to say about this post? Comment!