QuickBox2D 1.1 (2 major bug fixes)

Since the release of QuickBox2D 1.0 two bugs were discovered by numerous developers. The first bug was the inability to properly destroy group objects. The second bug was a small memory leak that caused most QuickObjects to remain in memory. Both of these bugs are now resolved.

Download QuickBox2D 1.1

Testing the memory leak. In QuickBox2D 1.0 if you created and destroyed 100s of rigid bodies, the ram would very slowly rise... Sometimes it would get garbage collected, but the memory would never fully be released. I created a simple test to make sure this is fixed in 1.1:

  1. import com.actionsnippet.qbox.*;
  3. var sim:QuickBox2D = new QuickBox2D(this, {debug:false});
  5. sim.createStageWalls();
  7. var levelParts:Array = [];
  8. const TWO_PI:Number = Math.PI * 2;
  10. // destroys and then creates a bunch of rigid bodies
  11. // called every second
  12. buildRandomLevel();
  13. setInterval(buildRandomLevel, 1000);
  15. var txt:TextField = TextField(addChild(new TextField()));
  16. txt.x = txt.y = 30;
  17. txt.backgroundColor = 0xFFFFFF;
  18. sim.start();
  20. // display amount of ram used, to make sure garbage collection is working
  21. sim.addEventListener(QuickBox2D.STEP, onStep);
  22. function onStep(evt:Event):void{
  23.     txt.text = (System.totalMemory / 100000).toFixed(2) + "mb";
  24. }
  26. function buildRandomLevel():void{
  27.     // destroy all rigid bodies
  28.     for (var i:int = 0; i<levelParts.length; i++){
  29.         levelParts[i].destroy();
  30.     }
  31.     levelParts = [];
  32.     // create a bunch of circles and boxes
  33.     for (i = 0; i<16; i++){
  34.         var rad:Number = 0.4 ;
  35.         levelParts.push(sim.addCircle({x:1 + i * rad * 4, y : 2, radius:rad - Math.random()*0.3}));
  37.         var rot:Number = Math.random() * TWO_PI;
  38.         levelParts.push(sim.addBox({x:4+Math.random() * i * 2, y:4+Math.random()*i,
  39.                     width:3, height:0.25, angle:rot, density:0}));
  40.     }
  41. }

This snippet creates and destroys a bunch of rigid bodies again and again.... On my macbook 2.6 Ghz intel core duo... the ram runs between 79mb and 112mb. I let it run for 40 minutes and this did not change - it always eventually returned down to 79mb.

Have a look at the swf...

