Back to The Cursed Forest
#2

The Tree Rendering Bug Saga

Trees gave me more trouble than the rest of the game combined. Since they're billboard sprites and not actual 3D geometry, they come with their own set of problems that I didn't really anticipate. I captured gifs of most of these while debugging, so here's what went wrong and how I fixed it.

The Rising Tree Problem

When the player got too close to a tree, it would rise up off the ground. The closer you walked, the higher it floated.

Tree rise bug

The screen position was being calculated from the tree's base (ground level, y=0). The camera sits at about y=1.5, so the base is always below the camera. As you walk closer, the base gets projected further and further below the bottom of the screen. The sprite draws upward from that base by its full height, so you end up only seeing the top portion of the tree. From the player's perspective, the tree looks like it's floating upward.

Tree rise bug fixed

Fixed it by projecting from the tree's vertical centre instead of the base. That keeps the projection point near the part of the tree you can actually see. The screen Y is adjusted back to the base when drawing so the sprite still renders in the right place.

The Disappearing Act

Same root cause, taken further. Get close enough and the base projects so far off screen that the bounds check culls the entire sprite. The tree just vanishes. They still had collision, so the player would stop where the tree would be.

Tree disappearing when close
Tree close-up culling bug

The centre-projection fix solved both the rising and the disappearing. On top of that, I also had the sprite budget sorted by array index instead of depth, so trees would randomly pop in and out when more than 25 passed culling. Switched to depth-sorting the candidates and bumped the budget to 40.

Distance Rendering

Once the close-up stuff was sorted, distant trees started acting up. Trees flickering and popping at the far end of the render distance. Had to tune the LOD transitions so they didn't look jarring.

Tree distance rendering fix

Visual Oddities

Not every bug was a culling problem. At one point the trees just rendered completely white. No texture, just flat white silhouettes.

White trees rendering bug

Then there was this. The dithering pattern would stretch weirdly on trees that were close to the camera. The Bayer matrix wasn't scaling with the sprite, so up close everything looked blocky and wrong.

Dither scaling issue

What I Learned

Pretty much every tree bug was the same lesson: what I thought was on screen and what was actually on screen were two different things. The maths was usually fine. The problem was always the assumptions going into it. Wrong projection point, wrong sort order, wrong threshold for the wrong type of object.

Recording gifs saved me. Being able to scrub through frame by frame and see exactly what the player was seeing made it way easier to figure out what was going on. Way more useful than staring at numbers in a console.