It is a strange facet of game programming that when everything works, you feel awesome, but when a bug arises, particularly one you have trouble fixing, you feel like an idiot. It’s hard not to be convinced that all other programmers in the industry would have no difficulty solving a given problem, and would look down on you for being so flummoxed.
I swear I did not wake up today looking for an excuse to use the word “flummoxed” in a post.
The bug in question this afternoon was that the game’s enemy airplanes could get themselves stuck in a corner of a map. The way their collision normally works is, when they hit a wall, I take into account their angle relative to the wall and give their angular velocity a boost towards the direction away from the wall.
Problems arise though when flying into a corner, because it’s possible for the airplane to bounce off of one wall, then hit the opposing corner wall before turning enough to be given an angular push away from the corner. Instead the angular push brings the airplane back into the corner and the airplane gets stuck.
My first approach to solving this issue didn’t work at all (the idiot phase of the process), and is probably not worth mentioning. I find it helpful when designing program logic to physically write down what is going wrong, and how things would behave if everything was going right. That led me to my solution, which was to implement a timer for each airplane that tracked how long ago it hit a wall.
Since it is highly unlikely an airplane would hit another wall within a couple seconds unless it were in a corner, I check the timer for that (the timer gets set to zero each time the airplane hits a wall). If the timer is under a limit of about 2 seconds when a wall is hit, I skip the normal angular velocity push, and instead give the airplane a push in whatever angular velocity it currently has. This causes the airplane to keep turning a single direction until it exits the corner.
It works great, and seeing it fixed made me feel awesome.
Of course, I realize any programmer with some AI skills could probably prevent the airplane from bumping into walls in the first place. So I’m back to feeling like an idiot. Damn.