- Home /
Infinite terrain generation algorithm problems
I've been trying to track down a cause of an error in some code for several days now, and I just can't seem to find it. Normally I would try to find which variables are not as they should be, and then use the "wolf fence algorithm" to find exactly where the cause is, but I have no way of knowing when variables are wrong or not in this case. I've tried checking all the variables I can, and they all seem to be correct.
The problem isn't the wrong normals. When the player moves, new chunks are loaded, towards where the player is walking, making the world "infinite". The problem is the chunks are often being loaded... incorrectly. I'm not sure how to describe it, but the blocks are in the wrong places, making floating chunks, and large cliffs between some chunks. It's best if you just try it yourself by moving the character controller to new locations and watch how the new chunks get created.
Code can be found at http://pastebin.com/XBgb1kwy. To get it working, just add a character motor tagged "Player", and a gameObject with the "terrainGen" script attacked. You will also need to save the priorityQueue and chunkVec scripts as assets. No console output happens whatsoever.
Post it on a form such as answers.unity3D.com or unless you have signed some contract restricting you from doing this then I would ask other people who are working on the project for you. Else try to come up with a problem simpler to yours and post that on a form and see if it translate to your problem. Errors typically require fresh set of eyes to help you see.
Are you sure The community would be willing to do that? The script is pretty long (~800 lines) and I don't know of any way to simplify it more than it already is. Also, sorry for making this an answer ins$$anonymous$$d of a comment, but I can't, nor ever have been, able to find the "add comment" button.
Seems like you already have a good grasp of your debugging process, it would be hard to help you solve your problem with this information.
When you say you have an error - can you elaborate on its nature? Do you see anything in the console or not? If so, it will help a lot.
Since you did not mention it, I am guessing there is nothing in the console, but rather an unexpected behavior in your game. These are harder to find, but I can tell you that in addition to trying and fence the wolf, one other technique I have used in harder cases, is to start removing code so that you are left with only the essential bits that reproduce the error.
Sin/Cos/Tan and degrees/radians, double negatives, populating and Out of bounds arrays, ==0 (or other round number when its actually a float), parsing, dot product 90º, ROUNDING error, vector3.angle direction.
All good places to debug when looking for an error. Unity loves rounding error. It adds it in everywhere. ==whole number is a nightmare.
Tell us what the error is and we can all sit here shouting stuff out like playing charades.
Dropbox your code. I'll take a look at it. I'm sure others will have a stab too. Just don't recommend CODESTOR$$anonymous$$ING it onto a QA page :P
I might take a look at the code as well, if made available. Just remember to tell us what the problem is specifically. Searching for a non-specified non-syntax error in ~800 lines of code will not be succesful.
Answer by fherbst · Sep 01, 2013 at 06:37 PM
The only thing I can see at once that's missing is the normals of the cubes, thus the lighting is wrong. This can be fixed by simply moving the mesh.RecalculateNormals() call further down:
Mesh mesh = new Mesh();
mesh.vertices = vertices.ToArray();
// mesh.RecalculateNormals(); // this is too early
mesh.uv = uvs.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals(); // triangles are needed before normals can be calculated
mesh.RecalculateBounds(); // I would do this as well
Then it looks like this:
[OP said that the normals weren't the original problem]
The problem definitely lies within your GenerateChunks and ShiftLoadedChunks method (moving the player and calling Generate all the time works as expected). I would start by trying to restate your algorithm in a way that doesn't involve try-catch as integral part of the solution.
Also, I don't know why you need that priority queue and 1-s-generating-delay at all. I think you could just go through your chunks, delete the ones not matching the player position anymore, and generating new ones on the opposite side. If that's too resource intense, I would add exactly that objects to the generating queue and generate them one at a time after sorting by distance to the player (you seem to currently have quite a complicated way of doing that - you should think about your data structure).
[1]: /storage/temp/14963-screenshot01-09-2013+20.29.50.png
@felix: I've gotten rid of the try-catch blocks, and the error hasn't seemed to change. That still was a good suggestion though because the exception handling was expensive. SOme things I've noticed on the chunk generation glitches is that it only generates wrong if you add more chunks to the chunks-to-load queue while there are already chunks in it. So, if you move in a direction, then stop and wait for all the chunks to load before moving again, the error doesn't occur. Could that have something to do with it?
Cheers.
Like I said: you should think about your storage structure (right now, I find it quite inconvenient to access a chunk's position from the world array) and match the thing you're doing in the Generate method (which works fine) and the thing you're doing in the other methods (that produce glitches).
@felix: What do you mean by " the thing you're doing in the Generate method"? Both the Generate method, and the other methods use ChunkRenderer.render and assignChunk methods.