Sunday, December 4, 2011

Randomized metroidvania update: simple fix

I decided to take a break from skeletal animation and focus more on the room generation for the Metroidvania game. While implementing architecture and art for underground caves, I think I found out how to fix the problem where transitioning into a new room would "bump" you to an unexpected place. I say that I "found out" instead of "figured out" because it was really just a lucky observation and a lot of trial and error.

Why did it take me so long to figure that out? Well, the problem seemed to only happen when transitioning from one room to another room, while on the floor (i.e. not jumping), where the new room's ceiling was lower on the world map than the room you were transitioning from. Confusing enough? Even then, it only happened some of the time. After a lot of logging, I noticed an odd pattern: the player's new y coordinate would be slightly higher than expected (by less than a single pixel). After all that, here's the fix:

private function checkBounds():void
    /* code to figure out the new room and new player.x and player.y values */

    if (toRoom.mapY > currentRoom.mapY)
        player.y -= 1; // gravity causes weird things sometimes....   

Although I'm glad to finally have this fixed, as far as I can tell at least, I'm still annoyed by it. What annoys me most is that I have no idea how I could have prevented this and no idea how to even figure out this fix. It was a lot of observation and trial and error. There were at least a dozen previous attempted fixes that didn't fix anything. I still don't fully understand why having a y coordinate of 144 is ok but one of 144.00115 causes you to fall to the room below and have a different x coordinate. I don't even know how to write a unit test for this: it involves Flixel physics, Flixel collision detection, and the constantly changing algorithms that I use to create rooms. Could TDD have prevented this? Is there a unit test that reliably shows what the bug was?


  1. I know what causes that bug, it took me a couple of minutes to figure it out.
    It's actually 2 different bugs that interact strangely. The first bug is, that the room transition "doors" aren't aligned correctly, so when switching rooms the character sometimes falls a single tile upon entering the room. If you then go back into the previous room, the character ends up standing inside a tile and what probably happens is, your collision resolving code pushes the character in a direction until the character stops colliding with a tile. The fix would be first to align all transitions, so that a solid tile if followed up by a solid tile, and as a failsafe don't allow a transition blindly, if the transition leaves the character standing inside a tile.

  2. @Gnaf, I noticed that too. Even when I changed it so doors are always aligned, it was still happening though. I think I'm going to redo some of the architecture generation when I get back to this project.