I Made Minecraft's Terrain Generation in Roblox

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I've had exams for like the past month and I'm not gonna lie I've been working on the game A little maybe I mean you might consider quite a bit or a little I don't want to come off as like one of those Minecraft youtubers that say I did a little mining off camera when they literally finished the game off camera however there are in fact a few changes and I think they're minor and they're mostly little things you guys requested like for example you know how I said you gotta press W twice to Sprint well some of you guys commented you can do that with control so that works now in today's video I want to add terrain generation and building blocks but I can't add building blocks until I add terrain generation otherwise I'm just gonna have some really stupid code that I'm gonna have to delete anyways but I am pretty excited for terrain generation at first I thought like I'm not gonna be able to figure it out but I mean I've been researching it a little okay like I can maybe figure it out we'll see never done it before anyway so let me show you the stuff that I've added and you guys are gonna love this because literally been asking for this and I had already done it while you guys were commenting it so first things first when you're mining a block particles appear and this depends on the surface that you're Mining and if you switch surfaces it does the particles there pretty straightforward it's just particles which makes mining blocks a whole lot more satisfying this just uses particles emitters and it checks which normal you're on which is why I use the ray cats instead of mouse.target because it Returns the normal and the normal refers to the side anyway so that's that and one more thing is when you end up breaking the block there's also a break particle explosion you can really see when you're like mining grass right there and one more thing you may have noticed is now like the way it drops the animation is different and that's because I'm not using physics so it does look worse however it's less glitchy or buggy or like it doesn't do weird stuff like you'll never mind a block and see it go into another block and that's because I'm using tweens now instead of physics which essentially lets me control where it goes however what's really annoying about it and now I need you to pay attention here is the animation itself okay that didn't look bad is the animation itself looks okay I don't look bad either it's the animation itself doesn't look natural like right there that was like pretty triangular like it's supposed to be a curve right but it was triangular and the reason for that is because I have two separate tweens one to go up and one to go down I'm not entirely sure how I'm supposed to do it to get it to look smooth but I don't want to apply physics that's all I know physics are finicky and it's annoying to work with and you also may have noticed one more thing which I kind of forgot about which is animations they actually like look somewhat good enough actually they probably look good to you but they have their issues okay I know their issues there's a lot of work to be done here also when you pick up the block you also hold the block now and also has an animation when you're breaking stuff and you can punch so like if I hold my left click and here you can hear it punches once doesn't do it multiple times but if you're holding a block then you know you don't have to spam it so that's pretty good and now the biggest change that I made is probably the inventory system and I don't know if I've already added it but like you can see like yes I don't know if I added that in the last video but like you can pick up blocks goes in the inventory and everything and if you press e it opens it and now don't ask why this is broken I'm pretty sure this was working last video I somehow broke it I was trying to do something different but yeah and I was broken like I undid my code but it's still broken anyway so now you can move an item in your inventory which is pretty cool or right click it now it's two so that's pretty good and if you're already holding an item and you right click a slot it's going to place a single item there however it still has some limitations there are a few features that Minecraft has that this doesn't have and that's mainly because I don't even play Minecraft that much so I didn't even know these were actual features until I was playing around with their inventory system and one of those being if you hold an item you should be able to hold right click and then like drag your mouse and it should add a single one to each one of the slot and if you you hold an item you should be able to hold shift then like double left click on an empty slot which is very specific conditions and then all the items should do this essentially like they should all get combined into that one slot so yeah I don't have that I don't think should be too hard to add and honestly probably not going to add that in this video because it's just not a high priority I wanted to turn generation and couple more things so I redid the inventory using Roblox frames instead of using an image label so now it looks much better same thing with the Hub Bar and you may have noticed that the models are no longer glitching on top of each other they look pretty nice and the last thing that I believe I added is fall damage which was not too difficult to add so obviously it takes damage away and it calculates it it's actually a very accurate calculation very realistic to the game and on top of that when you take damage as you may be able to tell the camera shakes a little and the reason I'm trying to kill myself is because if you reach two hearts as I have right now it starts shaking Isn't that cool and one more thing now mining like flowers actually works like they also go up and down and however they don't spin and that's annoying me a lot and that's an issue with Roblox so if we look at like a piece of dirt like it goes up and down and Spins if you look at this it only goes up and down why is that you may be asking for this we're tweening we have separate tweets and what a tween is basically just an animation that we're applying so we have two separate animations once you go up and down and wants to spin around and around and the reason they're separate instead of being combined is because the one that goes up and down needs to reverse itself I think something along those lines like they can be combined that's all I know however when you're tweeting a model you can only tween the C frame you can't do like the orientation and position separately and a C frame combines the position and orientation set of Separation which is why I can only pick to either make them go up and down or rotate them and I chose to make them go up and down because why not and you can also hold them but I mean so that should be everything that I've added and also some of you guys have been requesting for me to show you some of the code I guess I'm not too technical oh and one more thing I mean I'm really starting to sound like those Minecraft youtubers saying they did the mining off camera but there's also particles for your footstep now I literally stole this code for my Would You Rather game but the color that the footprint like depends on the thingy you're stepping on anyway so that's all let me quickly show you some of the code if you're interested before we get into the terrain generation so the way I implemented the fall damage is well first of all there's an event called humanoid State changed basically just fires and events whenever the humanoids either falling running lands or anything like that there are a few humanoid State types you could use I was mostly interested in when they're falling and when they've landed so basically whenever the player Falls whenever it updates to Free Falling I register their y position and what that means is I'm just saving what their y position is now let me get paint so let's say a player is over here and now let's say his y position is 40. and if you don't know what a y position is basically you have an X Y and Z and this just refers the position and Y refers to the height so I'll say Y is 40 over here so what we're doing is we're registering the Y position in this case it's a 40 when they jump when they're humanoids they type changes to Free Fall we register this and then once their humanoid state type updates to being landed which let's say they land over here and let's say the Y position here is 10. what the code does is then it checks for fall damage and what that basically means is we're going to take both y positions and subtract them from one another to calculate the difference which in this case would be 30. and then and then what the function does is it calculates how high that is in block height and since each one of my blocks is three studs tall that means this is equal to 10 blocks and then on top of that we use a formula that Minecraft uses don't tell Microsoft which basically takes the block height subtract screen divides it by two it's right here and basically what that does is if you fell from let's say two blocks high or even three blocks high you're not going to take any fall damage but if it's four blocks or over then you will take fall damage and the amount of fall damage is however many blocks that was divided by two so let's say in our case it's 10 blocks right so we gotta do 10 subtracted by three which is seven and then we divide that by two which is 3.5 which means you lose three and a half hearts anyway so that's fall damage I don't really want to bore you with it yeah also have inventory which I mean is kind of messy and pretty long oh and one more teeny tiny thing I added a loading screen that loading screen is artificial the loading bar means nothing but I'll add everyone in the future anyways that's enough talking that's everything I've added another reason I didn't record myself adding those is because I think they're pretty minor a bit uninteresting and I did them during my exams like whenever I got bored I I coded when I got bored and it kind of accumulated like after I finished parallel one I was like hmm let me see if we can add particles and then oh I can add particles then after I was like let me see if I can figure out how to make an inventory system and then it kind of snowballed anyways I saved the best for last which is terrain generation so let's get started and that's gonna have to take place on the server side the way it works in Minecraft is it uses something called purlin noise which is an algorithm that generates this and this can be converted into this okay so I'm oversimplifying it the way Minecraft does it from what I could figure out is they have let's say one of these layers to a specific job so in this case let's say the darker it is the higher it is like the position of the blocks right black then like that could be a mountain if it's white then it's like very low so that makes sense right and what Minecraft does on top of that is it uses multiple noise Maps so you might have another one that calculates where each biome is and you might have one that calculates where water is and you might have another one that calculates where let's say Villages are like in this case we could be like all the really dark spots or Villages so really all they use is purlin noise which is what I'm going to be using which also allows us to use a seed and I'm not gonna lie I have no idea how to even access pearling noise in Roblox so um one second guys I'm gonna need to educate myself and I'll be right back so after a bit of work now I have the terrain generation working and let me show you you here it is now don't worry there are blocks below the base plate and it's just blocking the view so if we delete the base plate this is what we've got and now this was randomly generated I promise you I didn't build this I mean the fact that I built my own map in the last video is like such a big waste of time now that I think about it and I can also change the size of the map to whatever I want but right now it's 25 by 25 so let's turn that into a hundred and there we go I swear I didn't build any of this myself and I'm not gonna lie this is incredibly cool especially considering how little code I had to write I mean it was literally like 20 lines of code we have basics of Minecraft but there's still a lot more to add so basically what we're doing is we're using noise the Perler notes that I showed you earlier to calculate the y height or the Y position of the block right and what I'm doing is also storing this value in its own table and the reason for that is pretty interesting so let me show you right now that's the reason now we have grass so since the x and z position starts at zero and they just keep going forward and on top of that we have the Y positions so basically what's happening there is first of all there's a 25 chance of grass being on the Block and we're simply accessing like the y height at the x and z position and then we just add one to it so let's say the grass blocks positions at four and then the Y values at two and then the Z values at three and let's say that block is over here what we're doing is just adding one to the Y value which means it's going to be grass over here so the code is pretty simple and I believe it's pretty efficient also on top of that if you recall our grass block is not the same size as a normal block which means it was actually floating a little up and it was positioned weird and using some mathematical magic I've positioned them right on on top of that I don't know if you've noticed this in Minecraft but every single grass block is not placed directly on top of the grass block like if you look over here this one's a bit to the right side like it's not here but it's more here you know and every single grass block is just randomly placed on top of the grass block just to give it some variety and make it seem a bit more natural now there is one issue that we're gonna have to deal with or I'm gonna have to do which is chunk generation see there there's two options that I have here that I can think of one of them is making the map infinite and just keep generating or unloading chunks as the player walks that's not too difficult I can do that and now the second one which is also not difficult is I can pre-generate all the chunks on the server which would mean we're gonna have a finite World however I'm not sure how I'm supposed to store when a block was deleted or like a block was built inside of a trunk but I know how to do that if I already pre-loaded all the chunks on the server then all I would do is whenever a player mines a block or builds a block I just buy a remote event and register it on the server and whenever a player wants to load a trunk they would just request that chunk back from the server I hope you understand what I mean I'm not sure which one to do I don't know if the world should be infinite or not I'll probably just end up doing some research and if I can find any resources that can help me then I'll try to make the world's infinite otherwise it's probably gonna be fine eh but it's gonna be pretty big so like you probably wouldn't even notice it and one more issue see there are a bunch of issues with terrain generation there's nothing underneath the grass blocks which is not good however we can solve this pretty easily so as I told you before we're saving the surface wire like the Y position or for the surface is Right which means we basically know the point that's underneath the ground so we can Loop through our blocks and then just find where the surface level is and if the current y position since I'm looping from Max height to the minimum height is smaller then we'll add a dirt block now if I make the base plate transparent you can see that we have a bunch of dirt blocks but don't get too excited this is not perfect in fact it has its own issues there's a reason I only loaded a singular chunk instead of having a very big map which is the fact that it's already pre-loading every single block even though we can't see them it's like all the blocks over here have been pre-loaded which is if we load a very big math and this is going to be incredibly laggy and if you want evidence I am gonna do it and I hope my computer is not gonna crash and we just had a script timeout because of how much time it took um I I genuinely don't know if my computer is going to be able to handle this and I only made the grit size 100 by 100 which is not even that much that's like five chunks oh my God okay okay we are getting a bunch of Errors I should I should leave um anyway so all my computer is crying there there actually is a solution to this problem which is the same solution that Minecraft uses however I'm not completely sure of how to implement it because there are a few edge cases actually never mind now that I think about it I do know how to do it so I'm gonna try to explain this to you but it might be a little confusing because I'm not sure how to explain this really so let's say we have these nine dirt blocks right instead of pre-loading every single layer like this what we could do is just load the surface layer right and then once the player breaks this block let's say what it does is first of all delete this part yes as I was saying it will delete this part oh my God so it would delete this part and then what it would do is it would spawn a dirt block right underneath it so from your perspective this looks completely normal right and now let's say the player breaks this block again what it's gonna do is well it's gonna destroy this block and then it's gonna generate blocks all around it so from your perspective from the players perspective this is what they see but this is what it actually looks like from the outside so instead of having to load I don't know how many blocks it would have been we only loaded what's necessary which is really good for performance and I believe that system is called calling so I have to implement calling system and a chunk system and once those two are implemented we're gonna have a very efficient game and you may have noticed that there is a tree over here and yes I'm not gonna lie I I did implement the code for it it was not difficult at all so the thing about trees in Minecraft is every single tree looks a little bit different and what I mean by that is sometimes some of the trees are missing this part some others are maybe missing that part and this part some other ones are maybe missing this part they all happen to be missing one of these Corner blocks which is what I've noticed So I placed all the blocks that might be like missing into a canned lead folder and when the game spawns in a tree it's gonna delete some of them randomly now the code is not done yet however we can see trees oh God oh god what have I done why why what did I do no so over here now we have trees so I'm using purlin noise to generate the terrain plus perlin noise to generate the trees however for the grass I'm not using perlin noise as just random 25 chance on top of each block which might not be ideal honestly but whatever yeah now that I think about it I'm probably gonna have to switch it to Furler noise as well just so every world is going to look the same with every seat anyways so if you look at the trees this one is missing its Corner block over here so this is what the world looks like from a bird's eye view that's me and yeah like this tree is missing this corner block that corner blocking this one and each one is different and on top of using Pearl and noise there was another issue I had to deal with which is I I had the trees just spawning on every single block next to each other let's say this section is for trees according to our Pearl and noise algorithm then what it would do is it would place a tree on every single individual one which is obviously not what we want to have so what I did is I made a check over here to make sure that the block is more than 20 studs away so then we're not gonna have any trees clipping with one another and I don't know if you've noticed but like we've reached the point where the world is literally lagging me a little even if I haven't generated what's underneath it's still laggy because of how much there is going on which is why we need the chunk system like I'm running a pretty decent computer it's an RTX 27 yeah I'm still alive and also one more thing I need to add is water and sand and the way that's gonna work is I'm just gonna check the the height of the block it fits in a certain range then I'm gonna make it sand and if it's underneath a certain range then it's going to be water so so let's say this is all the possible Heist that my blocks can be at so the highest blocks will all be grass and then below those blocks will be sand and then finally all the blocks here are going to be watered and one more thing you could do and I'm not sure if I want to do maybe in a future video is the very very top the mountains and the Very tippy tippy top can be snow on top of the mount so I I believe I'm going to implement that next also gotta add randomly generating flowers which is going to be a similar system to the grass and oars there's quite a bit but like so far like the game is coming together I'm gonna take a little detour for a second so you may have noticed that I called my game mind blocks so a bunch of you guys pointed out that you can't have blocks or Row in your title so if you were to call the game row craft you'll get banned if you were to call the game mind blocks you'll get banned however I really want to keep the name Mind Block so this really annoyed me so here's my solution mind blocks so let's change the Game's name to a better name and there we go hopefully I don't get banned all right back to programming so after quite a bit of work I did manage to get some sort of Chunk loading system to work and honestly it has its own issues um like exactly that I I have no idea what's causing this so the terrain generation appears to be a bit messed up but like hey like we're loading in chunks they might be messed up chunks but like hey they are trunks I ended up figuring out the issue in my code and honestly I don't even blame my code the issue was that any block on the first layer as in y equal to one wasn't loading in and that's because for some reason Roblox treats all numbers within a table as a number but except one actually no I said that the other way around so Roblox treats every single number as a string except one was being treated as a number and since I was indexing the table with a string and one was a number I was getting nothing in return which is why it was loading nothing in anyways so that's out of the way and I've also kind of figured a few things out but a few things now I mean chunk generation now works so let me explain what's happening here alright so let's imagine a grid and the player is always the sensor of this grid and let's say this is zero zero so so let's treat each section as a chunk and the center chunk is going to be zero zero because that's where the players and then the chunk over here is gonna be one zero so these are the chunks that are around the player and the basic premise of what I coded is I get the chunk that the player is in and then I set a loop that starts from this corner and goes down all the way to this corner and I'm just trying to get all the chunks from here to here and that's how loading works and for unloading which also works by the way I'm just going through the loaded chunks and seeing if one of them does not exist in one of these chunks I mean it's kind of hard to explain but let's say that there was another chunk over here that I had loaded in previously so this is what the old chunks used to look like and this is what the new chunk looks like so obviously whatever was here is the outlier so we just delete it so if you look behind me you can see the trunks disappearing one by one and there's also a bit of a delay that same deal exists for loading chunks every 0.5 seconds I load a chunk in if I can load one in and also you can see it's sort of building the chunk block by block that's also for performance sakes I mean obviously I don't have to do it this way I could change it back whenever but at the same time I kind of I kind of find it satisfying so yeah now I can roam the world forever and there you can see where we started off anyway so the biggest issue that I gotta deal with is having this terrain generation occur on the client side So currently what's happening is that the server is the one determining what block should go where and then the client is the one that's putting where everything should be so now what I have to do is I want to put the terrain generation on the client side I mean it sort of already is but the way it's working is that the server is telling the client where every block should go but I think the client should be the one doing those calculations and then the client should ask the server for any possible changes that other players may have made because there's no way to keep track of those since we're only using noise and a seed so then let's say a player has mined a few blocks over here clients gonna ask the server for any changes that may have occurred within this chunk and then obviously the server is going to return these changes so then what the client's gonna do is going to load the terrain in first and then it's gonna check like oh what are the changes that have been applied to this chunk and then it'll apply and you know what that means that means we're gonna have multiplayer in this game so it's coming together which is pretty good anyway so now that that's working I'll see you guys in a bit so finally after hours of debugging I finally got quite a bit to work by that I mean there is grass now and also I fixed a few bugs and issues that like I didn't even talk about but there were issues okay anyways so now the way that the grass is generated is using purlin noise as well instead of using a random number generator and basically what that means is we're gonna have patches of grass instead of having it scattered around now some of you may like that others might not now we can play around with the values to make it more scattered so that's not a big deal and on top of that the main benefit of using patrolling noise instead of a random number generator is that given a seed you'll always generate the same exact world so I'm right here where my spawn point is and I look over here straight ahead so we have this little Hill with grass on it right remember and now the seed I'm using is 5000 now if you go back to where we were at that spawn by the way there it is it's still there now what I've been trying to figure out is how to add trees to the system and I did figure it out I I know how to add trees however the issue is I don't know how to get this more spread out like currently if I were to implement it and the code is working but it's going to crash my computer what it's going to do is it's gonna pick a patch of land and it's gonna make every single block of a tree on it which is not good so I'm not sure how to fix that in fact I I might leave that to the next video I don't know if I should end the video because I mean it's been a while I'm not sure how but I somehow figured out how to generate trees somewhat I mean it's not perfect like look at this I mean what's even going on here and also I think they're overlapping ah you can't even mine it okay I gotta be back in a sec and it works yes so you can mine the trees however there is one issue which is I believe if you have a bunch of trees together the blocks are gonna overlap yeah see they're overlapping there's like three leaves on top of each other that's not gonna be too hard to fix I just need to check whether there's a block there before placing a block there anyways I do think that's gonna be all for today's video I mean I'm not gonna lie I I did not think I was gonna get what I wanted to get done done today I mean I did say I'm gonna get Block placements done but I mean and there are still many improvements I can add to the terrain generation which of course I will that's all gonna be in the next video so stay tuned I'll try to have the next video out as soon as possible as this is literally the most fun project I've made see this is an example of a flaw in the code and I'm planning on fixing this anyway so I'll see you guys in the next video
Info
Channel: 2nix
Views: 64,850
Rating: undefined out of 5
Keywords: roblox, scripting, studio, minecraft, making, programming, learn, tutorial, coding
Id: MdVP65l-Oyk
Channel Id: undefined
Length: 24min 12sec (1452 seconds)
Published: Sat May 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.