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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static PointF[] GenPoly(double RadiusMin,double RadiusMax,int minpoints,int maxpoints,Random rgen=null) { if(rgen == null) rgen = new Random(); //select a number of points in the given range. int PointCount = rgen.Next(minpoints, maxpoints); var PolyPoints = new PointF[PointCount]; //calculate the angle between each generated point. float Angle = (float)((Math.PI * 2) / PointCount); for (int i = 0; i < PointCount; i++) { //generate this point, generating a radius between the given minimum and maximum range. float RadiusUse = (float)((BCBlockGameState.rgen.NextDouble() * (RadiusMax - RadiusMin)) + RadiusMin); float useangle =Angle*i; PointF newPoint = new PointF((float)Math.Sin(useangle) * RadiusUse,(float) Math.Cos(useangle) * RadiusUse); PolyPoints[i] = newPoint; } return PolyPoints; } |
Relatively straightforward, to be honest. It does go to show that trigonometry certainly has uses.