Hello and welcome to video six of our python uh minecraft version uh yeah video six so first i’ve gotta remind myself of where we got to last time and in this video what we want to do is create an infinite terrain using our system or developing our system from what we’ve got already So uh we’ve imported a giant model we’ve got our terrain generating using purlin noise and i think do i press f yes i can press f to then build blocks i think this is the last thing that we did oh yeah we can use the um number keys to switch between block types And yep i can also kind of like mine or at least delete the uh blocks that we’ve created individually oh yeah and we’ve also got different colors for different parts of the terrain okay so that’s where we got to last time what i want to show you Is this different system that i’ve been working on apologies it’s been a week or maybe more than a week since this video but i’ve been working on a few things and i’ve struggled to get time but here we go here’s my kind of updated behind the scenes Version which looks a lot more like classic minecraft and good news is wherever we walk we can now generate terrain and in fact if i just stand still you’ll notice what happens is the terrain generates in a circle around around the player or what i like to call the subject The person’s like position and notice i am videoing but we’ve got somewhere between 30 and 60 frames per second what happens in terms of performance actually is that um when we’re generating the terrain as you might imagine uh the the frame rate drops to around 30 although it’s holding now to to 60. Um but we can build in things like and i can press g and that turns off generating the terrain and we should get nicer performance in fact it’s very silky even though i’m recording which i’m really pleased with and so now technically speaking we could Keep on walking for as long as we like and we’ve got that in infinite terrain so exactly what we want and what i was really pleased with which we’ll also do in this video i think i’m planning is we can actually design a more sophisticated texture for each of our blocks To make our terrains look a lot better and i’m not great at texturing and i only do this one in a hurry actually i spent a little bit of time with it i’m not really experienced um but i’m quite pleased with that like first go at least anyway right So let’s close this one down and what i had yeah i had a couple of comments already so swathi chitwani has asked yeah hello please make a video on infinite terrain generation and i think i said it about a week ago i’ll do that in the next video so here’s the next video Thank you swathi for for watching and um i’ll try and uh be fairly clear in my explanation and anushka sorry if i pronounce your name wrong sorry um or kale why do you say carly maybe it’s kale anusha can i call you a noosh thank you anush um Yeah doubt from the tutorial when we get to the area where blocks are there are invisible blocks can we get blocks there so hopefully what i just showed you is exactly what you wanted it’s what i want so we can go anywhere and create an infinite terrain so that’s What we’re going to aim to do um and when you said there are invisible blocks if you remember they are the shell blocks where are we the shelley’s three by three grid i think is perfect by the way my experiments behind the scenes three by three is the most efficient way Of doing it um so let’s keep that number to three so nine that will generate a grid of nine blocks altogether three across three down um in terms of rows and and columns and so this year generate the shell it’s creating yeah those invisible cubes with colliders where we can stand Right let’s let’s set up our files so what i suggest you do is not change everything in your like most up-to-date file if you’ve been doing that way but let’s copy everything from tutorial 5 and we’ll make a new file and then just in my ide here i’ve just Got to select python there we go paste all of the old code and let’s call this tutorial 6. obviously you don’t have to call your tutorial six but i’m just marking it and then let’s save that in the folder we were in before so i’m in my tutorial folder yeah so i’ve got Tutorial one up to five and this will be my tutorial six so let’s just call that oh i can’t select one i’m gonna have to type it out fair enough right pi craft underscore top underscore six save there we go so now we’re happy we can make all the changes and all the Mistakes that we need to make and we can go back to tutorial five in case everything goes wrong um also we’re gonna be reusing a lot of this code anyway so what’s the general idea how does this work i’ve got another piece of code here that will just give us an idea Right hopefully you can see this oh wouldn’t i turn off gravity whoops i just glitched through the terrain anyway let me just turn off gravity so for my subject or my player’s gravity if you set that to zero and then re-run it we won’t drop or we shouldn’t drop To the floor and i can just look down so this is uh the new way that i’ve i’ve worked out to generate like an infinite terrain we’re going to have um a swirl kind of unravel from the the player’s position at the center and so all we’ve got to do is work out What is going to be the position of the next um tile if you like as we’re as we’re going around so let’s go and do a little drawing so we know what on earth is happening and i just found i didn’t realize this existed you realize this existed If you put jspaint.app you can use like the old style of paint which is amazing i haven’t got a mouse i’ve just got a track pad apologies if this doesn’t go very well but what we’re going to do let’s imagine that that is our first piece of terrain or our first cube In fact so we’ll go from like a single block this time we won’t have grids we’ll have blocks and the next block that we want to build is going to be up here and then the next one is going to be there and then the next one there And then this one’s going to go down and then left and then left again and now up one two and three and across one two and three and now it’s going to be down four so it’s going to keep moving like that so that’s our first one number two Sorry about my handwriting number three number four uh no sorry god you’re there good it sorry nobody number three number four let’s just do number five so we’re kind of on to the next like section and so on so it’s like a kind of a swirly system now What we’re going to do i found well i found two algorithms of doing this and there’s a i’m sure there’s a much better one to determine the exact position that you need to be in i’m just going to use polar coordinates and it sounds complicated but it isn’t It’s just two lines of code to determine the x and y position of our um of our cube so we position each cube and then we decide okay what position are you at what height do you need to be just using the purlin noise that we’re using before now to also make Our terrain look a lot better we’re going to run two passes of per limb noise over our terrain okay so i think we’ve got the basic idea of what we want let’s go and start the code hopefully i’ll remember to put like a little link to to the beginning so we’ve been Talking about 10 minutes already but let’s go and uh do the code now so make sure i’m in my latest one so let’s go and uh delete all the stuff that we don’t need right we’ll start at the top so we need our cena window color we can keep the Same we don’t need the exit button visible uh previous time equals time now leave all the fog ah our textures we’ll leave for the moment um oh our bte system we’ll leave all that there so this is the building system uh build tool can stay and generate subset so we’re going to create Essentially a new generation function so we’ll call it gen uh sub or i guess we’re not creating subsets yet we’ll just call it gen terrain there we go generate some new terrain oh talking of subsets let’s just go back to our diagram what we’re going to do is something like How do i make my um line thicker there we go let’s do it in blue something like that so what will happen is the our algorithm our functions will keep a track of how many um tiles that we’ve laid down let’s say we’ve put down seven it will then Make um it will give it sorry it will make the parent of each of those tiles um a subset and then that subset will combine all of those into one and then this next load of tiles or the next seven i don’t know if this is seven it’s probably about six annoyingly They’ll be the next subset and then go pink and then the next slot will be these guys and so on and so forth so just as before we’re using subsets to to make sure that our program runs nice and smoothly um we’ll be doing the same again but We’re just going around in this different pattern um also then what we can do when we have a number of subsets together we can then say let’s say we’ve got i don’t know 100 subsets we can then say combine all those subsets together and so again we’re reducing the number Of objects we’ve gone from like one two three subsets to just one massive piece of terrain um it’s got thousands of um cubes in that the program can just think of as one great big model which you can deal with a lot faster so that’s what we’ll do so we’ll have We’ll have some terrain we’ll have a um a list of terrains which will be combined subsets then we’ll have our list of subsets just like we did in the previous one and then we’ll have a list of sub cubes which i’ll just call cubes here so it’s just three lists that kind of Nest into one another sub sub cube will go into a subset and each subset will go into um terrain right let’s go back to the code okay so we’re going to have a new function called generate terrain um let’s go and find generate subset i found that down here It’s around in my code 125 again there’ll be links to all the code in the description so um let’s just gut the whole thing this is really scary but it’s all right we we we’ve got the code back there okay there we go and we called it gen terrain there we go okay So let’s begin working on that so what we want to do first of all is go and get a cube and put it in the correct position so um we need the current like sub cube that we should be working with so we’ll call that current so we need a global Variable that we’re dealing with called um current sub cube i’ll just call it current cube there we go and we’ll make current cube equals zero and here are our ghost subset cubes so how many do we want let me just go back to my sample code that’s working and already prepared Number of sub cubes 16 i’ve gone for so okay 16 sub cubes and it looks like i make 420 420 subsets each with 16 sub cubes in before combining it into one large thing okay so we need 16 or i should rather put in here um number of sub cubes I’m going to keep all my variables the new variables i’m creating down here so current cube is zero number of sub cubes will be 16. and yeah it wants to be a cube and will also disable i found that this is a little bit better for performance as well So we’ll just disable our sub cubes there okay um and i think does my i don’t know if this variable needs to appear up here before it appears in the code let’s just put all that together so all of this kind of stuff like the purlin noise the terrain terrain with Subsets and all that kind of stuff i guess is around line 100 so i’ll keep that all together but i’m just going to separate off the new stuff that we’re creating in fact let’s just comment new variables smiley face there we go okay so we’ve created our invisible Um cubes they’ve not got colliders and they’re going to be used in our generate terrain function so the first thing we want to do is decide where to place um new terrain cube exciting so this is where we’re going to use our polar coordinates so we’ll say its x position wants to be Some origin point and the origin point will base on the um subjects the player’s position but let’s keep that separate to just we could just say subject wherever they are x but maybe maybe we want to let’s say stick the origin point at where the player is to begin with and Then if the player wanders off we still want to like grow that position from where the subject used to be and maybe later on we want to say okay now once they’ve moved 10 steps now start growing the terrain over here so it’s the origin is based on the Subject position at certain point but it doesn’t track it all up all the time by all means play with that and you might have different effects so it wants to be origin we’ll just call it x plus um plus and now we’re going to do sine Right we need the sine function which we can get from numpy so from numpy import sign and while we’re here we’re going to need our other trigonometric function which is cosine there we go and a trigonometric function like sine which just generates a wave it takes in a an angle or what’s usually Called theta i’ll call it theta and basically that just describes going around around around a circle and then a sine wave will just go up and down up and down like this um anyway this will give us essentially because we’re moving us in a circle but we’re trying to find grid positions The sine and the cosine function or rather these polar coordinate lines are going to work out our position on a grid if we’re kind of moving around in a circle that’s what’s happening um and also the sine function will take radians not degrees so we just have to use the radians Function there so in in brackets within brackets and radians is not showing up because i haven’t imported that from numpy input radians there we go and that function just converts from degrees into radians and the final thing we need to do is multiply this by the radius so Far what i mean by the radius is the first step the radius is going to be zero it’s just no diff distance away from the first cube now our radius is going to be this amount to cover all these guys and notice that we need to move about 45 degrees Every block and so this is our first radius and so if you like it’s 0.5 away or we could say one block away or one unit away and then the next radius let’s go to another color what will yellow look like our next radius will be um i’ve drawn this really badly Something like that and then notice that our angle start at zero the next one will be there so it’s exactly half the 45 degrees this looks like a complete mess now but we’re getting the idea okay uh getting a little so we need a radius we’ll just call that Rad and now our line is done so our x position is there so we need to also take in our theta and our current radius and again we need to go and create those so we need a theta we’ll start at zero and our radius will start at Zero for the first uh block right at the center okay oh i didn’t create our origin um in fact let’s go and create the origin where we create our subject and we can say um origin equal origin origin equals uh subject dot position so i think that’s going to be a vector three Whoops a vector3 object so essentially it will have an x component um a y component and a z component i’ve got to make sure i get those the right way around actually yeah the x and z that i’m referring to okay so uh yeah so our origin From the beginning of the application is mapped to or copies the position of our subject so that’s fine i went too far where are we there we go lovely so nothing’s on the line nothing’s angry so far so good news is we’ve done the most complicated bit um So the z position wants to be the origin dot z plus cosine of theta multiplied by our radius okay um now we only want to go and move one of our sub cubes into that position and then go and find its height to start generating the purlin terrain if there isn’t Already a piece of terrain there because you can imagine you could walk all over the terrain come back and then you might start generating or triggering generating some terrain so what we want to do here is just check there’s nothing there and then if there isn’t we can go ahead Um if there is something there what we want to do at the end of this function is to say well increase theta start going around the um uh the radius of what you’re trying to generate okay so so we want to ask um we’re not asking if that if there’s anything there Now the way i got around this was using a dictionary in my um code and i haven’t used a dictionary up to now in these tutorials good news is they’re very simple and really good news they’re very fast they’re faster than lists so i think this is a perfect time to use one Let me just go and find where my dictionary is there it is so we’ve got dictionary for recording whether terrain blocks exists at location specified in a key so i’m going to copy all that put it into my code next to my new variables logo dictionary for recording whether terrain Blocks exist our location provided key so if you’re making a list you’d use the square brackets making a dictionary use the curly brackets or braces and basically inside a dictionary you have a key and then a value and a key would look like something like this i could say um i don’t know Mom and then i could say um she’s great for example comma and then the next key we could have dad um is that pretty great pretty great and so on or more like more realistic for us i’m going to record an x position and then it might be something like At position 3 and then the z position that may be positioned minus 56 and then if that block exists i could just say something like high or i exist or underscore or zero or whatever i want probably the smallest thing so maybe just i something like that And so what you can then do you can then um say sub uh dictionary dot get and then you put the key in here for example x three z um minus 56 now that will return none if it doesn’t exist or it will equal i if it does exist i.e if I’ve recorded this key and this value which i’ll do or will do in the generate terrain function so if we place a new piece of terrain we’ll record what its position is as a key and a value and um python is incredibly quick at looking through all the keys and just Like going straight to the one it needs so that’s how that works and at the minute we don’t want anything in there because we haven’t created any terrain so we now want to say if um sub dictionary dot get and so here’s where we want to do that um we want to say Um x plus um x so this x is just the letter x sorry i pointed to the variable i just mean the letter x because it’s a string whereas this x is actually that variable so that’s going to be a number and so what we’re doing is concatenating A string so we’re building up that code like x what do i say x3 z minus 56 for example something like that so to make this a string i just need to wrap that variable in s t r okay so what uh then i need to plus uh zed And then i’m gonna add whatever number we’re at here uh with string make that string and just put in then the whatever number we’ve got z there there we go so if that equals um i um well actually we’ll say none then we can place some terrain there What did i do in my version i’m intrigued whether i says does not equal none or sorry does not equal i or whether i just put equals none we can also have a look at my um so x is cosine yeah we’ve got the radians lovely sign string oh does not equal i I’ve gone full let’s do that because i know that works it does not equal i so let’s just label what’s going on so check whether there is terrain here already dot dot so now we can go and grab our sub cube and say what position you are so we can say um sub Cubes um current cube dot x equals x and at z position it’s z lovely and oh yeah now we want to record this excited i’m going to record our first dictionary kind of entry so we want to say and then in square brackets this is how to like assign A value to a new key we can just copy all of this thank goodness this is our code so that will generate our new key our unique key because nothing exists there yet at this this string um you just write equals so the assignment um i there we go so now something exists So if i try and place another block at this position it will say i and so it won’t run this piece of code it won’t move the sub cube it will go on to the next bit i.e is going to go down here else do something and since we’ve got This else here you’ve got to write something in the next indent block so you can just write pass and that will um that will suffice although i seem to have an error for some reason okay save that and actually i want to know what have i done wrong there’s something mysterious here Maybe we’ll come back did i write um i wish i had like live comments and i was doing this live and you could tell me in the comments what am i doing oh no else no that’s fine let me just have a look at my code else pass Maybe i’ve just put it on the wrong no i haven’t oh this guy was on the wrong indent well done if you spotted that 100 points to you very good okay okay um let’s just put in a comment so we know what this bit of code means so um there was terrain already There so uh continue Rotation to find new terrain spot and we’ll do that at the bottom so um essentially we we just want to say um theta plus equals remember 45 degrees from my amazing diagram in the red so that first iteration wants to be 45 degrees and the next one wants to be half that So divided by our radius and i suppose our radius to begin with is zero so we don’t divide by zero um so we can only do that so we’re gonna if radius is greater than zero then you can in then you can rotate or maybe we should uh no we shouldn’t Increase our radius here and then divide by radius now that was safe um so first it’s going to be one so yeah it wants to be by 45 by one okay um we also want to ask um if theta is greater or equal to 360 degrees so we’ve gone all the way around We want to say theta equals zero again and radius wants to increment by one so we want to increase and we can fiddle around with these um if they don’t work so essentially we’re just like increasing that radius going around the next set of blocks increasing it again um Okay right what else do we need to do with our piece with our sub cube oh we i guess we want to say first of all sub cube current cube enable to switch him on sub cubes and we want to determine his color and things here Oh and we need to know his height that’s the crucial thing isn’t it and we need to um parent him so let’s do that sub cubes current cube parent equals sub uh subsets and we need a current subset let’s just call that cs so you know what no i’ll call it current subset To match everything else current subset oh does that actually exist current subset oh we’ve already got a current subset brilliant so we can keep that you have been promoted to the new variables put that in there so zero brilliant so later on if we’ve collected enough sub cubes then We need to combine the the subsets i guess we do that in here as well um let’s just borrow from my old code my prepared code so i enable the sub cube i parent it we’ve recorded its position um yep we put it in the right position we’ve got to Find its correct height um we want to map it out the color oh and then we can disable it afterwards so then we’re increasing that oh we need to increase to the next um subset oh and here we do do the com combining uh else pass Okay right so there’s just oh the re the radius limit that we need to look at as well okay let me just i just need a little pause a little break to collect my thoughts okay we’re back we’re back so what did i need to do i’m supposed to have prepared myself so Um okay so still within this if we’ve used another sub cube we want to say current cube plus equals one so go to the next sub cube and we want to just before that actually we want to disable you you’ve done you’ve done everything we need you for Again just switching them on and off these objects or entities can help performance obviously we just use the cube to generate some vertices um to generate the the model that we see of the terrain in front of us and then after we do that our current cube um Might be the maximum that we’re allowed ier16 um so that we can generate our sorry we can combine our first subset so if current cube number equals number of sub cubes then subsets um current subset combine and auto destroy wants to be false so we want to keep all of our sub cubes I’m just thinking do i need to do do i need to disable it after i’ve done all that no that looks like i’ve uh sorted that out oh and it looks like i then enable this subset um enable so i must disable the subsets to begin with So we’re going to switch that on in a second so i sort that out in a second so then my net uh current subset needs to go to the next subset ready to gather it 16 sub cubes next time around right current subset is not being recognized now for some reason Maybe it’s not a global variable yet current subset is that a bit happier yes that’s happier lovely um right and we need to go and switch off the subsets or check that i did that ah here’s our empty subset so our empty subsets um we want um i’ll call it num subsets Because i’ve called it num sub cubes and that wants to be 420. uh yes so that’s a lot of subsets isn’t it and so that’s going to be empty its parent is the terrain well we’re going to have lots of terrain objects so we have to take out that And we want to disable it to begin with that’s why we came over here in the first place so first big change we’re going to have a fixed number of subsets there then we’re going to disable it and we’re going to take out its parent we need to set the parent um Later on when we finish the terrain i guess we’ll call it or when we combine into a terrain uh we’ll uh sort out so okay let’s return to our our function here and i want to get this running as soon as possible so we can like sort out any bugs Right we disabled our queue we haven’t given him a a color yet oh important thing is then we need to set its height so we want to do that before disabling it and things like this so um sub cubes current uh cube dot y equals and usually here we put in um the Purlin noise algorithm oh sorry function the noise there we’re not going to do we’re actually going to create a new function so that both the the generate shell and this function are using just one piece of code it’s bad practice to have this piece of code doing this the same Job as another piece of code if you want to make changes then you’ve got to remember to change in those different places also we’re going to make it a bit more sophisticated so let’s call that um jen perlin drain it generate some perlin and we’ve got to pass in there x and z It needs to know where we are right so let’s just above here above generate terrain we’ll call it jen perlin and it’s going to take in underscore x underscores it um i just like to call my parameters variable names with an underscore just so i remember that um function parameters There we go so i’m gonna now go and copy and paste some code so i don’t have to remember all the numbers i worked out basically i worked out some nice numbers for generating our perlin landscape i didn’t copy it okay so what is this doing here’s the noise function which You already understand but instead of having a global frequency in a global amplitude we’re actually going to create a local a temporary variable called y and it’s going to be set at zero then we’re going to determine a frequency sorry a an amplitude of 42 high and smooth out with a higher number We’re going to use the x and the z position that we’ve passed in from whichever our current terrain cube is and we’re going to add to the value we have in y and then we’re going to add again to that previous thing with now a lower amplitude and another frequency And basically these are called octaves each of these layers and hence the name you should have about eight octaves to have a very realistic um terrain so it’ll be really fun for you to um play around with these numbers usually you go from like a high The higher kind of basic shape of your terrain and then the next layer is like slightly more bubbly and then the smallest layer is like the tiny little bits right at the bottom play around with those numbers and things you get some gorgeous looking and interesting looking terrains But this looks okay i think for our purposes so we’ve now positioned our cube in the right position and this reference we need to paste into our generate the shell so we just want to say your x oh it’s down here sorry your y position is just go and generate Um or return a certain height and in fact i didn’t finish off our function here we are we now need to return um y so again y is this temporary variable is just going to return that number based on whatever number we pass in whatever position we pass it so That worked for both the shell generation and our generating the terrain in a swirly swirly swirly pattern um and to make it look like minecraft of course we want to wrap that with floor so be at one or two or three or four instead of 1.1.2 1.8 1.7 if you just take off that floor you’ll have a much flatter kind of terrain or a less minecrafty look okay right it feels like we’re very close to finishing um this needed a bit more work didn’t it what did i need to add at the bottom of our rotationally swirly things Oh we needed the rad limit of 128 okey dokey so um yeah if we’ve so if we’ve done a whole cycle of creating these swirls we’ve got back to zero theta is zero so our angle is zero and so then we increase the radius we only want to generate say a certain Large amount before stopping um um for performance issues um although i guess we could let’s just leave that for now and go and add that later on okay um terrain finished so this is where we want to we want to combine all of the subsets so let’s i think we finish with the Key functions here we want to go and delete sub width we don’t need terrain width we don’t need terrain we don’t need frequency and amplitude we don’t we need we do need our noise object our purlin noise object we only want to have one octave we’re not going to use its inbuilt octave And seed you can put whatever cg you want i’ll keep 99 in there and sub cube index i can’t remember if i’m using that for anything let’s just delete that um so it looks a lot cleaner now okay so in our update loop um oh yeah i’ve just noticed that here’s A mistake actually from previous videos i’m saying if i’ve moved one block either on the z axis or the x-axis then go and generate the shell but actually um i’m then not resetting where the previous x and z position was of the subject so essentially this is going to run every single Update which is a good idea so in fact um let’s just move this here so we go and generate the shell and then we’ll just put pass in there because this might be useful later on although um we do want to say um previous zed now equals the subject’s current Z position and same thing for previous x so yeah it’s just recording where your subject is or your player is and then the difference between where your player is at a future time and where we last recorded it could be greater than this this might be this might be useful for when we’ve Wandered a really far or a certain amount and do we want to start generating the train over there we probably do so you want to change this number to 10 or something but we’re not going to do that now i’ll just leave that like that this past just means we’re not going to Do anything but we are going to generate a shell every single update this will start with glitching me falling through the terrain of things so if time minus previous time is greater than 0.05 so we want to put this into a variable we’ll call it sub speed so that just means how how Quickly are we generating new um sub cubes new parts of terrain actually let’s call it gen speed generating new terrain so we probably want that as a global variable and we need to create the variable in our new variables place generate speed um i found an optimal number at the moment What was it oh that was the previous um oh zero so basically we’re just saying generate terrain as quickly as possible you’re going to want to increase this number from zero if you’re getting a low frame rate only generate those every so often and then it will be a smoother experience Okay um right back to yeah the update loop so we don’t want to so if enough time is a lot so in our case now every single update so 60 times a second hopefully we want to then go and gen terrain so we’re going to call our new generate terrain function that swirls Around and then we want to say previous time equals the time now lovely and safety net in case of glitching let’s just delete that i’m sure we won’t glitch anymore and then the giant chicken is going to look at us god bless vincent who’s going to look at Us and then we’ve got the build tool interesting so what does our code look like so we’ve got our imports at the top so we’ve got new sine cosine radians um we set up the window and the fog things like that we’ve got our textures then we’ve got all our build stuff which We did the last video we’ve got input oh generating the terrain what we want to be able to do is switch that on and off um very much like we did with the build mode we’ll do that in a moment if it’s working first oh we can take away amplitude we don’t Need amplitude from the update we just need previous zx previous time and generate speed um we’ve got build tool and then down here we’ve got our new variables so we’ve got our subsets and sub cubes which pre-existed we haven’t created all our terrains yet so we’ll do that If things are working then we instantiate all of our oh we make our dictionary with the curly braces we’ve instantiated our ghost subset cubes so these are doing all the work to create all our terrain and now we’ve got the subsets which are combining the terrain um And now we’ve got a new purlin noise function to get a much nicer terrain now we’ve got a generate terrain function which we don’t know if this work this is the the new engine of that infinite terrain and we are combining into subsets there aren’t we we’re going to the next subset Oh yeah if we hit the limit we’re not asking if current cube equals oh yeah then we need to go to current cube equals zero we need to go to the first cube again right so what we can’t do is get past 420 uh subsets um and now we’re just Just rotating down here okay right we’ll just leave this maybe or should we just we’ll delete the finished terrain we’ve got three by three for a shell generating the shell ah we’re using the new perlin noise function we don’t need amplitude and frequency there and now we’ve got our first person Controller we’ve got some gravity oh let’s start up really high let’s go 64 so we can drop down and see if our terrain is working and we’ve got our origin um which is not changing so okay we’ll work on that right okay enough program then we’ve almost done an hour of programming there Or yeah about 50 minutes let’s see if any of that works press play it’s actually loading okay good news is we seem to be standing on some terrain and a giant chicken is looking at us we’ve got fantastic frame rate if i can’t see any terrain whatsoever Or maybe if i walk around will it suddenly if i’ve gone more than 16 will it generate a subset okay let’s escape there’s no bugs whatsoever so we’ve got no clues of what i’ve done wrong so let’s just make sure i i made the radius we’ve made theta We haven’t got a rad limit um subsets can combine enable maybe we need to add textures and things here so um let’s just have a look at my previous code to find the uh the answer quicker um one second i’ll just pause and look for it so it’s quicker for you Okay i think i’ve found the answer we didn’t actually give our Subset a a texture so when we’re instantiating our empty subsets before we disable it let’s say texture equals um What would look good monotex i think that’s the one i’ve been using uh stroke mono okay oh yeah this this is the one we can like shine different colors through isn’t it uh yes texture equal okay save and play i can’t see anything yet oh you know what was also not happening This should have been updating and updating so i should have been rotating or and i’m not getting any new combination um records printed out here so there’s something else going wrong let me pause again to have a little look okay a few things um in our generate terrain Right at the end i’ve put this increment of the radius in the wrong block we always want to no we don’t always want to um we don’t always want to increase the radius We want to if we’re at zero which we are to begin with it never gets onto onto one but the the radius does want to increase to one that first time so we want to say else radius plus equals one let’s just see what that does so far List object has no attribute enable where’s that line 162 oh right subsets current subset well done if you spotted that bug we can’t just refer to the whole list we want a particular subset particular entity on the subsets list to actually enable oh then we would definitely not see anything if we’ve Never got to that i’m intrigued whether this it’s it’s kind of nice because it’s only six lines but it’s not that pretty whether it works it’s slightly different to my prepared code oh my goodness yes oh this is brilliant this is exciting it’s working and 60 frames per second lovely And notice i’ve moved away from the origin point the origin point is over there and it’s still generating until it’s kind of like happy and it will actually all keep on generating so we need to sort some things out the other thing is we’ve got um our sub cubes overlapping and things that’s Because i didn’t use our our floor mathematical function up here so when we’re deciding where the train cube needs to be we need to put it onto a grid so we just need to wrap that whole thing in floor there we go let’s just run that again And now we can see basically there’s a a cube running all the way around the train and if there’s not a cube there it will add itself and our shell terrain is adding itself uh sorry it’s generating its purlin heights and things we can see some gaps appearing That’s because as i said my my dirty little algorithm is not great so there’s one little hack for us and what that is is to instead of increasing by one when increased by 0.5 i think that will take those gaps away the next thing we need to do Is basically tweak it so that we can generate terrain as quickly as possible but balancing our frame rate at the moment it’s just positioning one um block one cube every update cycle and what we really want to do is generate as many as we can each cycle So far as it doesn’t kind of disturb our smoothness so uh to do that what we need to do is if we go to the update function here we are when it’s time to generate some terrain we’re only generating we’re only running that function once so in other words it’s only drain It’s only placing one cube so we want to say um if i oh no um for i in range so we’re just creating a little for loop and we’ll just say um per cycle i think i called it and that’s just like how many cubes do you want to create and then We’ll put that in the block there and per cycle will need to be a new variable we’ll add that to our list of global variables so we can assign it out here and refer to it in the function and it’s kind of related to the generate Speed um i think we want to match 16. is that what i did in my original yeah 1616 and rad limit okay let’s just see if our terrain is generating a little more quickly and just have a look at our frame rate okay so that’s a lot quicker our frame rate’s now dropped But i’m recording and i’m not on a very fast computer and it’s i wouldn’t say silky but it’s it’s like satin it’s not bad okay that’s good right so what do we need to do we need to be able to change the origin position and to stop swirling when or generating terrain when We don’t want that to happen so i’ll just do those two things we’re going over an hour now and next time i’ll show you how to like load in models to make everything look a bit more gorgeous and to add colors and things again so i’m pretty happy with this we’ve got an Infinite train working i haven’t tried like building stuff as well so anyway in the update if we’ve moved 10 is that what i do in my original oh i’ve just done one let’s stick to one then so if you’ve moved one block then first thing you want to do take out The pass of course first thing we want to do is say origin equals subject position so we’ve got a new subject position also we then want to say radius equals zero again and is that all we need to do we do need to add radius on origin then to our global Variables i’ll just make another global line global radius global origin so we can refer to those um yes what would also be nice um is if we’re actually generating so are we generating or equal one if it’s on make ourselves a note Is it minus one if off and then we can do that toggle thing a toggle thing so where is yeah generate terrain so what we want to do here is if key equals g then we want to say generating minus equals minus one so if it was minus one it’s now going to Be one so it’s generating and if it was one it’s not going to be minus one brilliant and generating doesn’t exist so we need to add that as a global variable so that’s how we can switch off generating the terrain um and really yeah if we’re saying i want to have a New origin and start generating some more terrain i want to say generating equals one i want to switch that on again generating doesn’t exist in our update loop so we need to or update function so we need to add the generating as a global there and Another little tweak actually is what if we wanted to just switch off generating the train and we move more than one terrain unit then we just switched on generating terrain again so we can’t like move without switching on so we only want to be able to trigger this if um We want to be able to trigger the uh terrain uh the generation on so we need another variable called can generate and we’ll say equals um um my my um oh there we go minus one so yeah it’s sorry i’m very tired so it’s just another negating or toggling Kind of system so it needs to be one and then it’s going to be minus one if off sorry it was a double negation that was too much for my little brain ah so i want to say generating equals um oh my god equals 1 times whoops i made a dot times Um what did i call it can generate so that means [Laughter] if we can’t generate it’s going to be -1 so generating is going to be off and up at g we want to say um yeah generating is off and we want to say can generate um is toggled So if it was minus one it’s not gonna be one if it was one it could be minus one that is quite complicated i should be more fair to myself um for getting a little bit tongue-tied and stuck there it’s okay it’s just a little bit complicated that’s all right okay so Let’s go and run this that’s my congratulations might not if you’ve watched this video um so far oh we dropped a little bit into the train there that wasn’t good a bit of a clash detection it is very fast this is nice and can i like run backwards Yeah that’s nice and so there we go anush is generating the terrain in the invisible places if we stand still we can let it kind of like grow around us do fiddle around with the numbers and things so maybe if you’ve got more powerful you probably have got a more Powerful computer than me you’ll be able to generate terrain a lot faster a lot smoother so if i go down here no if i go over here i’ll let it generate next to vincent and then see if we can switch it off so it’s generating press g it didn’t switch off So all that complicated stuff um was not working okay generating equals oh that’s because we’re saying um if uh generating equals one then oh wait a minute we don’t want that there actually we want this where’s our generate terrain function yeah we want to say global generating then we want to say If generating equals minus one return don’t run this that’s the best place to put that so what i didn’t do was the simple thing which is well if i’m not generating don’t run the function i was just letting it run whatever was happening okay so it’s running if i press g it stops Lovely and i can run around and it won’t trigger it generating perfect if i press g again i’ve moved more than one so i’ve got a new origin point and i’m here very good okay so what i need to do in the next video let’s have a look change building block Type done prevent building when already oh yeah we need to finish that um four or actually i’ll put this up we can delete these two and actually we’ll call that number one and we’ll say mining question mark and zero needs to be combine subsets into uh terrains List that’s what we need to do next time right thank you very much if you watched and hopefully you’ll enjoy creating interesting um interesting infinite terrains right goodbye uh leave any comments if you have any questions and things goodbye Video Information
This video, titled ‘Minecraft in python: infinite procedural terrain with Perlin noise (Ursina Engine) – part 6’, was uploaded by Red Hen dev on 2021-07-16 21:11:26. It has garnered 3462 views and 69 likes. The duration of the video is 01:17:58 or 4678 seconds.
We adapt our previous code to generate an infinite terrain. We use polar co-ordinates and a dictionary to create a ‘swirling’ pattern of terrain cubes moving outward from the player’s position, wherever they are on the map. We also improve our Perlin function for a more satisfying terrain. Again, we make use of ‘subsets’ of combined cube entities to keep performance silky.
Code: https://github.com/RedHenDev/ursina_tutorials/tree/main/python_minecraft_tut_2021
Hope you don’t mind the longer video!
Thanks for watching 🙂