{"id":20777,"date":"2013-08-30T05:30:50","date_gmt":"2013-08-30T10:30:50","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20777"},"modified":"2015-07-01T03:44:36","modified_gmt":"2015-07-01T08:44:36","slug":"project-good-robot-part-7-lines-of-sight","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20777","title":{"rendered":"Project Good Robot 7: Lines of Sight"},"content":{"rendered":"<p>As I play the game, I get this idea that a lot of AI problems are probably due to asymmetrical vision. Not all of them. It&#8217;s not <strong>that<\/strong> easy to make great AI. But there&#8217;s something inherently derpy about an enemy when you can see them and they can&#8217;t see you.<\/p>\n<p>There&#8217;s a 90&#8217;s movie where <a href=\"http:\/\/www.youtube.com\/watch?v=DLGReRz74dY\">fat guy Chris Farley plays a ninja<\/a>. There&#8217;s a bunch of shtick where he tries to hide like a ninja but fails because he&#8217;s huge. The humor (where applicable) comes from the idea that this 300 pound man is standing behind a floor lamp and thinks he&#8217;s hidden, when in reality he&#8217;s basically standing in the open. <em>He&#8217;s so dumb! He thinks we can&#8217;t see him!<\/em><\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight1.jpg' class='insetimage'   alt='gr7_sight1.jpg' title='gr7_sight1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;m noticing a lot of this in my game. Foes are parked behind a wall, waiting to ambush me. But instead of &#8220;Ooh, ambush!&#8221; I think, &#8220;Oh, idiot ninja that thinks I can&#8217;t see him.&#8221;  These are some really dumb AI, but the thing that makes them look dumb isn&#8217;t their AI, it&#8217;s the fact that I can see them hiding. <\/p>\n<p>So let&#8217;s experiment with the idea of restricting what the <em>player<\/em> can see to the things their <em>character<\/em> could see. <\/p>\n<p><!--more-->This doesn&#8217;t seem to be a common feature in 2D games. I know the original X-Com did it, along with most <acronym title=\"Real Time Strategy\">RTS<\/acronym> games. There was a semi-obscure game back in 2000 called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Nox_(video_game)\">Nox<\/a> that did this. I&#8217;m sure there have been others. But for the vast majority of 2D games, no attempt is made to reconcile player vision with character vision. In 3D this problem usually solves itself because player vision and character vision are the same thing (first person mode) or basically close enough (in a third person game) that we don&#8217;t need to worry about it.<\/p>\n<p>I don&#8217;t know how those other games did it, but here&#8217;s what I&#8217;m thinking:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight2.jpg' class='insetimage'   alt='gr7_sight2.jpg' title='gr7_sight2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;ll project a bunch of radial lines from the player, stopping when I hit some level geometry. This forms a perimeter of points that all have an open line between themselves and the player in the center.  <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight3.jpg' class='insetimage'   alt='gr7_sight3.jpg' title='gr7_sight3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>You can use these points to draw a triangle &#8220;Fan&#8221; in OpenGL.  You feed it the origin. (The player&#8217;s position.) Then you give it those radial points in order.  When you get to the end, repeat the first radial point again to close the loop.  This will create a filled region that covers everything the player should be able to see.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight4.jpg' class='insetimage'   alt='gr7_sight4.jpg' title='gr7_sight4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>But hold on a second there, code-monkey. Before you get out your big red crayon and start coloring in visible regions, we need to back up and figure out what we mean by &#8220;project radial lines&#8221;. It&#8217;s easy to say we&#8217;re going to do it, but how will we accomplish that, exactly? <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight5.jpg' class='insetimage'   alt='gr7_sight5.jpg' title='gr7_sight5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>We can take nice little baby-steps outward from the player, testing for wall collisions as we go. But if our visible range is, say, 4 units, and we&#8217;re taking steps of 0.1 or so, then it will take us 40 steps to get from the player to the edge of their vision, assuming we don&#8217;t meet any walls along the way.  Assuming we&#8217;re projecting 360 radial lines (the image above is actually 2 degrees per line, for 180 individual lines) then that&#8217;s 14,400 little collision checks. This is not something to be done lightly, particularly not when you&#8217;re aiming for 60 frames a second. <\/p>\n<p>We can make the steps larger, but then we run into this problem:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight6.jpg' class='insetimage'   alt='gr7_sight6.jpg' title='gr7_sight6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;ve replaced the normal cave wall texture with a texture that shows the actual shape of the walls, just so we can see what we&#8217;re doing.<\/p>\n<p>If our steps are too big, we run the risk of stepping over a narrow section of wall. One collision check happens on the near side of the wall, and the next one lands on the far side, and so we never run into it.  This means the player will see through the wall somewhat unpredictably. As they zip around, those little collision dots will sometimes strike the wall and sometimes hop over it, and so that triangle will alternate between blocking sight and being transparent. <\/p>\n<p>This won&#8217;t do.  We&#8217;re doing too much work, and even at this herculean level of effort we&#8217;re still not accurate enough. Also, we have this problem:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight7.jpg' class='insetimage'   alt='gr7_sight7.jpg' title='gr7_sight7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That light boundary shows where the points are stopping when they do finally crash into the wall.  Since they&#8217;re travelling outward from the player and since we&#8217;re taking big steps, those points kind of penetrate the walls in odd patterns. That light boundary will wiggle slightly as the player alters their distance from the wall. <\/p>\n<p>What we need is to make our radial boundary to be much more accurate while also making it do fewer checks. <\/p>\n<p>A first easy step is to just look at the space we&#8217;re moving through. Remember that the world is built on a grid of squares. We can hop along, taking giant 1-unit steps. But if we look and see that the next hop will land us in a square with SOME form of wall geometry in it, then we instead take smaller steps. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight8.jpg' class='insetimage'   alt='gr7_sight8.jpg' title='gr7_sight8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That solves the really ugly problem where sitting in an open chamber would perform some horrific number of checks. Now we need to refine our collision and have it stop when it hits the edge of a wall, not somewhere below the surface. <\/p>\n<p>What you do is you have it step forward until it hits a wall. Once it does, you go into collision mode, where you&#8217;re looking for the edge. You begin stepping backwards. Every time you pass through the wall (if you were hitting last hop and not this hop, or vice-versa) you reverse direction.  Each step is half the distance of the previous one. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_half_step.jpg' class='insetimage'   alt='gr7_half_step.jpg' title='gr7_half_step.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This lets you zero in on the edge with respectable accuracy.  The more hops you&#8217;re willing to do, the more perfect the edge will be.  We just need the edge to not wiggle around in distracting ways, so 2 or 3 collision hops is probably plenty. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight9.jpg' class='insetimage'   alt='gr7_sight9.jpg' title='gr7_sight9.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>We started out with the daunting task of doing 14,400 collision checks.  With all of this in place we can do the same job in ~1,200.  <\/p>\n<p>We&#8217;re halfway there. Whew.<\/p>\n<p>I get really nervous doing this kind of prototyping. I&#8217;m wary of things that have a large up-front cost and I won&#8217;t know if they&#8217;ll pan out until I&#8217;m nearly done. The eyes that I added <a href=\"?p=20774\">last time<\/a> were a trifle. If a fifteen minute change doesn&#8217;t work out, then it&#8217;s no big deal. But here we have a big complicated undertaking with multiple moving parts, performance concerns, and artistic worries. <\/p>\n<p>I could get all the way to the end and find out this looks horrible. I might find out it looks okay, but there&#8217;s some side-effect I didn&#8217;t take into account that makes it impractical. Or maybe I&#8217;ll discover it looks great, works fine, but <em>isn&#8217;t any dang fun<\/em>.  <\/p>\n<p>Sometimes it&#8217;s hard to tell, even when you get to the end. If it doesn&#8217;t work I have to decide if it&#8217;s a good idea that needs more fussing or if it&#8217;s just a fundamentally flawed idea that should be scrapped. Or perhaps the idea is good, but my implementation is crap? Maybe a lines-of-sight mode would be groovy but my idea of projecting lines is terrible?<\/p>\n<p>Well, we won&#8217;t know for sure until we finish. Back to work.<\/p>\n<p>Now we have a polygon region that defines everything we should be able to see. Now all we have to do is make sure that&#8217;s all that gets drawn.  For this we use the OpenGL stencil buffer.<\/p>\n<p>The stencil buffer is a strange beast. The stencil buffer lets you do stuff like:<\/p>\n<ol>\n<li>Okay OpenGL, I&#8217;m going to write to the stencil buffer now. (Draw a bunch of polygons.)\n<li>Okay, I&#8217;m done writing to the buffer now. From now on, I want to only draw polygons in the region I just covered. (Draw other polygons.)\n<\/ol>\n<p>You can write to the stencil buffer. You can write to only certain bits in the stencil buffer. You can draw using the stencil as a mask. You can draw only using certain bits as a mask. You can write and draw at the same time, using different bit patterns, in order to both add to and subtract from the mask at the same time.  You can draw to the stencil buffer but not the viewport. You can draw to the stencil buffer, and also the viewport, and you can make all of this conditional on other, tangentally related systems. <\/p>\n<p>There are a lot of moving parts. There are a lot of flags to set and values to define. A lot can do wrong, and if you mess up you&#8217;ll generally end up drawing everything or nothing, which isn&#8217;t very helpful in spotting the problem.  The thing is so confusing that I can never remember how it works. I end up reading the <a href=\"http:\/\/nehe.gamedev.net\/tutorial\/clipping__reflections_using_the_stencil_buffer\/17004\/\">NeHe stencil buffer tutorial<\/a> every dang time. <\/p>\n<p>But I get it working. And when I draw the shape to the stencil buffer it ends up shaped 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\/gr7_sight10.jpg' class='insetimage'   alt='gr7_sight10.jpg' title='gr7_sight10.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>You can see it&#8217;s still clipping through the walls a tiny bit. That&#8217;s fine. It doesn&#8217;t need to be pixel perfect. Once we get our normal wall texture in here you won&#8217;t be able to tell. <\/p>\n<p>I draw in that region with a texture that gets darker on the edges, so it looks like I&#8217;m shining a light:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight11.jpg' class='insetimage'   alt='gr7_sight11.jpg' title='gr7_sight11.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Get rid of this debug texture and put some robots in.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr7_sight12.jpg' class='insetimage'   alt='gr7_sight12.jpg' title='gr7_sight12.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This accomplishes exactly what I was hoping. Robots can ambush you and actually take you by surprise, and when they duck behind a wall you can&#8217;t tell when and where they will pop up again. Gameplay is a little more paranoid and a little more surprising.  <\/p>\n<p>It&#8217;s kind of rare for an idea to pay off on the first try like this, but this is good enough that I&#8217;ve decided it&#8217;s a core part of the game. I&#8217;d be willing to cut other planned features if it means I get to keep this one.<\/p>\n<p>Well, they don&#8217;t always work out this well, but it&#8217;s nice when they do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I play the game, I get this idea that a lot of AI problems are probably due to asymmetrical vision. Not all of them. It&#8217;s not that easy to make great AI. But there&#8217;s something inherently derpy about an enemy when you can see them and they can&#8217;t see you. There&#8217;s a 90&#8217;s movie [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-20777","post","type-post","status-publish","format-standard","hentry","category-good-robot"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20777","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=20777"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20777\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}