{"author_name":"yangamedev","cat":"MiniLD","comments":[{"author_name":"MiniBobbo","time":"July 25, 2013 9:06 am","epoch":1374761160,"text":"It has been interesting seeing how you tackled this problem. It is one I&#8217;ve been pondering also. I like your implementation and might have to steal the basics.","spam":"N"}],"epoch":1374724260,"likes":3,"metadata":{"p_key":"42292","p_author":"yangamedev","p_authorkey":"20928","p_urlkey":"77915","p_title":"Out of Sight","p_cat":"MiniLD","p_event":"LD27","p_time":"1374724260","p_likes":"3","p_comments":"1","p_status":"UPD5","us_key":"20928","us_name":"yangamedev","us_username":"yangamedev","event_start":"1377216000","event_key":"18","event_name":"LD27"},"text":"<p>Last time I showed off my 162-unit RTS at a stunning 5 seconds per frame. Not FPS, that&#8217;s SPF. And that ain&#8217;t good.<\/p>\n <p>Clearly it&#8217;s not scaling well as I crank up the units. What we&#8217;ve got here is your classic O(n^2) problem between entities. There&#8217;s a bevy of literature out there regarding space-division in a proper manner for these types of issues, but I had an idea.<\/p>\n <p>The game literature out there deals with LOS for terrain but not for dynamic entities. I decided to roll my own: introducing the Visibility Matrix(tm):<\/p>\n <div id=\"attachment_263436\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/darkness.png\"><img class=\"size-medium wp-image-263436 \" alt=\"darkness\" src=\"http:\/\/www.ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/darkness-300x214.png\" width=\"300\" height=\"214\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/darkness-300x214.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/darkness-550x393.png 550w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/darkness.png 976w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p class=\"wp-caption-text\">Anybody there? &#8220;None.&#8221; Seems legit, Bob you&#8217;re on point.<\/p><\/div>\n <p>&nbsp;<\/p>\n <p>You&#8217;re blue, the enemy is red but hidden (left the text on to show their position). I turned off the world so it can read better, but remember there&#8217;s a slab in the middle of the world blocking each other&#8217;s view. Each cell refers to the visibility between the two actors. The &#8220;oo&#8221;&#8216;s mean &#8220;can see&#8221;&#8230;the &#8220;&#8211;&#8220;&#8216;s mean &#8220;can&#8217;t see&#8221;.<\/p>\n <p>&nbsp;<\/p>\n <div id=\"attachment_263434\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/during.png\"><img class=\"size-medium wp-image-263434 \" alt=\"during\" src=\"http:\/\/www.ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/during-300x214.png\" width=\"300\" height=\"214\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/during-300x214.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/during-550x393.png 550w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/during.png 976w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p class=\"wp-caption-text\">Good ol&#8217; Bob. Never complained about recon.<\/p><\/div>\n <p style=\"text-align: center\">\n <p>&nbsp;<\/p>\n <p>Visibility between Blue:7 and Red:0-5 has been established in the Visibility Matrix, and the units can query these values rather than generate it on their own expensively.<\/p>\n <p>What makes this faster is these vis checks are\u00a0<em>time-sliced<\/em>, i.e. only a few of them are updated every frame instead of the O(n^2) nightmare from the last post. To achieve this we simply walk through the matrix left-&gt;right, top-&gt;bottom, updating each cell with a fresh LOS.<\/p>\n <p>How many cells do we update each frame? Well if there&#8217;s\u00a0<strong>n<\/strong>\u00a0units, then the Visibility Matrix only needs <strong>n * (n &#8211; 1)<\/strong> <strong>\/ 2<\/strong> cells, as visibility is <a title=\"commutative\" href=\"https:\/\/en.wikipedia.org\/wiki\/Commutative_property\">commutative<\/a>\u00a0and we assume each actor can see themselves. Best of all we \u00a0guarantee that no enemy can see you before you see them, so no ambushes just a little lag.<\/p>\n <p>Finally if we demand a\u00a0<strong>maximum response time<\/strong> for the unit in frames, then the number of checks performed each frame to guarantee an update = <strong>max frames \/ # cells<\/strong>. At 60fps and a 1 second response time your calls are spread out over 60 frames.<\/p>\n <p>&nbsp;<\/p>\n <p><a href=\"http:\/\/www.ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/pincers.png\"><img class=\"size-medium wp-image-263435 aligncenter\" alt=\"pincers\" src=\"http:\/\/www.ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/pincers-300x214.png\" width=\"300\" height=\"214\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/pincers-300x214.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/pincers-550x393.png 550w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2013\/07\/pincers.png 976w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n <p>Here&#8217;s the same 162 units rocking it out at a smoking 9fps. I&#8217;m calling it 10fps, that&#8217;s a 50x framerate increase.<\/p>\n <p>Where this really takes off is I can store\u00a0<strong>more<\/strong> than just vis data in there &#8212; distance calculations, health differential, whose turn it is to do the dishes, whatever I&#8217;d normally have to generate at runtime about information for each pair of actors.<\/p>\n <p>Once I move all these calculations to the server I hope to throttle up a gear in terms of units. But maybe I should make a game first&#8230;<\/p>","time":"July 24th, 2013 10:51 pm","title":"Out of Sight"}