{"id":50737,"date":"2020-09-15T06:00:42","date_gmt":"2020-09-15T10:00:42","guid":{"rendered":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=50737"},"modified":"2020-09-15T06:21:16","modified_gmt":"2020-09-15T10:21:16","slug":"project-bug-hunt-2-lets-build-some-walls","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=50737","title":{"rendered":"Project Bug Hunt #2: Let&#8217;s Build Some Walls"},"content":{"rendered":"<p>To recap: The goal of this project is to generate something like an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Immersive_sim\">immersive sim<\/a> shooter level. This means designing a level, building the rooms, and &#8220;furnishing&#8221; those rooms. I don&#8217;t know what furniture we&#8217;re going to use and to a certain extent it doesn&#8217;t matter. We just need to prove that we could fill the rooms with whatever the game calls for, and the furniture needs to be placed in a way that makes sense. <\/p>\n<p>The Star Trek Enterprise might have a Captain&#8217;s chair, beds, a warp core, and an ice cream machine<span class='snote' title='1'>Oh, you&#8217;ve never seen the Trek Ice Cream machine? Watch the episode where someone uses the toilet. The ice cream is right next door to that.<\/span>, but we shouldn&#8217;t see them all next to each other. A fridge doesn&#8217;t make sense in the middle of the room and the captain&#8217;s chair doesn&#8217;t make sense in the corner. My program needs to be able make this logic work without resorting to premade room layouts.<\/p>\n<p>In addition, we need lighting that works within some reasonable performance constraints. The world needs coherent collision detection so the player doesn&#8217;t fall out of the level or get stuck on invisible walls. It needs to be possible for an AI to navigate the space, even if we don&#8217;t add proper shooter combat mechanics or proper enemy AI. <\/p>\n<p><!--more--><\/p>\n<h3>Marching Squares<\/h3>\n<p>So I want to create a procgen shooter level, but I want to keep code complexity to a minimum. This means I have slightly contradictory goals. <\/p>\n<p>The most obvious and direct way to do this would be to make my levels out of giant cubes on a grid, like in Wolfenstein 3D. That would indeed keep the complexity way down, but that would make for some catastrophically boring levels. <\/p>\n<p>My plan to sort this out is to use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Marching_squares\">Marching Squares<\/a>. According to Wikipedia: <\/p>\n<blockquote><p>&#8220;Marching squares is a computer graphics algorithm that generates contours for a two-dimensional scalar field (rectangular array of individual numerical values). A similar method can be used to contour 2D triangle meshes.&#8221;<\/p><\/blockquote>\n<p>I actually explained marching squares <a href=\"?p=15945\">back in 2012<\/a>:<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant10_3.png' width=100% alt='I messed up this image when I originally made it. In the final  two diagrams, there should be one more little triangle at the very top, just left of center.' title='I messed up this image when I originally made it. In the final  two diagrams, there should be one more little triangle at the very top, just left of center.'\/><\/div><div class='mouseover-alt'>I messed up this image when I originally made it. In the final  two diagrams, there should be one more little triangle at the very top, just left of center.<\/div><\/p>\n<p>Basically, you fill in a grid with on \/ off values. An <b>on<\/b> value would be &#8220;inside a room&#8221; and an off value would be &#8220;void space between rooms&#8221;. Marching Square logic then fills in walls between the two, creating an enclosed space. It&#8217;s more complex than grid-based levels and it gives you beveled corners rather than sharp angles. It&#8217;s a little more complex than grids, but far more interesting to look at. I don&#8217;t know if this is the correct way to go, but it seems like a good place to start in terms of code complexity vs. visual interesting-ness..<\/p>\n<p>If the 2D layouts are still too boring, there&#8217;s always the possibility that we could stack sections on top of each other and connect them with lifts \/ ramps \/ staircases. The logic would still be 2D (and thus low in complexity) but we&#8217;d have some vertical motion for the player.<\/p>\n<p>And if none of that works out, we could always dump Marching Squares and try (say) <a href=\"https:\/\/en.wikipedia.org\/wiki\/Marching_cubes\">Marching Cubes<\/a>. I&#8217;ve <a href=\"?p=15945\">done both<\/a> in the past and I still remember how they work, so implementation should be easy. I can do either, but we might as well start with the easy one first.<\/p>\n<h3>Top-Down, or Bottom-Up?<\/h3>\n<p>In the end, we&#8217;re (hopefully) going to have code that will cut the level into sections, sections that cut themselves into corridors and rooms, and rooms that fill themselves with furniture.<\/p>\n<p>It seems like you&#8217;d want to start at one end or the other. Maybe we start at the bottom and get the furniture code working, then create a room to contain the furniture, then make a section made of multiple rooms, then several such sections to make a level. Or maybe we should start at the other end and work our way down to furniture.<\/p>\n<p>While it seems counter-intuitive, I kinda want to start in the middle. I want to focus on building rooms, since that&#8217;s where the Marching Squares stuff is going to happen. I want to get that system working, or discard it early if it doesn&#8217;t work out. I might need to iterate on it a bit, and that will be easier if it&#8217;s not tied to a bunch of other systems yet.<\/p>\n<p>But if rooms come from levels, then how can I generate rooms without generating the level first?<\/p>\n<p>Well, I&#8217;m going to cheat. I&#8217;m going to use a hand-made 64&#215;64 png for my map. Something like this:<\/p>\n<p><img decoding=\"async\" src=\"images\/bughunt_map1.jpg?\"><\/p>\n<p>Okay, that&#8217;s annoyingly small and probably impossible to see on a mobile device. Here&#8217;s a larger version:<\/p>\n<p><img decoding=\"async\" src=\"images\/bughunt_map2.jpg?\"><\/p>\n<p>Black is void space, white is a corridor, and any other color is a room. So for now my &#8220;level editor&#8221; is a simple image editor. Someday layouts will come from code, but for now we have this. This has the added benefit of allowing me to create controlled tests. If I notice that (say) the north-facing walls in rooms are missing, then I&#8217;d be stuck wondering if it was a problem with the level generator, the room generator, or the code that turns those shapes into polygons. But here I can test the room code while knowing exactly what the level is &#8220;supposed&#8221; to look like.<\/p>\n<p>There&#8217;s a lot to say about getting this project set up and laying the foundation, but I&#8217;m in a hurry to get to the bits where I can start showing you screenshots. Once I have something to show, we can backtrack and talk about the finer points. I don&#8217;t want to spend three entries on theory before we draw our first polygon.<\/p>\n<p>So here is the first map, along with the image that was used to generate it.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_map3.jpg' width=100% alt='Left: My hand-made 64x64 pixel data. Right: The resulting level layout.' title='Left: My hand-made 64x64 pixel data. Right: The resulting level layout.'\/><\/div><div class='mouseover-alt'>Left: My hand-made 64x64 pixel data. Right: The resulting level layout.<\/div><\/p>\n<p>I&#8217;m cheating a bit here in order to show you this map. I didn&#8217;t add the feature to draw the layout like this until much later in the project, but I&#8217;m using it here because it makes all of this easier to follow. I really regret not adding it sooner. At the time I thought &#8220;Bah. I don&#8217;t need a map. That&#8217;s a fancy luxury feature. I don&#8217;t want to get distracted twiddling with novelty features rather than doing Real Work&trade;!&#8221; That mindset was a mistake. Getting the basics working was a fiddly job, and I did the whole thing blind. When something went wrong, I had to figure it out by looking at lists of numbers and calculating in my head what things &#8220;should&#8221; look like. If I&#8217;d had the map available, I could have glanced at it to see what the program was TRYING to build, which would give me a lot of clues about where the process was breaking down.<\/p>\n<p>Anyway, let&#8217;s switch back to the present tense so we can resume the pretense that I&#8217;m telling you about this stuff as I do it.<\/p>\n<h3>But How Does it Work?<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_map4.jpg' width=100% alt='For this image, the camera is positioned in the North-East corner of the space. Those soda-can shapes on the left are pillars. Those come from the vertical line of black dots in the source image.' title='For this image, the camera is positioned in the North-East corner of the space. Those soda-can shapes on the left are pillars. Those come from the vertical line of black dots in the source image.'\/><\/div><div class='mouseover-alt'>For this image, the camera is positioned in the North-East corner of the space. Those soda-can shapes on the left are pillars. Those come from the vertical line of black dots in the source image.<\/div><\/p>\n<p>The program passes over this little image and looks at the pixels. &#8220;Okay, this blue pixel belongs to room #3, this white belongs to the corridor, this green pixel is room #12, etc.&#8221; When I&#8217;m done, I have a grid of room assignment numbers. Since 1 pixel = 1 meter, this grid represents 64&#215;64 meters of game space. <\/p>\n<p>Earlier I said that Marching Squares uses points that are on\/off. I&#8217;m doing a bit of a variation on that here. I pass over the grid for Room #1. All points that belong to #1 are &#8220;on&#8221;, and any points that don&#8217;t are &#8220;off&#8221;. I use these on\/off points to generate line segments, and I give those line segments to the room-building code. I pass over the grid once for each room, creating the 1D line segments that will (eventually) become the walls.<\/p>\n<p>The trick here is that I&#8217;m passing over the grid left-to-right, top-to-bottom, because that&#8217;s the easiest way to traverse a grid. But the RoomBuilder code doesn&#8217;t care about the grid. It wants the line segments ordered so that it can trace around the perimeter of the room. All rooms will inevitably form closed loops. <\/p>\n<p>So I have the room builder code sort the line segments to put them in order, so that running through the list will take you around the perimeter of the room clockwise. <\/p>\n<p>When it&#8217;s done, I can look at which way the lines face and use that to find the angle looking into the room, perpendicular to the wall.<\/p>\n<p>Okay. Now if this was a C++ project I&#8217;d need to stop here and spend the next two days hammering together shaders, a lighting system, and shadow casting code. But this is Unity and someone else did all that work for me. So let me slap down a huge plane so we have something to stand on, put a texture on the walls, and then turn on the lighting system and see what we get.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_map5.jpg' width=100% alt='Maybe someone could have used a larger texture here? Don&apos;t worry, I&apos;ve already sent an angry note to the art team about it.' title='Maybe someone could have used a larger texture here? Don&apos;t worry, I&apos;ve already sent an angry note to the art team about it.'\/><\/div><div class='mouseover-alt'>Maybe someone could have used a larger texture here? Don&apos;t worry, I&apos;ve already sent an angry note to the art team about it.<\/div><\/p>\n<p>No doubt you&#8217;re about to demand to know when you can pre-order this magnificent thing. But before you begin throwing money at your screen, I should warn you that the rooms are fully enclosed so we can&#8217;t travel between them. There&#8217;s no ceiling, the light comes from nowhere, and it would be physically impossible to make the texture mapping more boring.<\/p>\n<h3>Extruding<\/h3>\n<p>Making the level entirely out of extruded 2D shapes would probably get way too boring. I mean, what we&#8217;re building right now is basically the original Doom, but without elevation changes. As it is now, the floor will always be perfectly flat and the walls will always be perfectly vertical. We need a little interest <b>somewhere<\/b>, but I don&#8217;t want to make anything too complicated just yet.<\/p>\n<p>My plan here is to add the ability to shape walls. We can give the walls a set of 2D points that would act as a profile, controlling the depth of the walls. Essentially, we&#8217;re extruding up out of the floor to make walls, but then extruding outward from the walls to get&#8230; slightly more lumpy walls?<\/p>\n<p>As things are now, all walls are infinitely thin. We don&#8217;t have doors yet, but if we did and you stood in a doorway, you could position yourself so that you were looking at the wall edge-on and see that it has zero thickness. We can fix this by pushing the polygons away from the wall. If I pull this wall outward by (say) 0.2m, and the adjacent room does the same, then we&#8217;ll have created a gap between the rooms that&#8217;s 0.4m thick. Now if we change that thickness as we go up the wall, then it will (hopefully) give the wall an interesting shape.<\/p>\n<p>Like I said, we do this using coordinate pairs to define height and thickness. A number pair like 0.2, 0.0 says that &#8220;at the bottom of the wall (0.0 height), make the wall 0.2m thick.&#8221; The coord 0.5, 2.0 would say &#8220;At two meters off the ground, the wall is 0.5m thick&#8221;. And so on. So I&#8217;m going to make the wall bulge outward at 2m off the ground, and then get thin again.<\/p>\n<p>Also, the art team has finally gotten around to adding a proper texture to the walls. (You know how those people are.) The result looks like this:<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_map6.jpg' width=100% alt='Better than a poke in the eye, I guess.' title='Better than a poke in the eye, I guess.'\/><\/div><div class='mouseover-alt'>Better than a poke in the eye, I guess.<\/div><\/p>\n<p>Eventually this wall shape will come from a settings file, but for now It&#8217;s all hard-coded.<\/p>\n<p>I know it&#8217;s hard to get a sense of scale without recognizable objects in the scene. If it helps, the walls are 3 meters tall.<\/p>\n<p>I&#8217;m not sure what you&#8217;d properly call this type of shaping. Like a lathe maybe?<\/p>\n<p>In any case, I&#8217;m not sure if this is going to be interesting enough, but we&#8217;re going to experiment with it and see how it turns out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To recap: The goal of this project is to generate something like an immersive sim shooter level. This means designing a level, building the rooms, and &#8220;furnishing&#8221; those rooms. I don&#8217;t know what furniture we&#8217;re going to use and to a certain extent it doesn&#8217;t matter. We just need to prove that we could fill [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-50737","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=50737"}],"version-history":[{"count":15,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50737\/revisions"}],"predecessor-version":[{"id":50752,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50737\/revisions\/50752"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=50737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=50737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=50737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}