{"id":396,"date":"2009-09-04T11:44:49","date_gmt":"2009-09-04T18:44:49","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2114"},"modified":"2009-09-04T11:44:49","modified_gmt":"2009-09-04T18:44:49","slug":"quickbox2d-groupindex-platforms","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=396","title":{"rendered":"QuickBox2D groupIndex Platforms"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nimport com.actionsnippet.qbox.*;\r\nimport Box2D.Common.Math.*;\r\nimport Box2D.Collision.Shapes.*;\r\n\r\n&#x5B;SWF(width = 800, height = 600, backgroundColor = 0x000000)]\r\n\r\nvar sim:QuickBox2D = new QuickBox2D(this);\r\n\r\nsim.createStageWalls();\r\n\r\nvar boxNum:int = 8;\r\nvar boxes:Array = &#x5B;];\r\nvar xp:Number, yp:Number, w:Number;\r\nfor (var i:int = 0; i&lt;boxNum; i++){\r\n\txp = 3 + Math.random() * 10;\r\n\typ = 4 + i * 2;\r\n\tw = 2 + Math.random() * 3;\r\n\tboxes.push(sim.addBox({x: xp, y: yp, width: w, height: 0.5, density:0, groupIndex:-1, fillColor:0xFF6532}));\r\n}\r\n\r\nvar char:QuickObject = sim.addBox({x:2, y:18, width:1, height:2, allowSleep:false, groupIndex:-1, fillColor:0x0099CC});\r\n\r\nsim.start();\r\n\r\nvar charVel:b2Vec2;\r\nvar charVelAng:Number;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t \r\n\tcharVel = char.body.GetLinearVelocity();\r\n\tcharVelAng =  char.body.GetAngularVelocity();\r\n\tvar filter:b2FilterData;\r\n\t\r\n\tif (key&#x5B;Keyboard.RIGHT]){\r\n\t\tcharVel.x += 1\r\n\t\tchar.body.SetLinearVelocity(charVel);\r\n\t\tcharVelAng += 1;\r\n\t\tchar.body.SetAngularVelocity(charVelAng);\r\n\t}\r\n\tif (key&#x5B;Keyboard.LEFT]){\r\n\t\tcharVel.x -=1;\r\n\t\tchar.body.SetLinearVelocity(charVel);\r\n\t\tcharVelAng -= 1;\r\n\t\tchar.body.SetAngularVelocity(charVelAng);\r\n\t}\r\n\tif (key&#x5B;Keyboard.UP] &amp;&amp; sim.w.GetContactCount() &gt; 0){\r\n\t\t charVel.y = -10;\r\n\t\t trace(charVel.y);\r\n\t\t char.body.SetLinearVelocity(charVel);\r\n\t\t charVelAng *= 0.8;\r\n\t\t char.body.SetAngularVelocity(charVelAng);\r\n\t}\r\n\tfor (var i:int = 0; i&lt;boxes.length; i++){\r\n\t\tvar rect:Rectangle = char.userData.getRect(this);\r\n\t\tif (rect.bottom \/ 30 &lt; boxes&#x5B;i].y){\r\n\t\t\tfilter = boxes&#x5B;i].shape.GetFilterData();\r\n\t\t\tfilter.groupIndex = 1;\r\n\t\t\tboxes&#x5B;i].shape.SetFilterData(filter);\r\n\t\t}else{\r\n\t\t\tfilter = boxes&#x5B;i].shape.GetFilterData();\r\n\t\t\tif (filter.groupIndex != -1){\r\n\t\t\t\tfilter.groupIndex = -1;\r\n\t\t\t\tboxes&#x5B;i].shape.SetFilterData(filter);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\nvar key:Object = new Object();\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);\r\nstage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);\r\nfunction onKeyPressed(evt:KeyboardEvent):void{ key&#x5B;evt.keyCode] = true; }\r\nfunction onKeyReleased(evt:KeyboardEvent):void{ key&#x5B;evt.keyCode] = false; }\r\n<\/pre>\n<p><i>NOTE: This requires the <a href=\"https:\/\/actionsnippet.com\/?page_id=1391\" target=\"blank\">QuickBox2D<\/a> mini-library<\/i><\/p>\n<p>This snippet shows how to use groupIndex to create simple platforms for a character to jump on.  A few people have asked me about this so I figured I&#8217;d post about it. It works by altering the groupIndex of each platform based on the characters position &#8211; if the character is below a platform, both the character and the platform have groupIndex -1 (meaning they will not collide). As soon as the character is above a platform, the platforms groupIndex is set to 1 (allowing the character to walk on the platform despite having just passed through it.<\/p>\n<p><a data-asruffle-linked=\"true\" href=\"http:\/\/www.actionsnippet.com\/swfs\/groupIndex_platforms.html\" target=\"blank\"><br \/>\nHave a look at the swf&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/groupindex_platforms.jpg\" \/><br \/>\n<\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/groupIndex_platforms.swf&quot;,&quot;title&quot;:&quot;groupIndex platforms&quot;,&quot;width&quot;:800,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#000000&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: groupIndex platforms<\/span><\/button><\/span><\/p>\n<p>It&#8217;s also worth noting that I&#8217;m using b2World.GetContactCount() to make sure it&#8217;s ok to jump&#8230; if there were more going on in the demo you&#8217;d need to check a specific contact point(s), but since the only collision that could happen involves the character this works nicely&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import com.actionsnippet.qbox.*; import Box2D.Common.Math.*; import Box2D.Collision.Shapes.*; &#x5B;SWF(width = 800, height = 600, backgroundColor = 0x000000)] var sim:QuickBox2D = new QuickBox2D(this); sim.createStageWalls(); var boxNum:int = 8; var boxes:Array = &#x5B;]; var xp:Number, yp:Number, w:Number; for (var i:int = 0; i&lt;boxNum; i++){ xp = 3 + Math.random() * 10; yp = 4 + i * 2; w [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[3,14,15,4,16],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-box2d","category-quickbox2d","tag-actionscript","tag-as3","tag-box2d","tag-flash","tag-quickbox2d"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}