Well, the kids are having a fun time play testing the asteroids game as I develop it. My son in particular likes being able to blow up the asteroids, though he doesn’t seem to get that this game doesn’t have good guys and bad guys.
The game is pretty playable, with 3 lives, scores for the asteroids, full collision detection. Things that were still missing:
- ufos and satellites – these will be added later as I think these don’t add a lot
- realistic shaped asteroids – high up on the list, but square polygons are easier to debug
- hyperspace and/or shields – need to do a bit of thinking on this one
- stars on the backdrop – a little piece of fluff
- sounds for the actions – this will enhance the game, but I will wait with this
- extra lives – variations included extra lives when hitting milestones
- better restarting of level – right now, I restart the entire level if the ship is destroyed
- high score – some simple way to keep track of the high scores
- explosions for the asteroids and the ship – this one I decided to tackle
There are probably more on the list, but this is a good list for now. The explosions were a bit of work, so decided to tackle them last.
Explosions are something that add a bit of fun to the game, but trying to figure out how to do it was a bit of research. How should I make the polygons explode when something hits them? Looking at a couple of implementations out there, the best way seemed to be some spinning thing with each of the edges of the polygon.
How to do this? Once again, back to high school math. What we really want to do is to take the edge and move it away from the center of the explosions, and spin the edges and have them die out after a number of iterations. For debugging, it is really good that we still have our asteroids as squares, as it will make it easy to debug. To start with, no aging or spinning, just edges.
High school math to the rescue. It shames me to admit, but I was looking at this problem for a while, and it was when I was in the car doing an errand that the formula hit me. I had half of the answer right, as I was taking the average of both the delta X and delta Y, which got me the mid point in one axis, but the other was way off. When I used my debugging mode to find the bounding circles, it was way off, sometimes off the screen.
Then it hit me… I do want to use the average for the delta Y, which would give me an established point, but then I needed to solve for the delta X. When I was taking both averages, I was picking a “random” point that was definitely not going to be on the line. What I needed was something that would tell me how many X to add for my selected Y. What I really wanted was to find the slope of the given line… and slope = rise / run.
Plugging that in, there were still a couple of problems. As the squares are in use, there is a deltaX == 0 and deltaY == 0 case that appear when the square it oriented normally. Those are easy cases to figure out as you just need to take the midpoint of the non-zero delta. The other edge is simply deltaY * slope, which gives us a midpoint. I might want to change this later to pick someplace close to the midpoint to make it more interesting, but this works for now.
Testing this out, the explosions starts to work, the edges show up and stay there. Forgot to add in a velocity component. Not a big thing… set the velocity to the normalized vector from the middle of the exploding object to the midpoint that we selected. If we don’t normalize the vector, the bigger the object, the faster it goes, which is not what we want. Normalizing is finding out the magnitude of the vector (square root of the combined square of the x and y components) and multiplying both x and y components by the inverse of that magnitude.
Testing yet again produced edges that would travel slowly away from the object and eventually die when they left the screen. Easy solution here, give some sprites a maximum age and when they hit that age, they die out. On top of that, we add a rotation element to each and make sure it has a good spin to it.
After a bit of testing and fiddling, got it in place. Seems to be working pretty well, and my kids are loving it so far. Looking for a good place to post the code as it evolves, so hopefully that will be up somewhere soon.