{"id":9686,"date":"2010-11-03T12:17:45","date_gmt":"2010-11-03T17:17:45","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=9686"},"modified":"2010-11-08T14:43:20","modified_gmt":"2010-11-08T19:43:20","slug":"project-hex-part-3","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=9686","title":{"rendered":"Project Hex Part 3 &#8211; Things Get Hilly"},"content":{"rendered":"<p>I didn&#8217;t post about the project last week.  You didn&#8217;t miss much.  I needed to add non-visual stuff like camera controls, a debug log, <small>play a bunch of minecraft<\/small> and that sort of thing. <\/p>\n<p>So now we have a hex grid based world.  <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills1.jpg' class='insetimage'   alt='hex_hills1.jpg' title='hex_hills1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p><!--more-->I&#8217;ve got this bit of code which takes a simple integer and returns a color value.  It&#8217;s set up to be deterministic (the same number always returns the same color) but irregular (the colors for 37 and 38 will be very different) so that it will produce the above &#8220;kaleidoscope puke&#8221; effect.  It&#8217;s not pretty, but it&#8217;s really handy for debugging.  I say this as someone who has spent a lot of time squinting at rolling green hills, trying to find that one bad spot I was looking at last time  I ran the program.<\/p>\n<p>Time for some depth. Because the back end is really just a regular grid, I can easily pull in some elevation data. And the quickest way to do this is to bring in an image and use it as a heightmap. I just take some random image of bumpy white stuff:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_topo.jpg' class='insetimage'   alt='hex_topo.jpg' title='hex_topo.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And import it into my program, using the color values as height values.  The brighter the pixel, the higher the point. Take all of the elevations below a certain level and flatten them out, which will produce an &#8220;ocean&#8221;. The result:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills2.jpg' class='insetimage'   alt='hex_hills2.jpg' title='hex_hills2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p> Now, the game is going to be played from a fairly close-in view. The player will never see this much scenery at once. But for development, I want to be able to pull back and see what everything looks like.<\/p>\n<p>Note that much later in the project I&#8217;ll want to generate the world procedurally.  I&#8217;ll be making mountains, valleys, plateaus, lakes, etc. by generating them at startup.  But that&#8217;s a whole lot of complex stuff and I&#8217;d prefer to have the basics done first, which is why we&#8217;re using the heightmap for now.  For those of you squinting at the previous two images and trying to reconcile them, note that I&#8217;m only using one quarter of the heightmap for now. <\/p>\n<p>Now let&#8217;s see about making this look more like a landscape and less like a Care Bear took a dump on it.  <\/p>\n<p>This is pretty familiar work for me. You can make a visually convincing landscape with a few simple rules&#8230; <\/p>\n<p>Anything at or below the water level is water.  Anything above water level that is touching water is beach.  Anything point that&#8217;s on a slope steeper than 45 degrees is a cliff, where grass doesn&#8217;t grow. Anything above a certain elevation is rock.  Anything above another, higher elevation  is snow. Everything else is grass. <\/p>\n<p>This is not a simplification.  The previous paragraph could just as easily work as pseudocode, because this is all I&#8217;m doing. Follow these rules and you&#8217;ll wind up with the same results I do. Again, this will be a lot more sophisticated later, but we&#8217;re taking baby steps. <\/p>\n<p>Let&#8217;s see how it looks:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills3.jpg' class='insetimage'   alt='hex_hills3.jpg' title='hex_hills3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Note that I&#8217;m applying the above rules on a per-point basis, but I&#8217;m drawing the world on a per-hex basis. So, there&#8217;s additional detail in the points that make up the borders of hexes that&#8217;s not being shown.   Here is what it &#8220;really&#8221; looks like under the hexes:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills4.jpg' class='insetimage'   alt='hex_hills4.jpg' title='hex_hills4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That&#8217;s more interesting, obviously. But it will be irrelevant once we get the real artwork into place. Here is another look at the terrain, without drawing the hex boundaries: <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills5.jpg' class='insetimage'   alt='hex_hills5.jpg' title='hex_hills5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Yawn.  Without lighting or contour lines, the surface is very flat looking and it&#8217;s tough to get  feel for the shape. <\/p>\n<p>Now let&#8217;s import the entire heightmap and have a look from higher up.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills6.jpg' class='insetimage'   alt='hex_hills6.jpg' title='hex_hills6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Let&#8217;s do one more.  I&#8217;m going to feed it a larger heightmap and turn the world up to 512&#215;512 points. (The previous screenshot was a 256&#215;256 world, and all previous ones were 128&#215;128.)  This largest size is probably the size of the world in the final game.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills7.jpg' class='insetimage'   alt='hex_hills7.jpg' title='hex_hills7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>A 512&#215;512 grid of points yields a 255&#215;388 grid of hexes. (388? Man, working in hexes is <em>strange<\/em>.)  Each hex is 6 triangles. So we&#8217;re drawing 593,640 triangles. And it is stupid slow. <\/p>\n<p>Of course, the player will never see more than a fraction of this at a time. About this much:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_hills8.jpg' class='insetimage'   alt='hex_hills8.jpg' title='hex_hills8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Somewhere between 20,000 and 30,000 polygons, assuming I don&#8217;t do any optimizations at all. <\/p>\n<p>I could nitpick a lot about how the world looks right now, but this is good enough as a starting point.  Next time I&#8217;m going to go about implementing the texturing system I have in mind, and from there the look of the program should start to take shape. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t post about the project last week. You didn&#8217;t miss much. I needed to add non-visual stuff like camera controls, a debug log, play a bunch of minecraft and that sort of thing. So now we have a hex grid based world.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-9686","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\/9686","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=9686"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/9686\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}