QuickBox2D Contacts Part 1

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3.  
  4. [SWF(width = 800, height = 600, backgroundColor = 0x000000, frameRate=60)]
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this, {debug:false, frim:true});
  7.  
  8. sim.createStageWalls();
  9.  
  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});
  12.  
  13. sim.start();
  14. sim.mouseDrag();
  15.  
  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.

3 Comments

  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:
    http://actionsnippet.com/qb2d/docs/html/com_actionsnippet_qbox_QuickBox2D.html#addBox

    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…

One Trackback

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

Post a Comment

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

*
*