Lets Code A Voxel Game in C++ and OpenGL - World Generation I

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Sofer on this box will game that we've been creating we have the ability to sort of create this little world and also break blocks and place blocks but the world that we've created is completely flat and as we all know the world isn't flat so we should do something about this and add some terrain generation to keep things simple for now the terrains gonna be creatives using a height map that is generated by simplex noise where simplex noise is an algorithm used to create these smooth little textures like the one you can see here but if we were to use this function to create Heights rather than colors for a texture you can see how it would create a smooth height map and the way this algorithm is used is you imp own X and Z value for example a position in the world and you get back a value between minus 1 and 1 the nice thing about this function is that if you input two positions in the worlds they are quite close together then the output you're going to get back for these two separate inputs will be pretty similar to each other so anyways to start off two functions are going to be created the first function will be used to generate a height map for a column of chunks in the world and the second function will be used to apply the height map to a chunk and this has been done separately because multiple chunks can soar share the same X instead position in the world and we don't want to generate the same height map multiple times the way this height map function works so first for loop through the block positions of some chunk column in the world and then get the noise value at those exact coordinates so right now this noise value is in between minus 1 and 1 but ideally we want a positive number so by adding 1 to it and dividing it by 2 we can instead make the noise value between 0 and 1 I then multiply the number by 5 to give it some height before applying it to the height map and this is done for every single block position in the world but unfortunately as you can see here this isn't quite right because the terrain being generated is kind of bumpy and this is because jumping from one block position to another even if it is just an increase in one is actually pretty big in terms of noise so the thing to do here is to take the inputs and divide it by some number to make the jump between the positions a little bit smaller and by doing that the bumps become a lot more spread out and therefore the terrain becomes a little bit smoother and if you increase the numbers by a little bit which will spread out the bumps even more and make the terrain a bit taller then the results can become pretty cool-looking but this doesn't look very realistic and that's because natural terrain tends to be very bumpy and unpredictable you know what we have here is just way too smooth so if we were to take the current world generation and sort of apply it to an image instead then you'd get a sort of overly smooth texture like this that looks nothing like actual terrain but if we were instead to sort of calculate the noise function at the same location multiple times with slightly different inputs then you will get multiple different outputs and then if you were to sort of combine these into one final noisy image then you'll get something that looks a lot like this an image that looks a lot more like in actual height map a lot more like real terrain and most importantly of all does not look like an overly smooth blurry image like we had before so taking that combining noise logic and applying it the actual game itself gives a result that looks something like this and as you can see it looks a lot better than before because it kind of resembles actual terrain now which is pretty nice but there's still only a single block type and that's grass and you know for more interesting terrain we need more block types so I feel like it's a good time to actually add support for that kind of thing so different block types have different data for example textures whether they can be collided with and what item they drop when destroyed so how can we store this data about the different block types we don't want to be strong the state on a per block basis because otherwise we'll have a lot of blocks showing the same data which would be a waste of memory so instead what we can do is store the data about each block type just once in some kind of array and then as mentioned in the first episode the chunks themselves just store a bunch of numbers which represent the block ID and these block IDs double as some kind of index into that block data array for example if I'm building the chunks mesh I need to know what texture is associated with each block ID so if I come across the number two I can go to block index two of the block data array and in grab all the information about the grass block included the textures so that's how we can store the block data but we also need a way to load the information about the different block types into the game and as I was planning to eventually add some kind of modding support for its scripting I decided to go with lure for loading us information where Laura's a language designed to be used alongside game engines and it's used by a lousy games such as factorio fable 2 and world Warcraft and excused by interfacing of a library written in seed that allows you to create a virtual machine in your existing code to run Lua code but to make my life a lot easier I decided to use another library alongside it called soul - we'll just solve a C++ wrapper around the original C API so using this library I created the function in the C++ code add voxel and made it available from the lower code and what this function does is add some new block type into the game so then for the lower code I'm able to call this function to add new blocks for example water grass or dirt and also define their properties such as their name and texture files and the reason Luis being used for this is because eventually the gameplay code is going to be programmed using Lua anyway and it's also gonna make it a lot easier to create a modern API for the game so anyways this Lua file is ran when the server started loading all of the different block types into the game storing it in the array as mentioned earlier when a client connects the server this block data array will be sent over to them who will then load the different texture files for all of the different block types and store the information about the blocks into an array exactly the same as the one on the server sides and doing it this way means that the person hosting the server is able to change the block types in the Lua script however they want to and all the clients connected will be synchronized at the same block data so anyways once the server has loaded up different block data into the game then the world generation code is able to start using it and this can be done by comparing the position of the block and the chunk of the height map so for example if the y position of the block is at the same position as the height map then you were set it to a grass block but if it's underneath that you'd set it to a dirt block or a stone block for example and the world generation being done using multiple brought types plus the client knowing how to texture them resorts in the world that looks like this but anyways as I'm sort of for use in this project as a way to learn networking I'm trying to keep things as simple as I can and so I'm probably not going to be adding the infinite terrain thing you would find in a typical minecraft clone but for off we keep the world as a fixed size but keep things interesting I for I could sort of embrace this limitation and make it so the world is generated as sort of a small island so this can be done by taking the cargo generation and multiplying each point in the world of another high map which looks like the sort of shape of an island and what this would do is multiply the heights at the edges of the world of 0 which would result in no high while the heights in the middle of the world will be multiplied by 1 which would result in the heights being unaffected and so taking the original height map and multiplying it by this island height map gives you this nice little island shape terrain thing and this is how it looks in game and as you can see it looks quite nice but the only thing that's really missing from this island right now is some kind of vegetation like some trees so this really takes two steps with the first one being where to actually place the trees in the world and the second step being to set the blocks that make it up so for finding where to place the trees I just went a very quick and dirty method of picking a random number whenever I place a grass block and if this random number is Abe of a certain threshold then I would generate a tree there so for generation this tree I picked a random number to be the high of the trunk and then use of for loops to place a bunch of wood and leaf blocks into the shape of a tree so anyways after doing this I'm placing a bunch of trees in my island I now have a nice-looking forest so even though we have trees now the world is still feeling a little bit empty and so I feel like the next thing to add would be tall grass and that kind of thing and the police when all these tall grass blocks can be done in exactly the same way that trees are sort of placed in the world where you would pick a random number and if this number is above a certain threshold then you would put a tool grass buffer so anyways after doing this I know how trees and tall grass than the and so at this point the only finish really left to add for the royal generation is biomes and this can be implemented by using noise except rather than using the noise values as height for a height map you instead use them as sort of a biome selection in a biome map so this means when generating the world we now create a bio map as well as the height map like we were before so the way this bio map works is when a block is being set in the world it would first of all check the bio map to see which biome that block belongs to and then place the correct block accordingly for example in a desert biome it would place a sand block but in a forest biome it would place a grass block and when it comes to placing decorations in the world for example trees or tall grass it works in pretty much the same way where it would first of all check the biome to see which decorations are has an in check to see like how often that decoration would appear and then place them accordingly throughout the world so for example in a forest biome you would place a tree pretty often but in the desert you would place a cactus instead but a lot more sparsely so anyways here's the final result with just two by Murnau a desert and some kind of grass slash wood band thing where each biome has their own unique decorations like tree and cactus and their own block types such as grass and sand so yeah the game now has support for the basics of well generation the only Phoenix really left to do for this is to find some way to take the world generation code and somehow move her into the lower code so it's a lot more easier to add things for example if you wanted to create a mod which adds a new biome into the game then you would very easily be able to do that so anyways that's the end of today's episode once again thank you for watching and I'll see you all next time quick shout out to my patreon supporters thank you killer crazy man Connor McNeely Timothy Gibbons Timmy Schroeder Allen Fernandez Ben sayers Michael Kirsh Lukas Taryn Berger Neil Blakeley Mona and Nate Brown thank you very much for the support so anyways once again thank you for watching and I'll see you all next time you
Info
Channel: Hopson
Views: 114,121
Rating: undefined out of 5
Keywords: Unity, Unreal, Engine, MineCraft, How, To, Tutorial, C++, SFML, Programming, Game, Dev, Sky, does, minecraft, pewdiepie, Multithreaded, tut, how, to, make, games, android, ios, apple, lwjgl, java, C#, rust, language, do, learn, program, game, for
Id: HPao-rNrH5Q
Channel Id: undefined
Length: 10min 58sec (658 seconds)
Published: Fri Feb 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.