Friday, November 19, 2010

Blue Skies v1.1

  - New Feature: Stars at night
  - Bug Fix: Flickering clouds

I thought I had the flickering issue fixed with an adjustment to my sorting function, but I was wrong.  As it turned out, the sorting has been fine all along.  Here's what the bug actually was.

Basically, the clouds move towards the camera.  As they get close they alpha out, and once they pass a certain point they teleport back to their distant starting position.  This gets us the infinite-supply-of-clouds look.

Cool, except that when I moved them back to the start I was moving them explicitly to CLOUD_START_Y.  Since their movement is time-adjusted based on the framerate, they'd gradually drift relative to the other clouds as fractions of a value got knocked off.  Eventually, you'd end up with two clouds really close to each other, and then you'd get a framerate hitch or something, and have two clouds get teleported within the same frame.

Once that happens, those two clouds end up sitting in the exact same spot forever, effectively.  The sorting code would work its way through the list, and occasionally slight rounding errors would result in them being sorted in a different order, then back again a bit later.  This is where the flickering look came from.  This also explains why I never saw the problem yesterday -- I was restarting the wallpaper periodically as I tested things, and it takes quite a while to manifest, somewhere around 20-30 minutes.

The fix for all this is to calculate CLOUD_START_Y - CLOUD_VANISH_Y and subtract that value from the cloud's existing Y origin.  This means everything stays spaced the same relative to each other, and the flickering doesn't occur.

While doing this, I added stars that fade in during the night, since I got that as a comment.  :)

No comments:

Post a Comment