{"id":20774,"date":"2013-08-28T03:42:16","date_gmt":"2013-08-28T08:42:16","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20774"},"modified":"2015-07-01T03:44:21","modified_gmt":"2015-07-01T08:44:21","slug":"project-good-robot-part-6-controls","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20774","title":{"rendered":"Project Good Robot 6: Controls"},"content":{"rendered":"<p>I apologize if this series seems to be glossing over some details while exploring others in exhaustive detail. This is a very hard series to write and I&#8217;m having trouble keeping it all straight in my head.<\/p>\n<p>At any given moment there&#8217;s the stuff I&#8217;m working on and thinking about.  Lagging behind that activity by about three weeks is the stuff I&#8217;ve written about and organized into words. And lagging behind that by another week is what has actually been posted to the blog.  So when organizing my thoughts I have to figure out if feature X is something I&#8217;ve done, something I&#8217;ve documented, and something you know about. <\/p>\n<p>It&#8217;s confusing, is what I&#8217;m getting at.<\/p>\n<p>One one hand, these programming posts are really useful for documenting and clarifying my thoughts. On the other hand, having this muti-stage process with three weeks of lag time is not so useful. Because of this, I&#8217;m really eager to plow through this early stuff as fast as possible. Do I document all the little diversions and side-paths that didn&#8217;t work out? If so, then I&#8217;ll never get caught up. But if I leave that stuff out then this threatens to became a very dry recounting of features added. <\/p>\n<p>I still don&#8217;t know how to handle this. <\/p>\n<p>And now I&#8217;m about to make the problem worse. Here is a feature from day three. It took me longer to document in this post than it did to write the feature in the first place:<\/p>\n<p><!--more-->It was many hours into my first play-through of Plants vs. Zombies before I realized, &#8220;Wow. <strong>Every single object<\/strong> in this game has a face.&#8221; Every plant, every weapon in your arsenal has at least a pair of eyes to give it some personality.  <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr2_descent.jpg' class='insetimage'   alt='Descent.' title='Descent.'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;m kind of hoping I can use this idea to give the game some character. Descent put eyes on their robots and I have to say it made a huge difference. They would have been much less interesting if they just looked like flying vehicles. I&#8217;m hoping that if I give them faces (or at least eyes) then the player will fill in the gaps and ascribe personalities to them based on their behavior and facial &#8220;expression&#8221;. Ideally, the suicide bomber foes should look kind of crazed while the ones that shoot at a distance should look kind of cunning and maybe slow-moving foes would look kind of derp.  That&#8217;s the idea, anyway. <\/p>\n<p>To keep the eyes from looking like simple running lights, I hope to make them move around a bit. <\/p>\n<p>So I make a new type of object called &#8220;eyes&#8221;.  To start with, it just renders this little circle of color:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_eye1.jpg' class='insetimage'   alt='gr6_eye1.jpg' title='gr6_eye1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I don&#8217;t know if I actually want to use that color banding. We&#8217;ll see. In any case, we need an iris (or a pupil, the physiology of cartoon robot eyes is a little sketchy) that moves around in this circle.  It&#8217;s not enough to just draw a bright dot and call it a day. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_eye2.jpg' class='insetimage'   alt='gr6_eye2.jpg' title='gr6_eye2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The illusion of this being an eye is broken, and it looks like a firefly zipping around in front of a round window. The dot needs to be constrained to the area we&#8217;ve already drawn. You can accomplish this with the depth buffer.<\/p>\n<p>How it normally works is that you draw some polygons to the screen and OpenGL (or whatever you&#8217;re using for drawing) writes to the depth buffer. This keeps track of how far away from the camera each pixel is. When you draw more polygons later, they only show up if they end up closer to the camera than the stuff that&#8217;s already drawn.  This is how we can draw a scene and have some objects appear to be behind others. <\/p>\n<p>But we can change this behavior if we want. We can tell it to only draw if the new polygons and the old polygons are at exactly the same depth. Doing it this way, the iris only shows up where it overlaps with the eye:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_eye3.jpg' class='insetimage'   alt='gr6_eye3.jpg' title='gr6_eye3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Up until now my bad robots have just been silhouettes with some running lights or simple fixed dots for eyes. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_robots1.jpg' class='insetimage'   alt='gr6_robots1.jpg' title='gr6_robots1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And now we add the eyes:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_robots2.jpg' class='insetimage'   alt='gr6_robots2.jpg' title='gr6_robots2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Some eyes sweep back and forth, like Cylons.  Some point the way the robot is going, like Pac-Man ghosts. And some follow the player, like creepy stalkers. <\/p>\n<p>Hm.<\/p>\n<p>The difference is not nearly as noticeable or attention-grabbing as I&#8217;d hoped it would be. In my mind it was going to be this OH WOW effect that would bring the robots to life. In practice it&#8217;s kind of subtle. But it&#8217;s good. (Enough. (For now.))<\/p>\n<p>Now we need a character for the player. <\/p>\n<p>I went through a lot of designs over the course of a week or so.  I didn&#8217;t take screenshots of the early versions, but the figure in the lower right is what I settled on:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_player.jpg' class='insetimage'   alt='gr6_player.jpg' title='gr6_player.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The player is a robot, and their character is a little bit cute and a little more &#8220;human&#8221; than the robots they&#8217;re fighting. I was aiming for gender-less. Not sure how well I succeeded. There&#8217;s nothing about it that screams out &#8220;THIS IS A MAN ROBOT&#8221; to me, at any rate. <\/p>\n<p>It&#8217;s called an &#8220;avatar&#8221; internally, a habit I picked up at Activeworlds where all human-controlled models were called that. Outside of the source, there&#8217;s no official name for it. The avatar is made of several different sprites instead of a single fixed whole.  This means that as they zip around the level, the torso can lean into the turns or lag behind ever so slightly when accelerating. This puts a lot of life into the little thing. The avatar has animated eyes like the other robots, although the avatar eyes look whichever way the mouse is pointing \/ aiming.  <\/p>\n<p>The left (from your perspective) arm shoots lasers, and the right arm shoots missiles. And now I realize I added missiles to the game without mentioning it. I also didn&#8217;t mention them in the next entry, which is already written but which you won&#8217;t see until Friday. <\/p>\n<p>See what I mean about this being confusing?<\/p>\n<p>Gah. Here we are at the end of an entry and I&#8217;ve managed to document ONE feature. Considering at this point in the project I was adding a half dozen features a day, I was working 6 days a week,  and these blog entries only come out three times a week, this is clearly unsustainable. We will never catch up like this. <\/p>\n<p>So you get a double-sized entry today. Let&#8217;s talk about game controllers. The only controller I have is the USB XBox controller for the PC:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_controller1.jpg' class='insetimage'   alt='gr6_controller1.jpg' title='gr6_controller1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I did have another off-brand controller around here, but I seem to have misplaced it in <a href=\"?p=18946\">the move<\/a>. For the purposes of what we&#8217;re doing here, it doesn&#8217;t really matter.<\/p>\n<p>For the record: I&#8217;m talking to the joystick through <a href=\"http:\/\/www.libsdl.org\/\">SDL<\/a>. SDL is nice, clean, lightweight, and cross-platform. I could also talk to the joysticks through the Direct X library, which I think is called Direct Input. Or maybe I&#8217;m remembering wrong. It&#8217;s been more than a decade. At any rate, this would marry my project to Direct X, which is massive, complex, and would limit me to platforms supported by Microsoft. It would also give my game a bad case of DirectX dependency. Many of you have no doubt installed a game and ran into the situation where it said, &#8220;Installing Direct X step 1 of N&#8221; even though Direct X was up to date, in which case you&#8217;ve seen <a href=\"http:\/\/www.rockpapershotgun.com\/2011\/08\/03\/why-steam-makes-you-install-directx-repeatedly\/\">this terrible sickness<\/a>. <\/p>\n<p>Basically there are an endless number of different versions of Direct X. There are versions and sub-versions and in-between versions and each of them has their own binaries. Games are usually built against one particular version, and getting the various nearly identical but ever-so-slightly distinct versions to coexist is apparently a major headache. <\/p>\n<p>So screw DirectX, is what I&#8217;m getting at. <\/p>\n<p>Here is how the controller looks to the software engineer:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_controller2.jpg' class='insetimage'   alt='gr6_controller2.jpg' title='gr6_controller2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Yes, the trigger buttons are actually a single analog stick. If neither trigger is down, then the stick is in the neutral position. If one trigger is down then this &#8220;stick&#8221; is pressed one way and if the other trigger is down it&#8217;s going the other.  If you squeeze both triggers at once they cancel each other out. This is kind of interesting. I didn&#8217;t know that about the trigger buttons. That does place limits on how we can use it. I don&#8217;t know if this is how it&#8217;s &#8220;supposed&#8221; to work, or if this is a limitation of interfacing with a Microsoft controller using a non-Microsoft interface. It does seem strange to design a controller with trigger buttons that are mutually exclusive. <\/p>\n<p>The green X brand button in the middle of the controller is invisible to me. If I was using Direct Input I could probably see it, but since I&#8217;m talking to the hardware through SDL I don&#8217;t know it&#8217;s there and I can&#8217;t tell if the user is pressing it. But who cares? <\/p>\n<p>The d-pad is technically a &#8220;hat&#8221; control. You probably remember hat controls from those mid-90&#8217;s joysticks:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr6_hat.jpg' class='insetimage'   alt='gr6_hat.jpg' title='gr6_hat.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I imagine the reason for this distinction is that while the hat can technically be thought of as four direction buttons, in practice they have a limited number of mutually exclusive states. You can&#8217;t physically push the hat right and left at the same time. You can&#8217;t press both &#8220;hat up&#8221; and &#8220;hat down&#8221;.<\/p>\n<p>And while all of this is kind of interesting and important to people who design these input devices, it&#8217;s all very tedious and beside the point to a would-be game developer. When I&#8217;m trying to figure out if the user is pressing a &#8220;go right&#8221; button, I really don&#8217;t care if that button is a joystick button, a hat button, an analog stick, a keyboard button, or a mouse button.  All I care about is if they are doing it.  <\/p>\n<p>In terms of physical hardware, we have seven distinct forms of input:<\/p>\n<ol>\n<li>Analog stick.\n<li>Hat control.\n<li>Joystick buttons.\n<li>Mouse movement.\n<li>Mouse buttons.\n<li>Keyboard standard keys.\n<li>Keyboard special keys like shift, control, alt, or numlock.\n<\/ol>\n<p>Each of these input types has a unique way of querying it. Sometimes SDL tells me, but sometimes I have to ask. Sometimes the answer is obvious and easy to read, and sometimes the answer is complex and needs to undergo some sort of scaling and conversion. (Like mouse speed or joystick dead zone, for example.) But in practice, there are only three distinct forms of input:<\/p>\n<ol>\n<li>Analog stick\n<li>Mouse movement\n<li>Buttons. Any button.\n<\/ol>\n<p>It&#8217;s really inconvenient to have to poll all these different input channels, so what we need is a system of virtual buttons. I create a layer of pretend buttons and we feed all these disparate input channels into this one list of inputs.  While the user is playing, I can query if they&#8217;re pressing button X without concerning myself with where X is physically located or what sort of button it is.<\/p>\n<p>This also lets me create new abstract or amalgamated buttons. I can create a generic &#8220;joystick up&#8221; button. If the user nudges the left analog stick up and releases it again, it generates a keypress on this new imaginary button. If they hit up on the D-pad, it generates the same keypress. This means I can ask even more abstract questions, like &#8220;Is the user trying to move up?&#8221; and not worry about how they are expressing this decision.  It&#8217;s not terribly useful for gameplay, but it&#8217;s a must-have if you&#8217;re going to navigate menus. <\/p>\n<p>With all of this done, I end up with a nice clean input system. I shouldn&#8217;t need to touch it again unless this turns into an actual game, in which case I&#8217;d probably need some kind of way to remap keys, which is a complete pain in the ass to support properly. In order to let the user re-map keys, you need a robust menu system, you need to be aware of all the possible inputs even if they don&#8217;t exist on keyboards in your country, and you need to know the proper written names of those buttons so the user can see that the selection they made is the one they wanted. (The piss-easy way to do it is to just jam all those settings in a config file, which hasn&#8217;t been tolerated since the 90&#8217;s. I should know. I&#8217;m one of the people who doesn&#8217;t tolerate it.) <\/p>\n<p>One thing to note is that the hardest part of adding controller support was getting it to handle hot-plugging of joysticks. When you start up SDL, it looks around, takes an inventory of the available joysticks, and sets them up for use. But if you plug one in afterward it doesn&#8217;t notice or tell you about it. Not even if you ask. <\/p>\n<p>This is made worse by the fact that some genius at Microsoft decided that when the wireless controller loses connection it should <em>yank the device away from you as if someone physically unplugged the joystick<\/em>. I have no idea why. The receiver is still plugged in. <\/p>\n<p>This is annoyingly common. If the batteries are starting to go, that will lead to a disconnect. If you set the controller down for more than a couple of minutes it goes to sleep, which is also a disconnect. To your game, this doesn&#8217;t just mean you stop getting input.  It means the device itself vanishes. <\/p>\n<p>A lot of games (indies in particular) don&#8217;t take this into account, which means that if the device isn&#8217;t awake when you launch the game, you will never, ever get the game to acknowledge the controller. You have to exit the game and re-launch it  to use the controller. (Which sucks, since the most likely thing is for the user to launch the game and THEN pick up the controller.) In some cases, it doesn&#8217;t handle the re-connect properly which means that this disconnect will force you to relaunch the game if you want to use the controller again.<\/p>\n<p>It turns out the secret to fix this is to shut down the SDL joystick subsystem and restart it at regular intervals. Like so:<\/p>\n<pre lang=\"c\">\r\n  \/\/No joystick active. See if one showed up.\r\n  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);\r\n  SDL_InitSubSystem(SDL_INIT_JOYSTICK);\r\n  if (!SDL_NumJoysticks()) \r\n    return; \/\/Still nothin'\r\n<\/pre>\n<p>I mention this only because it was really, really hard to track this down. I found this buried deep in a forum somewhere. I didn&#8217;t even know you could shut down individual SDL subsystems like this. I have no idea if there&#8217;s a performance cost to doing this, so I only do the check every five seconds. <\/p>\n<p>With this in place, I can handle the strange comings and goings of Xbox wireless controllers without forcing the user to exit the game. <\/p>\n<p>The game feels very different with the controller. Unlike an FPS, this is more of a trade-off than a handicap. It&#8217;s a little easier to set up really precise long-distance shots with the mouse, while the analog stick just doesn&#8217;t have the same ability to make fine micro-adjustments. On the other hand, moving with an analog stick is a lot nicer than with the keyboard. WASD are obviously binary indicators, and the only way to go slow is to flutter the keys. Worse, you can only go in the 8 ordinal directions with the keyboard, while the controller allows you to fly in nice swooping curves if you want to.  <\/p>\n<p>I don&#8217;t think one is significantly easier than the other, but they do feel very different in your hands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I apologize if this series seems to be glossing over some details while exploring others in exhaustive detail. This is a very hard series to write and I&#8217;m having trouble keeping it all straight in my head. At any given moment there&#8217;s the stuff I&#8217;m working on and thinking about. Lagging behind that activity by [&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-20774","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\/20774","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=20774"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20774\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}