QuickBox2D Contact Filtering

import com.actionsnippet.qbox.*;
import Box2D.Dynamics.*;
import Box2D.Collision.Shapes.*;
 
[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, width:1, height:1});

sim.createStageWalls();

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

var filter:b2ContactFilter = new QuickContactFilter();
QuickContactFilter(filter).addGroupIndexCallback(onGroupNeg1, -1);

function onGroupNeg1(a:b2Shape, b:b2Shape):void{
	//trace("group index -1 had a collision");
	box.userData.alpha = 0.5;
	setTimeout(function():void{ box.userData.alpha = 1 }, 200);
}

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…

16 comments

  1. 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. 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…

  3. 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

  4. hi zevan,

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

    and you talk about a skinning bug correction in the new QuickBox2D_v1.0.zip here :
    https://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();
    }

  5. 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.

  6. 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.

  7. @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…

  8. 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

Leave a Reply

Your email address will not be published. Required fields are marked *