{"id":412,"date":"2009-09-20T09:10:39","date_gmt":"2009-09-20T16:10:39","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2218"},"modified":"2009-09-20T09:10:39","modified_gmt":"2009-09-20T16:10:39","slug":"quickbox2d-contact-filtering","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=412","title":{"rendered":"QuickBox2D Contact Filtering"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nimport com.actionsnippet.qbox.*;\r\nimport Box2D.Dynamics.*;\r\nimport Box2D.Collision.Shapes.*;\r\n \r\n&#x5B;SWF(width = 800, height = 600, backgroundColor = 0x222222, frameRate=60)]\r\n\r\nvar sim:QuickBox2D = new QuickBox2D(this, {debug:false});\r\n\r\nvar box:QuickObject = sim.addBox({x:12, y:16, width:3, height:3, density:0 , groupIndex:-1});\r\nvar circle:QuickObject = sim.addCircle({x:5, y:3, radius:0.5, groupIndex:-1});\r\n\r\n\/\/ add another box to show that Box2D is still working\r\nvar littleBox:QuickObject = sim.addBox({x:12, y:3, width:1, height:1});\r\n\r\nsim.createStageWalls();\r\n\r\nsim.start();\r\nsim.mouseDrag();\r\n\r\nvar filter:b2ContactFilter = new QuickContactFilter();\r\nQuickContactFilter(filter).addGroupIndexCallback(onGroupNeg1, -1);\r\n\r\nfunction onGroupNeg1(a:b2Shape, b:b2Shape):void{\r\n\t\/\/trace(&quot;group index -1 had a collision&quot;);\r\n\tbox.userData.alpha = 0.5;\r\n\tsetTimeout(function():void{ box.userData.alpha = 1 }, 200);\r\n}\r\n\r\nsim.w.SetContactFilter(filter);\r\n<\/pre>\n<p>This snippet answers a question from a <a href=\"https:\/\/actionsnippet.com\/?p=2114#comments\" target=\"blank\">recent comment<\/a>. The question was basically: &#8220;Can you detect a collision between two rigid bodies that have the same negative groupIndex?&#8221;. When you give QuickObjects a negative groupIndex it means that they will not collide with one another. So by default Box2D contacts will not be triggered when these two rigid bodies overlap. In order to determine if these rigid bodies overlap you need to implement a custom b2ContactFilter class. This may sound confusing but its actually very easy. You can take a look at the custom b2ContactFilter class used in this snippet here: <a href=\"https:\/\/actionsnippet.com\/source\/QuickContactFilter.as\" target=\"blank\">QuickContactFilter.as<\/a> &#8211; <em>It&#8217;s important to note that QuickContactFilter.as is not yet part of QuickBox2D, so if you want to run this snippet you&#8217;ll need to download the .as file.<\/em><\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/contact_filter.html\" target=\"blank\">You can check out the swf here&#8230; <\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/contact_filter.swf&quot;,&quot;title&quot;:&quot;contact filter&quot;,&quot;width&quot;:800,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false,&quot;backgroundColor&quot;:&quot;#222222&quot;}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: contact filter<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import com.actionsnippet.qbox.*; import Box2D.Dynamics.*; import Box2D.Collision.Shapes.*; &#x5B;SWF(width = 800, height = 600, backgroundColor = 0x222222, frameRate=60)] var sim:QuickBox2D = new QuickBox2D(this, {debug:false}); var box:QuickObject = sim.addBox({x:12, y:16, width:3, height:3, density:0 , groupIndex:-1}); var circle:QuickObject = sim.addCircle({x:5, y:3, radius:0.5, groupIndex:-1}); \/\/ add another box to show that Box2D is still working var littleBox:QuickObject = sim.addBox({x:12, y:3, [&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-412","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\/412","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=412"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}