{"id":406,"date":"2009-09-14T12:29:42","date_gmt":"2009-09-14T19:29:42","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2179"},"modified":"2009-09-14T12:29:42","modified_gmt":"2009-09-14T19:29:42","slug":"quickbox2d-contacts-part-1","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=406","title":{"rendered":"QuickBox2D Contacts Part 1"},"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 = 0x000000, frameRate=60)]\r\n\r\nvar sim:QuickBox2D = new QuickBox2D(this, {debug:false, frim:true});\r\n\r\nsim.createStageWalls();\r\n\r\nvar boxA:QuickObject = sim.addBox({x:3, y:3, width:2, height:2, fillColor:0xFF0000});\r\nvar boxB:QuickObject = sim.addBox({x:3, y:6, width:2, height:2, fillColor:0xFF0000});\r\n\r\nsim.start();\r\nsim.mouseDrag();\r\n\r\n\/\/ when boxA touches boxB a circle QuickObject is created\r\nvar contacts:QuickContacts = sim.addContactListener();\r\n\/\/ listen for contact points being added\r\ncontacts.addEventListener(QuickContacts.ADD, onAdd);\r\nfunction onAdd(evt:Event):void{\r\n\t\/\/ see if this contact event is associated with boxA and boxB\r\n\tif(contacts.isCurrentContact(boxA, boxB)){\r\n\t\t\/\/ get the location of the collision in world space\r\n\t\tvar loc:b2Vec2 = contacts.currentPoint.position;\r\n\t\t\/\/ you cannot create new QuickObjects inside this listener function\r\n\t\t\/\/ so we just give a 5 ms delay\r\n\t\tsetTimeout(sim.addCircle, 5, {x:loc.x, y:loc.y});\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>I&#8217;ve created a few simple examples to show how to use QuickBox2D contact listeners. This is the first one. When two boxes collide circles are added to the simulation at the point of collision.<\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/contacts_part1.html\" target=\"blank\"><br \/>\nHave a look at the swf&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/contactpoints1.jpg\" \/><\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/contacts_part1.swf&quot;,&quot;title&quot;:&quot;contacts part1&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\">: contacts part1<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import com.actionsnippet.qbox.*; import Box2D.Common.Math.*; &#x5B;SWF(width = 800, height = 600, backgroundColor = 0x000000, frameRate=60)] var sim:QuickBox2D = new QuickBox2D(this, {debug:false, frim:true}); sim.createStageWalls(); var boxA:QuickObject = sim.addBox({x:3, y:3, width:2, height:2, fillColor:0xFF0000}); var boxB:QuickObject = sim.addBox({x:3, y:6, width:2, height:2, fillColor:0xFF0000}); sim.start(); sim.mouseDrag(); \/\/ when boxA touches boxB a circle QuickObject is created var contacts:QuickContacts = sim.addContactListener(); \/\/ [&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-406","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\/406","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=406"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/406\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}