Not Nano Day 8

I decided to tackle the chunk generation issue today. But first I looked into making text draw on the screen so I can debug easier without needing to refer to the console. This will also mean when it comes time to draw the amount of resources, for example, on the screen that I’ve already gotten the hard part figured out.

cocos2d text test.PNG
Info output showing details of a hex.

On to the actual hard part, chunk generation. Somewhere in my arithmetic I’ve got an error, which is causing chunks to be generated (and draw overtop of) existing chunks. I fixed one small bug I saw, but it was minor and didn’t seem to actually fix the issue. Dang.

While working on a fix, I implemented a way to determine all of the hexes inside the viewport, and I’ll probably update all of my drawing code to only draw the hexes that are actually inside the viewport. The algorithm finds the corners, draws a pair of line between each of the top two and bottom two corners, and then iterates over those two lines drawing lines in between.

cocos2d viewport hexes.png
Viewport is cyan rectangle, hexes outlined in magenta are the calculated ones visible from the viewport. 

The image above shows the hexes calculated to be inside the viewport. There’s some extra hexes on the sides and top/bottom, this was done to make sure that these hexes completely cover the viewport. Why does it look like a little too much extra? Because I applied the same amount to the vertical edges as well as horizontal edges, but the top and bottom should be half of the sides. For my current implementation, this would mean drawing 483 hexes instead of 1089 for the 1280×800 window I’m testing with.

Unfortunately, I wasn’t able to find the arithmetic error in chunk generation. The code all looks correct, but something is very obviously wrong. It’s probably a simple off-by-one error, but finding it has proven to be challenging.

As a very quick fix for the worst of the issue, I made it so that terrain tiles can’t be updated, only created. It doesn’t solve the root cause, but it does make the bug have a lot less impact. It’ll still be an issue when terrain chunks aren’t randomly generated but represent part of a larger land form.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s