Thursday, March 27, 2014

Interesting Moving Platform Bug

The saga of the moving platforms continues... or maybe is finally over. Only time will tell. After about 3 or 4 different versions of my moving platforms I feel like I finally had something that worked, that was clean, and that was generalized. I started setting up a series of test cases to make sure all my collisions worked as intended and discovered an entirely new bug:


All of my movement in the game (things like player walk speed, gravity, jump speed, etc) are defined with whole numbers. Even curved motion (like the ballistic trajectory the player follows when jumping to the side) is calculated with x and y components, so as long as the speed factors applied to those x and y components are whole numbers, your position is still restricted to whole numbers. I suspect that in many cases this is the best practice. It eliminates the need to deal with fractional positions in general, leaving the code nice and neat.

Where I ran into the problem is with Game Maker paths. Paths are defined in the Game Maker IDE as a series of points defined by x/y coordinates. In order to move between the points however, an interpolation is done resulting in intermediate positions that may have a fractional component. Paths can also be "smoothed" turning linear segments between points into curves, resulting in the same situation.

Luckily the solution to my specific problem was pretty simple once I did some pondering. At the beginning of every step the platform object rounds it's position to the nearest whole number. Only after it has done that does it moved the player. This means the player's coordinates are always (so far) whole numbers, even if the platform's coordinates are not.

No comments:

Post a Comment