Taking a look at my list from last time, I decided to make a dig at coming up with better asteroids and stars. The stars were pretty easy. I looked at a couple of games and noticed that most of the stars looked like a small circle with a pair of lines through it. Thus, I created a sprite that was like that, made it yellow. Then, I moved the "clearing the sprite collection" method into its own method and made sure that I added between 5 and 15 stars to the screen at random locations where the screen was cleared. Pretty simple.
The asteroids were a bit more of a problem. How to come up with a "realistic" looking asteroid that would be enticing without too much effort to render, thus slowing down the rendering speed. At the moment, a square asteroid isn’t too convincing, but it sure is easy to debug. So the first thing I did is to make sure that if I have the debug mode enabled, the square asteroids would remain.
Now for rest of the block. A nice empty block with this at the top.
//TODO: Make the asteroids more realistic looking.
Now its time for the thinking cap. What makes the asteroids from the original game interesting to look at? After I looked at a couple, it was the small variations that made it interesting to me. I thought it was the big craters at first, but it was more of the "snowflake" approach. By that, I mean that no two snowflakes are the same, so I would want to make sure there was a low chance that two asteroids were the same.
On doing some closer looking, I noticed that this may be accomplished by picking a random degree, add it to the current position, and set a point a random amount away from the center at that angle from the last position. In the square case, this would have been 90 degree increments at a fixed distance away from the center. It would have to be more than that.
It took quite a bit of fiddling, but what I came up with is as follows:
// The outer bound for our asteroids are asteroidSize, so pick an inner number to
// use that allows us to have interesting looking asteroids.
int otherSize = (75 * AsteroidSize) / 100;// Pick a starting angle and set the first point. Not that sin(0 deg) = 0 and cos(0 deg) = 1,
// so we can take a quick short cut for this first one.
int startDegree = 0;
int distanceOfRadius = AbstractGameForm.Singleton.RandomGenerator.Next(otherSize, AsteroidSize);
_asteroidPolygon.AddPoint( distanceOfRadius, 0.0f );// Keep on going until we get to close to the start, in which case, just end it.
while (startDegree < 345)
{
// Pick a number of degrees and add that to our total, and pick a length from the
// center to draw it.
startDegree += AbstractGameForm.Singleton.RandomGenerator.Next(15, 30);
distanceOfRadius = AbstractGameForm.Singleton.RandomGenerator.Next(otherSize, AsteroidSize);// Figure out the next point, at the specified distance from the middle and the specified angle.
float angleOfArc = FloatingPolygon.RadiansPer * startDegree;
_asteroidPolygon.AddPoint(
(float)(distanceOfRadius * Math.Cos(angleOfArc)),
(float)(distanceOfRadius * Math.Sin(angleOfArc))
);
}
This works in the following way. Pick a minimum distance away from the center, and use that with the "size" to determine the ranges for our distance from center. Our first point is at 0 deg, which means that X will be that random distance and Y will be 0. Then, as long as we don’t have a full range of degrees, iterate through picking a new angle to add and a new distance away from the midpoint, creating a vertex of our polygon at each point.
The numbers were pure fiddling. For some reason, 75% seems to work for my eyes and the minimum distance, and 15-30 degree increments seem to produce a smooth enough gradient that looked "natural". At least to me. I played around with little bits here and there until I came up with those numbers. Feel free to try the same thing and suggest other numbers.
Running it a couple of times, I was pleased with the asteroid shapes, but there was a bit of a problem when it got to the edge of the screen. As it approached the edges, it was fine. However, once one of the polygons points crossed over, it disappeared until the center crossed, at which point half of the asteroid appeared on the other side. Some simple calculations with boundaries took care of that, making it so that the asteroids appear as soon as any point crosses over.
This was not that difficult to overcome, a bit of simple math with the bounding circle and the polygon center to see if it crosses any boundary. Pretty quick work of that and then the asteroids seamlessly transition across the boundaries without a jump. Two checks for the upper and lower boundaries, and two checks for the left and right boundaries… well, actually three. We also need to account for the case where something crosses both the horizontal and vertical at the same time. Individually, these were taken care of, but that only redrew them at 2 places, leaving the 3rd corner mysteriously vacant. Easy change, and it was now covered.
Running the game with the asteroids set to 0 with no velocity proved interesting. For some reason, the bullets were not registering on any of the corners… so I will be checking into that next, and trying to figure out why that is happening.