- Home /
Infinite terrain generation algorithm problems
Notice: This question is long winded, and anyone wanting to help will need to dig through a formidable amount of code looking for an obscure glitch. If you are faint of heart, please turn away now.
I had asked a question that is more or less identical to this one a few months ago, but lacking a sufficiant answer and unable to find an answer myself after several months of on and off trying, I am here to ask for help again.
I am making infinite terrain generation, and have come across a problem. It is block and chunk based (think Minecraft), and it is supposed to make a mesh for each chunk by putting adding 2 triangles for every face of a block that is facing air. However, it does not do this correctly, and oftentimes renders blocks that are surrounded by other solid blocks. The only pattern I see in the rendering error are as follows:
If the player shifts positively in the x-axis to prompt new chunks to be loaded, extra blocks seem to be loaded below the player with the quantity equal to how many chunks the player shifted.
If the player shifts negatively in the x-axis, no blocks are rendered.
Any help would be appreciated.
I have used the following link as a guide to the scripts, reading through it may help you understand how my code works. Guide
Below I have the links to the different files in the generation (in descending order of importance)
ChunkRenderer.cs World.cs terrainGen.cs Chunk.cs B.cs BlockTypes.cs DirtyType.cs AirType.cs PriorityQueue.cs
Also, the website of unity answers seems to have a long running feud with me, and as such I often am unable to perform certain actions in posts, such as replying to answers. Because of this, I may post answers to this question that are really just replies to their answers, so please refrain from having the mistaken notion that I am making tons of new answers because I don't know how the site works or that I'm just an asshat.
Yours was an interesting problem. All I can suggest is making a test case with numbers you can work out and read off the values in your arrays to make sure everything is running as you expect.
@meat5000:
Well it's just really hard to deter$$anonymous$$e what the values should and shouldn't be, unfortunately. Also, I did make some progress since last time I posted this. I had simply wrote logX ins$$anonymous$$d of sizeX or something like that in this one method, which I had overlooked. Unfortunately, that only solves part of the problem, not all of it.
Oh wow, your code scares the crap out of me. All of your triply-nested loops are fertile territory for any number of copy-pasta or off-by-one bugs that are notoriously difficult to debug.
You need to start thinking in terms of GameObjects! Logical units of the game world that are responsible for rendering and managing themselves. There isn't any reason you can't have Chunk inherit from $$anonymous$$onoBehaviour and decide whether it's too far from the player to be seen anymore and things like that.
@flaviusxvii: You may be right about that, although for most things I think "thinking in game objects" would make it even more difficult, such as knowing when to shift the chunk array... unless you're saying theres a way to do this without even having an array of chunks.
Answer by Kyth'Pth3hk · Nov 23, 2013 at 05:48 PM
Ok, so I've finally fixed the error. Turns out the values of the chunk.blocks array was wrong, due to the makeChunk method failing to assign the values of the blocks before it rendered the chunk, since the chunks blocks are only assigned after the method returns, while the chunk renderer needs them to be assigned before they return. I just changed the method from this:
Chunk makeChunk(World world, int cx, int cy, int cz, Texture2D texture) { Vector3 chunkAssignPos = new Vector3(); Chunk chunk = assignChunk(cx, cy, cz, ref chunkAssignPos, true); ChunkRenderer.render(world, ref chunk, cx, cy, cz, texture, chunkAssignPos); return chunk; }
into this:
void makeChunk(World world, ref Chunk chunk, int cx, int cy, int cz, Texture2D texture) { Vector3 chunkAssignPos = new Vector3(); chunk = assignChunk(cx, cy, cz, ref chunkAssignPos, true); ChunkRenderer.render(world, ref chunk, cx, cy, cz, texture, chunkAssignPos); }
And now it works. Thanks to everyone for trying to help. Wow, it took what, 4 months working on and off to find this? I must be the worst debugger ever. XD
Your answer
Follow this Question
Related Questions
Flowing water in my voxel terrain 1 Answer
Tiled Terrain 0 Answers
Optimization problems 1 Answer
How to make a voxel terrain generate all around the start point 1 Answer
Unloading Chunks not working 0 Answers