Lessons Learned

I’ve been too busy this last week to make any significant progress, so instead I’ll spend this update sharing a few ideas I’ve picked up throughout the week.

There were some infrequent bugs in my collision resolution code that were quickly becoming a pain to test. Originally, I was just setting breakpoints in the problematic path and using the debugger to discover where the issue was originating. This worked well enough while the collision code was still fairly small, but as the code grew it became increasingly difficult to detect the source of the bugs. After some frustrating debugging sessions, I remembered something that could help: Unit tests. For whatever reason I’ve never had the experience of writing unit tests at school or work, so what better way to learn than through a personal project. Coding up a few unit tests quickly uncovered some bugs in my collision detection, and while I’m still working on ironing all these bugs out the tests did a great job of narrowing the search down to the right function.

My next point is the cause of these bugs: floating point numbers. It’s easy to forget that a simple equality comparison between two floating point numbers can fail because of the precision. In order to overcome this discrepancy, I’ve introduced an epsilon value. This value allows me to accept a certain error in my calculations so that even if the values are not exact equal, they are close enough that for the purposes of my game I will consider them equal.