Monday, June 25, 2012

Kaiken Lives, Sort Of

It's been a long time since I last touched Kaiken. Whether it's because I haven't been in a programming mood, didn't want to deal with all the corners I've coded myself into, or sheer laziness I can't say.

Regardless, I've been working on it since last night and have actually made some impressive progress. Well, impressive to me and only because of the time I've spent coding nothing and the short amount of time I worked.

The biggest thing I fixed was the Box Collision detection code. I use it for detecting when an object is touching the "ground", or a platform they can stand on. I decided to just rewrite the entire function, this time for readability and not minimum line count. While I could now understand what the function was trying to do, it still wasn't working. I traced the problem back to the way I calculated objects' previous positions.

I had been "reversing" the player's momentum to figure out where they'd been last. However, since a collision with one block would change the momentum (to prevent them from smashing through a wall instead of being stopped by it), this became unreliable when touching two blocks simultaneously. The first detected collision would return "Hey, he landed on top! Better stop him from falling!" and the second would say "Well, he didn't FALL here, he has no downward momentum! He must have collided from the side!". This would cause the player to be stopped by blocks he was standing on as if he was running into them. Not cool.

I needed a better way to figure out the previous position of the player. What I needed was a variable that stored the previous position reliably. It would need to be updated automatically whenever the player moved, perhaps in the function that moved the player.

For a while, I pondered how to implement such a system. Then, I dug through the code only to find that I'd already been doing this, but my collision code wasn't using this information. Sometimes I feel like a dumbass. This instance went well beyond that.

So the collision detection pretty much works, but when colliding against the left side of a block, there's a little weirdness. At this point, I don't care; I'll fix it later.

The next morning, I implemented a Goal Block. When touched, a congratulation message is displayed and a countdown begins. At the end of the countdown the level ends.

I can't believe how easy it was to fix this stuff. Hopefully after work I have the time and inclination to keep working on it.