Thanks to all the people who gave feedback that lead to the discover of these bugs.

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


  1. CD
    Posted November 7, 2009 at 2:29 pm | Permalink

    Thank you for this new release/ bugfixes! My program was one of those ones capable of creating 100’s of objects over time and now its great to see it stay pretty level over time. I took a look at your code fixes and it seemed like a combo of weak keys on the dictionary and nulling the object in it to get it collected right? The Flash garbage collector is one of the most confusing entities in existence!
    I appreciate your dedication to this project as it has been a godsend in speeding up Flash Box2D dev projects!

  2. Posted November 7, 2009 at 5:35 pm | Permalink

    thanks for the kind words… I had weak keys before , but that wasn’t enough… i needed to null them out as well. For whatever reason I neglected to do that when I modified the QuickObject.destroy() implementation a few versions back.

  3. JasonFuzz
    Posted November 9, 2009 at 3:15 am | Permalink

    Hi Zeven, this is perfect! just what I needed to get the project finished. Your example on destroying the level has really helped as well. I really appreciate all the work you have put into this project, quickbox will be one of my most used libraries in the future.


  4. Posted November 9, 2009 at 8:10 am | Permalink

    Thanks you for all your work !

    Still some minor bugs :

    line 445 QuickBox2D.as :
    //if userData == null > error
    if ( userData.skin != “none”){
    //replace to
    if (userData && userData.skin != “none”){

  5. Posted November 9, 2009 at 1:39 pm | Permalink

    I’ll look into that Fardeen… thanks :)

  6. Posted November 11, 2009 at 2:50 pm | Permalink

    Hi, Thank you for your great Lib and this new release!

    I noticed a possible little optimisation in the debug grid on QuickBox2D.as
    In a diagram editor I made, the performance decrease dramatically when I drew a grid with lots of cells. The problem had been solved with a cacheAsBitmap on the grid’s Sprite.

    You could simply try this.

    public function grid(size:int = 30, lineColor:uint = 0xFFFF00, lineAlpha:Number = 1):void {
    var g:Sprite = new Sprite(); [...] g.cacheAsBitmap = true; }

    Have a nice evening !

  7. Posted November 11, 2009 at 3:25 pm | Permalink

    Hi Zevan. I’m working on a very simple project that I thought might require a physics library. This is my first time working with a physics library, so I’m afraid I’m not familiar with all of the terminology. I’ve gone through APE, Fisix, Glaze and now Box2D. First, thank you for this class, it does exactly what the title suggests. I’ll be honest, looking at the examples for Box2D are somewhat intimidating, but your system drastically reduces the amount of code required to create a physics app.

    However, I’m having trouble, and I’m hoping someone can help. All I want to do is create a rope. I don’t want it to stretch. I need the first point fixed, and then an ornament is attached to the other end. The ornament needs to drop from above the stage to the middle of the screen. Naturally, using a physics library, the ornament should do a little bounce and some swaying, however I want to limit the swaying as much as possible, making the rope somewhat stiff.

    I have it sort of working in APE, but it swings too much, and it stretches.

    In Fisix, I was able to get rid of the stretching by increasing the iterations, but then I was unable to attach my movieclip to the end of the rope and I have no idea why. It’s the only one I’ve had to use Flex for, and then I got some weird error “internal build error” and I can’t even test it anymore.

    In Glaze, they have a rope example, but a) I don’t like the programming, and b) I can’t find any documentation, so I gave up on it.

    That brings me to Box2D and QuickBox2D. I can make a rope, and using the distance joints, it doesn’t stretch, but I don’t know how to change the stiffness and get it so it doesn’t swing so much. Also, the math seems weird to me, I don’t understand what the numbers represent, where you set x: 3, y: 3 on a circle particle, and then x: 6, y: 6 on another - what are those numbers?

    I’m grasping at straws now, project is due very soon, and I don’t have much to show for it, except a lot of experiment files. Really hoping someone can point me in the right direction, or give me some sort of crash course into your library.

    The other thing I’m looking at doing is just trying to animate the ornament by itself, and then just draw a line from it to the fixed position above the stage. But now I’m getting into simulating what a bounce might look like, and then some swaying… and I just keep coming back to the fact that using a physics library should take care of all this for me.

    Sorry for the long comment. I appreciate any help anyone can give me:. Thanks.

  8. Posted November 12, 2009 at 9:18 am | Permalink

    Alright, you can ignore my previous post. I’ve decided to just give up and just animate a symbol with a straight line for a rope. It’s not going to look as good, but it will at least be finished quickly. I really like the idea of physics libraries, and you’ve really simplified things, but there are issues. I did get a rope working… but when I tried changing the length of the rope segments from 1 (30) to 0.333 (10), it fails… the rope ends up looking like lightning and freaks out and dies. That was the point I decided I need to stop experimenting.

    So thanks anyways, I will definitely be looking into this for future use, but for now I’ve moved on.

  9. Posted November 12, 2009 at 9:42 am | Permalink

    Hey Adam,
    Glad that you found a solution in the end.

    I could see how what your trying to achieve could be a bit tricky…
    I’m pretty sure setting the mass of the ornament to something higher is going to cause the string to sway less…you could also cheat a bit by giving the ornament a little push with b2Body.SetLinearVelocity … I saw something done with quickBox2D a little while ago that had a nice string, but they are still pretty bouncy…
    (have a look)

  10. Posted November 12, 2009 at 12:12 pm | Permalink

    Hmm, that’s actually similar to what I’m going for. I was able to lessen the bounce and swing by setting the linearDamping property to 1. Made a huge difference. Nevertheless, I still couldn’t get the string/rope to act properly. It’s ok, I’m almost done anyways, just using a few symbols and TweenMax.

  11. Posted November 12, 2009 at 5:29 pm | Permalink

    Thanks again for your help. I’m still quite interested in the wonderful world of physics in Flash, but I know now that I need to invest more time in it, learn a bunch of basic concepts, etc. and get a solid grasp of how it all works before I can start using it in real world applications.

    I just about finished the piece I was working on, you can see it here. http://citrusmedia.com/projects/nyf/. It’s all random, so you can refresh to see different formations. As you can see, it would’ve looked a lot nicer if I could use some physics to make the ornaments bounce and the string curve, etc. but I think it turned out pretty good regardless.

  12. Posted November 13, 2009 at 8:00 am | Permalink

    very nice Adam. I’m glad you were able to get to work.

    You actually reminded me of a snippet I wrote awhile back for pendulums… that might be of interest… I don’t think you should you it on your project (because it already look very nice). But if you interested you can check it out…

    (you’ll need to copy and paste the code in your timeline)

  13. AntScript
    Posted November 29, 2009 at 8:42 pm | Permalink

    Hi Zevan,can I change single object’s scale with quickBox2d ? I can’t find it in documentation.thanks.

  14. Posted November 29, 2009 at 9:45 pm | Permalink

    unfortunately there isn’t any way to change the scale of an object in Box2D… you can sort of fake it by destroying and recreating rigid bodies… but that probably won’t work all that well…

  15. AntScript
    Posted November 29, 2009 at 10:08 pm | Permalink

    thanks Zevan.I will try it.

  16. Mauricio
    Posted March 30, 2010 at 10:18 pm | Permalink

    Hi, excuse my english, i have a question…
    How can i know (thru AS3) which quickObject am i clicking?
    If i have this:

    function on_mouse_down(e:MouseEvent) {
    //Detect which QuickObject was clicked
    //Then do something to this object…

  17. Chris
    Posted May 5, 2010 at 1:54 pm | Permalink

    Hi, great library - really.
    But now I need some help:

    I want to use QuickBox2D for a game we create in our university.
    In this game the player handles a kind of a bucket with its mouse. The movement on the x-axis is done onMouseMove.
    From the top left of the screen multiple objects fall down. This objects have a velocity to move to the right also.

    The target is to catch the objects with the bucket .
    Heres the problem:

    If I move the mouse to fast, the objects “walk” through the walls (left & right) of the bucket without being influenced by the bucket.

    Is there a possibility to solve this or is the mousemove event to fast?
    Thank you so much.

  18. Posted May 6, 2010 at 4:48 am | Permalink

    try setting isBullet = true on the objects that you want to catch in the bucket. That way they won’t fall through the bucket:

    ball = sim.addCircle({x:3, y:3, radius:1, isBullet:true});

  19. Chris
    Posted June 1, 2010 at 3:33 am | Permalink

    Hey thanks for the tipp,

    at first i updated the position of my body directly with the new values from the mouse position. then the isBullet property didn’t do the trick.
    But when i update the body’s position with an mouse joint it works now.

  20. Lorenzo
    Posted November 11, 2010 at 2:15 pm | Permalink

    you made a very great work with QuickBox2D !
    thank’s a lot !
    i’m sad that you don’t support the new Box2DFlashAS3 2.1.

    the only change i have made to QuickBox2D is add a public static var “SCALE” for setting the world scale.
    in my opinion, 30 for the world scale wasn’t a good idea, it’s harder for calculate, with 10 or 100 it’s much easier.
    have you a particular reason for using 30 ?

  21. Posted November 22, 2010 at 3:57 pm | Permalink

    I just used 30 because that is what Box2D uses by default. I have read that setting it to match pixels can cause errors. I also didn’t mind doing the simple conversions involved.

    I may support 2.1 at some point in the next few months.

  22. Mike
    Posted February 17, 2011 at 12:29 pm | Permalink

    Great lib Zevan thnx a lot!
    I’m using qb2d for a game, i need a way to see if a QuickObject has been deleted or not.

  23. Posted February 17, 2011 at 1:28 pm | Permalink

    hey mike… as long as your using DisplayObjects and not debug draw mode you can do this:

    if (myObject.userData.stage == null){
    // i have been destroyed

    Note that objects are not destroyed immediately after you call destroy()… calling destroy places objects into an array that is purged at the end of every time step.

  24. Mike
    Posted February 17, 2011 at 4:01 pm | Permalink

    Cool i never thought of checking the stage :)
    kept checking myObject…

    thanx a billion!

  25. ifree
    Posted March 31, 2012 at 2:45 am | Permalink

    hi Zevan
    i found a bug about set simpleRender=false.
    when i set simpleRender=false, any joint take no effect and it also draw the objects out, can you help me?

Post a Comment

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