{"id":409,"date":"2009-09-16T06:51:17","date_gmt":"2009-09-16T13:51:17","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2201"},"modified":"2009-09-16T06:51:17","modified_gmt":"2009-09-16T13:51:17","slug":"quickbox2d-contacts-part-3","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=409","title":{"rendered":"QuickBox2D Contacts Part 3"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nimport com.actionsnippet.qbox.*;\r\nimport Box2D.Common.Math.*;\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\nsim.createStageWalls();\r\n\r\nvar cup:QuickObject = sim.addPoly({x:3, y:13, wireframe:false, density:0, \r\n\t\t\t\t\t\t\t\t   points:&#x5B;0,0, .5, 0, .5,3,  2,3,  2,0,  2.5,0, 2.5, 3.5, 0, 3.5, 0,0]});\r\nvar deleter:QuickObject = sim.addBox({x:4.251, y: 15.9, width:1.5, height:0.25, fillColor:0xFF0000});\r\n\r\n\/\/ use a distance joint instead of a group since isCurrentContact \r\n\/\/ uses the QuickObject.body property\r\nsim.addJoint({a:cup.body, b:deleter.body, x1:deleter.x, y1:deleter.y+0.5});\r\n\r\nvar circles:Array = &#x5B;];\r\nfor (var i:int = 0; i&lt;10; i++){\r\n\tcircles&#x5B;i] = sim.addCircle({x:8 + i, y:5, radius:0.5});\r\n}\r\nvar boxes:Array = &#x5B;];\r\nfor (i = 0; i&lt;10; i++){\r\n\tboxes&#x5B;i] = sim.addBox({x:8.5 + i, y:10, width:0.5, height:0.5});\r\n}\r\n\r\nsim.start();\r\nsim.mouseDrag();\r\n\r\nvar shootVec:b2Vec2 = new b2Vec2(4, -30);\r\n\r\n\/\/ when circles are dropped in the cup they are destroyed\r\n\/\/ when boxes are droppped in the cup their linear velocity is altered\r\nvar contacts:QuickContacts = sim.addContactListener();\r\ncontacts.addEventListener(QuickContacts.ADD, onAdd);\r\nfunction onAdd(evt:Event):void{\r\n\tfor (var i:int = 0; i&lt;circles.length; i++){\r\n\t\tvar circ:QuickObject = circles&#x5B;i];\r\n\t\tif (contacts.isCurrentContact(circ, deleter)){\r\n\t\t\t\/\/ it&#039;s ok to destroy QuickObject here because they are only actually\r\n\t\t\t\/\/ destroyed after the timeStep has finished at the end of QuickBox2D&#039;s \r\n\t\t\t\/\/ internal loop\r\n\t\t\tcirc.destroy();\r\n\t\t}\r\n\t } \r\n\t for (i = 0; i&lt;boxes.length; i++){\r\n\t\tvar box:QuickObject = boxes&#x5B;i];\r\n\t\tif (contacts.isCurrentContact(box, deleter)){\r\n\t\t\tbox.body.SetLinearVelocity(shootVec);\r\n\t\t}\r\n\t }\r\n}\r\n<\/pre>\n<p><i>Note: This snippet requires <a href=\"https:\/\/actionsnippet.com\/?page_id=1391\" target=\"blank\">QuickBox2D 1.0 or greater<\/a><\/i><\/p>\n<p>Here is another example of using QuickBox2D style contacts. In this example there is a cup with a small red box inside of it. When circles are placed inside the cup they are destroyed. When boxes are placed inside the cup they are shot upward. <\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/contacts_part3.html\" target=\"blank\"><br \/>\nHave a look at the swf&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/contacts3.gif\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/contacts_part3.swf&quot;,&quot;title&quot;:&quot;contacts part3&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\">: contacts part3<\/span><\/button><\/span><\/p>\n<h1>A Note About Creation and Destruction<\/h1>\n<p>It&#8217;s worth noting that in Box2D your not supposed to destroy or create rigid bodies within the listener function for any contact events. QuickBox2D gets around this restriction by flagging the rigid body for destruction and destroying it at the end of QuickBox2D&#8217;s internal loop. This is not the case with creation of rigid bodies &#8211; if you attempt to create a rigid body within one of these listener functions you will get an error. See <a href=\"https:\/\/actionsnippet.com\/?p=2179\">QuickBox2D Contacts Part 1<\/a> for an example of creating rigid bodies when there is a collision.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import com.actionsnippet.qbox.*; import Box2D.Common.Math.*; &#x5B;SWF(width = 800, height = 600, backgroundColor = 0x222222, frameRate=60)] var sim:QuickBox2D = new QuickBox2D(this, {debug:false}); sim.createStageWalls(); var cup:QuickObject = sim.addPoly({x:3, y:13, wireframe:false, density:0, points:&#x5B;0,0, .5, 0, .5,3, 2,3, 2,0, 2.5,0, 2.5, 3.5, 0, 3.5, 0,0]}); var deleter:QuickObject = sim.addBox({x:4.251, y: 15.9, width:1.5, height:0.25, fillColor:0xFF0000}); \/\/ use a distance joint instead [&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-409","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\/409","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=409"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}