QuickBox2D Contact Filtering

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Dynamics.*;
  3. import Box2D.Collision.Shapes.*;
  4.  
  5. [SWF(width = 800, height = 600, backgroundColor = 0x222222, frameRate=60)]
  6.  
  7. var sim:QuickBox2D = new QuickBox2D(this, {debug:false});
  8.  
  9. var box:QuickObject = sim.addBox({x:12, y:16, width:3, height:3, density:0 , groupIndex:-1});
  10. var circle:QuickObject = sim.addCircle({x:5, y:3, radius:0.5, groupIndex:-1});
  11.  
  12. // add another box to show that Box2D is still working
  13. var littleBox:QuickObject = sim.addBox({x:12, y:3, width:1, height:1});
  14.  
  15. sim.createStageWalls();
  16.  
  17. sim.start();
  18. sim.mouseDrag();
  19.  
  20. var filter:b2ContactFilter = new QuickContactFilter();
  21. QuickContactFilter(filter).addGroupIndexCallback(onGroupNeg1, -1);
  22.  
  23. function onGroupNeg1(a:b2Shape, b:b2Shape):void{
  24.     //trace("group index -1 had a collision");
  25.     box.userData.alpha = 0.5;
  26.     setTimeout(function():void{ box.userData.alpha = 1 }, 200);
  27. }
  28.  
  29. sim.w.SetContactFilter(filter);

This snippet answers a question from a recent comment. The question was basically: "Can you detect a collision between two rigid bodies that have the same negative groupIndex?". 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: QuickContactFilter.as - It's important to note that QuickContactFilter.as is not yet part of QuickBox2D, so if you want to run this snippet you'll need to download the .as file.

You can check out the swf here...

This entry was posted in Box2D, QuickBox2D and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

15 Comments

  1. Posted September 20, 2009 at 5:32 pm | Permalink

    hi zevan,

    i find a little bug in your cool library so i post it there:

    when i want to delete a GroupObject ( extends QuickObject) with the destroy/fullDestroy method, the object disappear of the scene but is still there and still interact with others objects..

    I searched to solve it but don’t find for the moment,
    do you know what add in the method to make it works ?

    thx

  2. Posted September 20, 2009 at 8:54 pm | Permalink

    Hey bertrand,

    I haven’t noticed this bug, but will look into it and see what I find. Thanks for pointing it out….

  3. Posted September 20, 2009 at 9:02 pm | Permalink

    yeah… i see what the problem is I think … can fix it at some point tomorrow. Luckily the destroy() method works on all other object types ;)

    Thanks again for pointing this out…

  4. frankie
    Posted September 21, 2009 at 9:16 am | Permalink

    Hi, Zevan!
    Sorry for such a silly question, I`ve just begun to use QuickBox2D :) How can I change the density of some QuickObject at run-time? someQuickObject.params.density can just trace this value.

    Thanks

  5. Posted September 22, 2009 at 3:00 pm | Permalink

    Hey frankie… I have demo for this floating around. I’ll dig it up and post it here late tonight or tomorrow….

  6. Posted September 30, 2009 at 8:03 am | Permalink

    hi zevan,

    i pointed this bug few days :
    http://actionsnippet.com/?p=2218#comment-485

    and you talk about a skinning bug correction in the new QuickBox2D_v1.0.zip here :
    http://actionsnippet.com/?p=2224

    so, are you talking about the destroy method problem with GroupObject ?

    if(no) sorry, i’m too impatient, i will wait the 1.1 !

    else if(yes), bug is still there, i post a test swf there:
    http://www.swfcabin.com/open/1254320950

    the code is:

    import com.actionsnippet.qbox.*;
    import Box2D.Common.Math.*;
    import flash.utils.Timer;

    var sim:QuickBox2D = new QuickBox2D(this, {debug:true, frim:true});
    sim.createStageWalls();

    var boxA:QuickObject = sim.addBox({x:1, y:6, width:2, height:2, fillColor:0xFF0000});
    var boxB:QuickObject = sim.addBox({x:5, y:6, width:2, height:2, fillColor:0xFF0000});
    var box:QuickObject = sim.addGroup({objects:[boxA, boxB],x:1, y:6});

    var boxAfdef:QuickObject = sim.addBox({x:1, y:5, width:2, height:2, fillColor:0xFF0000});
    var boxBef:QuickObject = sim.addBox({x:5, y:5, width:2, height:2, fillColor:0xFF0000});
    var boxAgvrr:QuickObject = sim.addBox({x:1, y:1, width:2, height:2, fillColor:0xFF0000});
    var boxBzer:QuickObject = sim.addBox({x:5, y:1, width:2, height:2, fillColor:0xFF0000});

    sim.start();
    sim.mouseDrag();

    var myTimer:Timer = new Timer(4000, 1);
    myTimer.addEventListener(”timer”, action);
    myTimer.start();

    function action(e:Event):void
    {
    trace(”delete double box !”);
    boxAfdef.destroy();
    box.destroy();
    }

  7. Posted September 30, 2009 at 8:31 am | Permalink

    hey bertrand… no I haven’t fixed that bug yet… but it will be fixed for 1.1 :) thanks for the extra info, its helpful…

  8. Posted September 30, 2009 at 1:52 pm | Permalink

    ok thx
    ;-)

  9. Plumpman
    Posted October 12, 2009 at 12:34 pm | Permalink

    I was just wondering if it was possible for a custom contact filter to call the function when the collision persists rather then only when its added.

  10. Posted October 12, 2009 at 12:53 pm | Permalink

    @Plumpman … I think you can just use the standard QuickContacts in conjunction with the custom contact filtering… the persist even should still get triggered… have a look at these demos:

    http://actionsnippet.com/?s=QuickContacts&searchsubmit=Search

  11. Felix
    Posted October 23, 2009 at 4:19 pm | Permalink

    Hey Zevan, thanks for the library. @frankie has a question and i’ve the same too. How can i change a property value at runtime. Some like myCircle.params.density=mouseX; for example.

    Thanks in advance.

  12. Posted October 26, 2009 at 6:14 am | Permalink

    @Felix well, x, y and angle can be changed for any rigid body like this:

    var box:QuickObject = sim.addBox({x:4, y:4, width:1, height:1});
    box.x = 5;
    box.y = 6;
    box.angle = 0.3; // in radians

    but altering other properties relies on the Box2D style property alteration via access to the b2Body instance and other Box2D classes…

    box.body.SetMass ( b2MassData )…

    Have a glance over the docs for QuickObjects… I point out all the Box2D classes that are accessible via QuickObjects…

  13. mogly
    Posted May 26, 2010 at 6:27 am | Permalink

    Hey Zevan,

    thanks for this awesome library.

    > I was just wondering if it was possible for a custom contact filter to call the function when the collision persists rather then only when its added.

    > @Plumpman … I think you can just use the standard QuickContacts in conjunction with the custom contact filtering… the persist even should still get triggered… have a look at these demos:

    I can’t figure out how to accomplish this and it’s driving me nuts. The callback registers, but the QuickContact events aren’t triggered at all.

    ANY help would be greatly appreciated.

    mogly

  14. Posted July 23, 2010 at 9:05 am | Permalink

    hola soy de colombia
    el quickbox2D es muy bueno me ayudado mucho
    espero sigan colocando tip o trucos de como manejarlo
    gracias por los aportes

  15. Scorcher
    Posted May 10, 2011 at 10:09 pm | Permalink

    Problem solved Thanks!

One Trackback

  1. [...] 指定したgroupIndex同士の衝突を検出する – QuickBox2D Contact Filtering [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*