QuickBox2D Contacts Part 1

  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  4. [SWF(width = 800, height = 600, backgroundColor = 0x000000, frameRate=60)]
  6. var sim:QuickBox2D = new QuickBox2D(this, {debug:false, frim:true});
  8. sim.createStageWalls();
  10. var boxA:QuickObject = sim.addBox({x:3, y:3, width:2, height:2, fillColor:0xFF0000});
  11. var boxB:QuickObject = sim.addBox({x:3, y:6, width:2, height:2, fillColor:0xFF0000});
  13. sim.start();
  14. sim.mouseDrag();
  16. // when boxA touches boxB a circle QuickObject is created
  17. var contacts:QuickContacts = sim.addContactListener();
  18. // listen for contact points being added
  19. contacts.addEventListener(QuickContacts.ADD, onAdd);
  20. function onAdd(evt:Event):void{
  21.     // see if this contact event is associated with boxA and boxB
  22.     if(contacts.isCurrentContact(boxA, boxB)){
  23.         // get the location of the collision in world space
  24.         var loc:b2Vec2 = contacts.currentPoint.position;
  25.         // you cannot create new QuickObjects inside this listener function
  26.         // so we just give a 5 ms delay
  27.         setTimeout(sim.addCircle, 5, {x:loc.x, y:loc.y});
  28.     }
  29. }

Note: This snippet requires QuickBox2D 1.0 or greater

I'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.

Have a look at the swf...

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


  1. Posted September 14, 2009 at 11:00 pm | Permalink

    Very nice work my mate. I think I’ll do some tests with it.
    Still I don’t understand why you use the method(object) pattern. It’s the most unintuitive way to do it. There is no way to see what parameters it takes from outside the class. Without reading the documentation all the time :(

    Why not write the methods like addBox(x:int = 0, y:int = 0, width:Number = 1…
    With ‘fast to use’ -defaults. Or at least use the ValueObject class like this:
    addBox(new BoxVO(x:int = 0, y:int = 0, width:Number = 1…

    It might be that the method(object) is best for creating the api, but what comes to usability it’s no go. Anyway it’s your API and you do what you like and I should go bitching somewhere else or build my own :)

  2. Posted September 15, 2009 at 4:22 am | Permalink

    Hey Simppa - thanks for the feedback, I don’t think your bitching at all - I like to hear suggestions like the one you propose here. First off, you CAN do exactly
    what you describe. You would just need to create a little patch containing the BoxVO, PolyVO etc classes… I created BoxVO and tested it and it works fine…
    since addBox takes an Object if you pass a class to it that has all the correct properties it works just fine… Still, I think there are more usability issues with that
    then the way it currently works…

    The reason I chose this style of API was because I really liked the flow of working with engines like TweenLite (which work in a similar way) - I’ve found that I now have that same kind of flow when I work with QuickBox2D.

    With Box2D there are SO many different params you can set up just for one rigid body - so doing method/constructors calls with defaults would be cumbersome because you would potentially end up having to type a lot of default values in order to get to a specific argument that is 10 arguments in… that is near the end of the function call:

    addBox(x:Number = 0, y:Number = 0, width:Number = 0, height:Number = 0, angle:Number, draggable:Boolean, density:Number, friction:Number, restitution:Number, linearDamping:Number, angularDamping:Number etc… etc…

    It’s nice to be able to do:

    addBox({x:100, y:3, width:1, height:1, groupIndex:-1});

    and not have to worry about typing a bunch of defaults to get to the groupIndex argument…

    here are all params for box, just so you see how many there are:

    After playing with Box2D for a few hours it was easy for me to remember things like, angle, density, friction, restitution, linearDamping, angularDamping, isBullet, allowSleep, isSleeping, fixedRotation, mass, groupIndex. In the end I think it’s a matter of taste, I don’t find myself going back to the documentation very much unless I’m using something obscure like categoryBits or maskBits and in that case, I’m also going back to the Box2D manual…

    Thanks again for your comments…

  3. Posted September 15, 2009 at 4:25 am | Permalink

    Also, if you do decide to create that patch, save yourself time by finding all the common object params and putting them in a superclass- then BoxVO would just have width and height , CircleVO would just have radius etc… Also, if you decide to create that patch - let me know, I’d be interested to see how it works out…

  4. John
    Posted November 24, 2010 at 3:12 pm | Permalink

    i have a question.i would like to know how to remove( destroy) a rigid body via mouseclick.For example i have 6 circles on the stage and each one should disappear if the mouse button was clicked.

    Could you provide an example on how to do this?

    Kind regards,

  5. Posted November 24, 2010 at 3:25 pm | Permalink

    Hey John,
    You can use the QuickObject.destroy() method. So anything you want to get rid of, just call the destroy() method on it. Here is a slightly advanced example I created to test for memory leaks: http://actionsnippet.com/?p=2359

  6. John
    Posted November 28, 2010 at 8:23 am | Permalink

    Hi Zevan,
    using contact listeners for removing rigid bodies via mouseclick doesn’t make sense to me.I think the best way is to use an array full of rigid bodies and then check for clicks and remove each one apart.

    Not so easy for me at the moment.I am new in the actionscript world.

    Kind regards,

  7. John
    Posted November 28, 2010 at 9:17 am | Permalink

    var circles:Array = [];
    for (var i:int = 0; i<6; i++)
    circles[i] = sim.addCircle({x:6 + i,y:5,radius:0.5});
    circles.addEventListener(MouseEvent.CLICK, onClick);

    function onClick(evt:Event):void

    here is my attempt but it fails.i get an error message.A little help is much appreciated.

  8. Posted November 28, 2010 at 12:28 pm | Permalink

    I didn’t mean to imply you need contact listeners to destroy a QuickObject - you just need to use the destroy() method. There area few things wrong with your code. First,


    circles is an Array so you can’t use addEventListener with it.

    second… to addEvents to QuickObjects you need to use the userData property. I’ve uploaded an example to wonderfl.net … have a look:


  9. John
    Posted November 29, 2010 at 4:33 am | Permalink

    Thank you very much,Zevan.

One Trackback

  1. [...] QuickBox2Dによる衝突判定1 – QuickBox2D Contacts Part 1 [...]

Post a Comment

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