{"id":9844,"date":"2010-11-17T12:15:45","date_gmt":"2010-11-17T17:15:45","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=9844"},"modified":"2010-11-17T12:17:52","modified_gmt":"2010-11-17T17:17:52","slug":"project-hex-part-5-growing-grass","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=9844","title":{"rendered":"Project Hex Part 5 &#8211; Growing Grass"},"content":{"rendered":"<p>Last time I created a texture for the world. It was okay.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_texture11.jpg' class='insetimage'   alt='hex_texture11.jpg' title='hex_texture11.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The next step is to give this texture a bit more depth and to make it less rigid and mechanical. The first thing I want to do is make the grass edging a little more interesting. I&#8217;m going to be basing my efforts on <a href=\"http:\/\/gas13.ru\/v3\/tutorials\/sywtbapa_de-mystifying_greats_1.php\">this tutorial<\/a>. (Halfway down the page.)  It shows some grass hanging over onto some dirt. Two things I&#8217;m taking away from this:<\/p>\n<p><!--more-->1) The grass looks much more interesting if it seems to spill over onto the non-grass.<br \/>\n2) The surface looks &#8220;deeper&#8221; if the grass casts shadows onto the non-grass.  The shadows don&#8217;t even need to be accurate based on the direction of the light source.  (And we don&#8217;t have a light source yet anyway.) It just needs a simple drop-shadow effect where it outlines the grass in dark pixels. The eye will do the rest of the work.<\/p>\n<p>Just to recap how I&#8217;m drawing the world. I&#8217;m putting a splat of texture at each point of the grid, like so:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges2.jpg' class='insetimage'   alt='hex_edges2.jpg' title='hex_edges2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>But I draw the splats large, so that they overlap.  I draw all of the deep water ones first, then the shallow water, then the beach, then the grass, and so on.  In the end we get a nice layered effect:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges3.jpg' class='insetimage'   alt='hex_edges3.jpg' title='hex_edges3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>But I want that grass that borders the sand to have a drop shadow effect.  So what we do is this:<\/p>\n<p>Before I draw the grass splat, I draw a splat that is slightly larger, and colored pure black:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges4.jpg' class='insetimage'   alt='hex_edges4.jpg' title='hex_edges4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Then once the black is down, I draw the grass splats.  Since they&#8217;re smaller, the black peeks out a bit around the edge:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges5.jpg' class='insetimage'   alt='hex_edges5.jpg' title='hex_edges5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>But of course that black outline is much too harsh. Let&#8217;s make it slightly transparent:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges6.jpg' class='insetimage'   alt='hex_edges6.jpg' title='hex_edges6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Now, at the end of the last entry I was fiddling with the splat sizes. It turns out that grass looks better if the splats are smaller, while water and beach look nicer with really big splats.  So I adjust things so that the splats of each layer can be sized independently. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges8.jpg' class='insetimage'   alt='hex_edges8.jpg' title='hex_edges8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I like this better.  The downside is that internally the game isn&#8217;t taking these oversized splats into account.  Note the hex I highlighted in the above image.  That&#8217;s shallow ocean as far as the game is concerned. But we got sand all over while we were drawing.  The player will see beach, but find that the game treats it as water.  In some games this would be unacceptable.  I&#8217;m not sure if or how this will impact my game, so I&#8217;ll ignore this for now and move on.<\/p>\n<p>The next step is to make the grass hang over onto the beach a bit.  Right now I&#8217;m using this texture for the grass:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges1.jpg' class='insetimage'   alt='hex_edges1.jpg' title='hex_edges1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>(Again, pink = transparent.)<\/p>\n<p>I&#8217;d like the grass to sort of hang over onto the sand, using a texture more like this:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges9.jpg' class='insetimage'   alt='hex_edges9.jpg' title='hex_edges9.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Dropping that into place, it looks a lot more interesting. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges10.jpg' class='insetimage'   alt='hex_edges10.jpg' title='hex_edges10.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Except.  There&#8217;s a problem here. Not all of the grass is pointed in the right direction.  I know it&#8217;s hard for you to see in these images.  Here, let me illustrate the problem for you:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges11.jpg' class='insetimage'   alt='hex_edges11.jpg' title='hex_edges11.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I took out most of the textures and replaced the grass with a downward facing arrow.  If the program was working correctly, then all of these arrows would be pointing out to sea. Or at least, roughly in the direction of downhill.  I&#8217;ve added some code to look at each grid point, then examine its neighbors (the three points which connect to it on the hex grid) and then compare them to figure out the lowest neighbor.  The direction to that lowest neighbor is &#8220;downhill&#8221;.  This code is not perfect, and it&#8217;s possible for it to become confused in certain situations.  In particular, I know I can&#8217;t count on it to come up with the right answer when dealing with saddle-shaped depressions, or when looking at a single point that juts up over its neighbors.   But wrong-way arrows should be an occasional aberration, and here we&#8217;re seeing almost half of them are way off and pointing directly uphill.  <em>This should not happen.<\/em><\/p>\n<p>Now, the grass actually looks okay.  The problem is so slight that I doubt most people would notice.   But this is troubling because it means I have a bug in my code. It&#8217;s probably the code I use to examine the grid and determine which points are related to which other points.  While it&#8217;s harmless now, this same code will end up being used later on, and if it&#8217;s malfunctioning  then it will cause problems that look like they&#8217;re caused by something else. I&#8217;m actually lucky to have spotted it now.  If this manifested itself later when I added (say) line-of-sight checking, then I would likely blame the LOS code I had just written and end up on a wild goose chase.<\/p>\n<p>So.. let&#8217;s fix this bug.  Should be easy.  Let&#8217;s just cover the map in arrows so we can spot which points are misbehaving.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges13.jpg' class='insetimage'   alt='hex_edges13.jpg' title='hex_edges13.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Ugh.  This is no good.  Too hard to spot &#8220;bad&#8221; points with the surface this complex.  Let&#8217;s simplify things.  I&#8217;ll turn the entire world into a big slope and get rid of the coloring.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges12.jpg' class='insetimage'   alt='I&#8217;ve highlighted some of the obviously incorrect arrows.  The slope points down to the lower-right, and these ones aren&#8217;t pointing that way.' title='I&#8217;ve highlighted some of the obviously incorrect arrows.  The slope points down to the lower-right, and these ones aren&#8217;t pointing that way.'\/><\/td><\/tr><tr><td class='insetcaption'>I&#8217;ve highlighted some of the obviously incorrect arrows.  The slope points down to the lower-right, and these ones aren&#8217;t pointing that way.<\/td><\/tr><\/table><\/p>\n<p>After a lot of time-consuming experimentation and scrutiny, I&#8217;m still not finding the problem.  Hm.  Using a simple slope as my test case is probably a bad idea. It will only catch wrong arrows going in one direction.  Let&#8217;s make the terrain a spike and look at it then.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/hex_edges14.jpg' class='insetimage'   alt='hex_edges14.jpg' title='hex_edges14.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>(HOURS LATER.)<\/p>\n<p>Wow.  This a toughie. I think I need to table this and come back to it later.  I&#8217;m betting the problem is simple, but I&#8217;m not seeing it in the code.  Sometimes it&#8217;s like proofreading: You read what you think you wrote, not what you really wrote. Coming back to it later will let me see the code fresh again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time I created a texture for the world. It was okay. The next step is to give this texture a bit more depth and to make it less rigid and mechanical. The first thing I want to do is make the grass edging a little more interesting. I&#8217;m going to be basing my efforts [&hellip;]<\/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-9844","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\/9844","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=9844"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/9844\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}