Programming Terrain Generation for my Farming Game

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to another devlog video for homegrown the Casual farming game I'm making using my own engine and this week I'm going to be making some big changes to the terrain generation so starting off the week with a lot of planning for this update I'm really excited about this update I think there's going to be a lot of very interesting coding coming up some procedural Generation stuff as well so the main reason I want to do this update for the terrain is I want to make the terrain system a bit more scalable because I'm planning an update to where the player is going to be able to buy more land for their farm and I think the Farm's going to get quite big and then secondly I'd really like to make the edges in the corners of the terrain a bit more rounded because currently in the game the corners and the edges they're very sharp they're very harsh and it doesn't really fit in with the rest of the art style of the game so I'd like to round them up and make them something more like this this is just a mock-up I've made in blender and I'm going to try and get it to look like this but it is quite a bit more complicated than it sounds to round off the edges and I'll show you in a bit why it adds so much complexity but right now I'm going to get into the code and start working on this new system [Music] so up until now my terrain generation system has been extremely simple imagine this is the world grid here then if this is a grass tile then I would simply render a grass mesh tile here and if this is a soil tile then I render a soil mesh in that position path tile render a path mesh and so on super easy because each mesh stays within its own tile but it does result in these hard Corners so let's now have a look at the same example with rounded corners and I want both the outer corners and the inner Corners to be equally rounded like this so if we have a look at this soil tunnel here it's mostly soil mesh but you can see that now a little bit of grass mesh has sneaked in there as well and this grass towel here it's lost a couple of corners and some soil has leaked in from the neighboring tiles so it's unfortunately no longer as simple as this is a grass tile render a tile of grass mesh here because now I have to determine the shape of the grass mesh and that's going to depend on all of its neighboring tiles and this example here is just with two terrain types I still want to have support for unlimited terrain types which obviously complicates things even more but that's what I'm trying to solve this week so I've been in the codes getting the absolute basics of this new terrain system setup and um I've got to the point where it's rendering in the game so you can see it here I haven't done any of the code but generating the tiles yet so currently it's just generating flat red quads for the tiles which is why it looks like this also you'll notice that around the edge it's kind of flashing that's because this terrain has to be half a tile bigger than the previous terrain so it's currently overlapping with the neighboring terrains which is why it's flashing a bit and I'll talk a bit more about why it has to be bigger in a bit but now now that it's rendering the fun can start and I'm going to get to work on some of the generation code for the tiles foreign so the technique I'm going to be implementing for the terrain generation this week is called the Dual grid system and I heard about it on Twitter from Another Game Dev called Oscar I'm sure a lot of you have heard of him already he's the guy who made townscaper among other things he's actually done a talk about this technique which I'll link to because he does a better job of explaining it than I can but very briefly if this is the world grid and this is your terrain mesh you probably want to split the mesh up into tiles the obvious way to do this would be to split it into tiles that are aligned with the world grid like this but in the Dual grid system we instead split the mesh up like this so the tiles are offset by half a square from the world's grid the result is still the same the mesh still ends up looking the same we're just cutting it up into tiles differently and just to clarify this second grid is only for the mesh tiles of the terrain only for rendering the terrain mesh the entire rest of the game still only uses and only knows about the world grid like always so what's the point in doing this well it actually simplifies the terrain generation process quite significantly with Oscar's system you can construct any terrain with only these six tile shapes whereas for the normal system you'd need way more if you wanted to get these equally rounded inner and outer curves also when a grid Square changes type you only need to update the four neighboring mesh tiles whereas usually up to nine local mesh tiles could be affected and it's the same the other way around as well so when you're generating a mesh tile with Oscar's system you only need to take into account the four neighboring squares in the world grid whereas usually you'd have to take into account nine local squares when determining the shape of the mesh tile so it's a really neat solution and I'm working on implementing it right now thank you so it's taken me about a day but I finally finished generating the six basic tiles needed for building the terrain so these are the six tiles you can see here in Oscar's diagram and I've been generating these tiles in the code as opposed to making them in blender or something like that for a couple of reasons firstly once I start introducing Pathways and other types of terrain there are just going to be so many different possible combinations for a tile that it's just not going to be feasible to individually create every single possible tile I'm instead going to be like procedurally generating each tile in the code anyway and then secondly it also has the added benefit of making it really easy to play around with the dimensions of these meshes so for example if I wanted to change how rounded the corners are or change any colors I can do that really easily by changing a couple of numbers in the code as opposed to having to go through each of the models and update them individually [Music] next up I've been working on getting all of these terrain tiles connected up properly so now whenever a Terrain tile updates it checks it's for neighboring grid squares to see what terrain type they are and then it uses that information to determine which of those six base tiles it should be and what rotation to render the tile in and after doing that I've now got this very nice terrain with nice rounded corners and if I just go into wireframe mode here quickly you'll be able to see that second grid a bit better so you can see that all these grass Styles here are offset by Harper tile from the main grid which is what's I as the player I'm using to edit the terrain so that's all looking good so far but the top edges of the terrain are still quite hard sharp edges so I'm going to get back into the generation code and try and round off those edges a bit [Music] the top edges of the terrain are now nice and smooth as well I'm really happy with how it's all looking now definitely fits the art style a lot better than the old hard edges did and um to round out these edges I actually only had to add one new Quad along that edge the way that this works is if I show you in this diagram here so this is a cross section of the edge before adding the extra quads and then I add that extra quad still doesn't look very round but the trick is to set the vertex normals like this and then when the normals get interpolated over the triangles in the Shader the top base stays completely flat the side face stays completely flat but on that extra quad the normals go smoothly from pointing upwards to pointing sideways and when you use those normals in the lighting calculations it gives the appearance of it being a nice rounded Edge next up I've just been planning how I'm going to introduce other terrain types like Pathways and the way I'm going to do this is I'm first going to generate the base tile like normal but this time instead of it being grass and soil it's going to be grass and anything that's not grass so I'll generate the grass Park first then I just have to sort out the other terrain types that might be on that tile and you can see a few examples of how that might look for each of the six space tiles here and to handle the boundary between two different non-grass terrain types I'm just going to keep it simple and I'm going to generate a little brick border to separate the two types which should hopefully end up looking something like this I thought I'd start off by generating the mesh for the bricks for the brick border so I've just written the code for generating one brick of whatever Dimension and position and rotation I want and then in the game just to test it out I've got a few of them rendering here and again I'm using that same trick with the normals to make the edges of the brick appear rounded even though they're not really rounded at all foreign this morning I had been working on introducing the other terrain types and when you do that it creates a lot of possible combinations for a tile as you can see here this isn't actually all of them this is just if there were two extra terrain types when there could be up to four on a single tile so there's going to be way too many combinations really for me to individually create each of these tiles so instead I'm going to programmatically generate these in the code and the way that that's going to work is that for each mesh tile it first checks the four neighboring grid squares to see what terrain types they are it then checks Witcher grass and which aren't grass and it uses that to determine which of those six base tiles it should be so that's the grass bit sorted out then I just have to fill in the non-grass part and it's really simple Each corner of the tile just has a look at the relevant grid squares to use what terrain type it is and generates a quarter tile out of that terrain type in the relevant corner so that's what I've implemented so far and you can see it's working in the game here just placing some Pathways here I will make the path mesh a little bit more interesting for now it's just a great quad and it works fine with the grass but once it goes over another terrain type like the soil here this is when I need those brick borders to hide the transition between the two terrain types [Music] finished generating all of the brick borders which turned out to actually be really easy firstly there are only three base tiles that actually need borders so let me show you the code for the half grass tile as you can see super easy it just checks if the top right corner is a different terrain type from the bottom right corner and if it is then it generates a brick between them so I did something similar for the other two base tiles as well and then in the game there we go the bricks are all being generated correctly now I even made sure that at the corners there's a little brick so that it all fits together nicely and it's looking pretty good obviously the pathways don't have rounded Corners maybe I could Implement that at some point it would be quite complicated but at least there's no sharp geometry because the Border bricks are all nicely rounded themselves so in general I'm pretty happy with it it was always going to be difficult to allow for nicely rounded grass and unlimited other terrain types but I think this is a pretty good solution just quickly write some code to generate a bit more of an interesting looking mesh for the pathways and this is what it looks like in the game and this is just going to be one of many different path types in the game so don't worry if you don't like this one I've just added one more terrain type which is this dirt path again just for now it's just a flat quad but there's a couple of interesting things to show you here firstly I've allowed different terrain types to have different brick borders so you can see that the dirt path border is a lot smaller and thinner than the one for the tile path and obviously a different color as well and this then reads the question when two different terrain types are next to each other which border do they use between them and for this I've simply given each terrain type a priority value and whichever terrain type has the higher priority value that's the border that they use between them so you can see the tile path has a higher priority than the dirt path so it uses the tail path border here last but not least I wanted to make the dirt path look a little bit more interesting so I wrote some code to generate these little Stone meshes and then I've got them randomly appearing on the surface of the dirt path so it looks a bit more like a rough surface now so that is going to be it for this video I'm really happy with how it's all gone I think the terrain looks significantly nicer than it did before now and um it was just really fun it was a really enjoyable update to program before I finish I want to give a big shout out to the top patreon supporters from last month who were Captain Sherman zanil ambikar Atomic code Walden Yan Kristen A Smith Alan Lentz Josiah Hillman Dieter Reiner Terry Chung John Needham Christopher popo nickat asgazada Adam Farkas Gregory Horvath hargan vingard Matthew Connerton Miggy does Andrew Witt Marek mikhilijuk Sean McCrory caffeine coder Timothy Gibbons Alexander Chavez and Neil Blakey Milner so a massive thank you to all of you and of course to everyone else supporting me over on patreon for this week though that is it so thank you guys very much for watching this video and I'll see you all again next time
Info
Channel: ThinMatrix
Views: 28,075
Rating: undefined out of 5
Keywords: thinmatrix, farm game, gamedev, devlog, indie game, day in the life, game development, java, opengl, programming, home grown, indie dev, homegrown, indie game dev, low poly game, graphics, game engine, 3d, town, particle effects, modelling, 3d graphics, dual grid, terrain, mesh generation
Id: buKQjkad2I0
Channel Id: undefined
Length: 15min 46sec (946 seconds)
Published: Sat Jan 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.