Simplify your Box2D projects with QuickBox2D

You know with Box2D you can make interesting games like Mazeroll, SamePhysics or Filler, but even if I published a Box2D tutorial for absolute beginners, everyday I am receiving emails complaining about the excessive complexity of this library.

If you are looking for a library able to simplify your Box2D projects, Zevan Rosser from ActionSnippet developed a library called QuickBox2D that promises to significantly simplify instantiation of rigid bodies and provide a simple way to skin rigid bodies with custom graphics.

In this quick example I am going to add a sphere to the stage and make it draggable.

Before looking at this script, take a look at Dragging objects with Box2D Flash to see how many lines of code I needed to make those crates draggable.

This is what you need with QuickBox2D:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package {
	import flash.display.MovieClip;
	// importing required library
	import com.actionsnippet.qbox.*;
	// it must be a MovieClip!! With a Sprite it won't work!
	public class quickbox extends MovieClip {
		public function quickbox() {
			// setting up the QuickBox2D world
			var qb:QuickBox2D=new QuickBox2D(this);
			// default fill color
			qb.setDefault({lineAlpha:0, fillColor:0xff0000});
			// automatic stage walls creation!!!!!!!
			qb.createStageWalls();
			// adding a ball... that easy...
			var ball:QuickObject=qb.addCircle({x:3,y:3,restitution:0.9,lineAlpha:1,fillColor:0x00ff00,allowSleep:false,fixedRotation:true});
			// automatic mouse dragging!!!
			qb.mouseDrag();
			// starting the simulation
			qb.start();
		}
	}
}

and this is what you get…

And this is the source code with all libraries in the correct path.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (22 votes, average: 4.09 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 27 comments

  1. walrus92

    on August 25, 2009 at 6:29 pm

    Very nice, but one thing I noticed about this was that when you throw the ball at an angle towards the floor, ceiling, or floor, that it stops following that angle and starts to bounce perpendicularly

  2. walrus92

    on August 25, 2009 at 6:29 pm

    *wall*

  3. New

    on August 25, 2009 at 7:20 pm

    Is that really box2d, it’s more like a buggy ASphysics engine… The collision angles are way off sometimes.

  4. manoj sahu

    on August 25, 2009 at 7:45 pm

    line #16: var ball:QuickObject=qb.addCircle({x:3,y:3,restitution:0.9,lineAlpha:1,fillColor:0x00ff00,allowSleep:false,fixedRotation:true});

    what is restitution:0.9? never found such property in any kind of PL,i wonder how guys are going against simplicity?.PL and library are about to make make programmers life easier rather then dumb.

  5. matakukos

    on August 25, 2009 at 8:38 pm

    +1

  6. Monkios

    on August 25, 2009 at 8:59 pm

    More like always buggy …

    Nice Ebonny ads btw.

  7. Stelimar

    on August 25, 2009 at 9:42 pm

    Looking at the code, objects automatically have a friction of 0.5, which might be why the ball seems to bounce weird. I’d try setting it lower, to maybe 0.2 or 0.3.

    Anyways, nice post. I’ve heard of this library before, but never taken the time to try to figure it out.

  8. Astro75

    on August 25, 2009 at 10:36 pm

    I have made a similar library. Only mine is smaller and not full. http://www.box2d.org/forum/viewtopic.php?t=3101

  9. Bindriy

    on August 26, 2009 at 2:16 am

    ????box2d????

  10. abhilash

    on August 26, 2009 at 6:48 pm

    gr8 it is the first physics engine of which each and every line i can understand looks really simple btw when are u writing next part of BOX2d for beginners?

  11. abhilash

    on August 26, 2009 at 6:55 pm

    dunno why but after following your Box2d for beginners steps for this engine i can’t seem to get it work when i compile the movie it says

    5008: The name of definition ‘quickbox’ does not reflect the location of this file. Please change the definition’s name inside this file, or rename the file. D:\Abhilash files(july)\Quick Box\demo.as

  12. abhilash

    on August 27, 2009 at 1:49 pm

    aaah!!tried each every trick to make it work but still it’s giving errors, i think it’s time for you to write ‘QuickBox2d for absolute
    beginners’ ;)

  13. Wyatt

    on August 27, 2009 at 5:19 pm

    - abhilash:
    Perhaps you should rename the “demo.as” file as “quickbox.as” or change “public class quickbox” to “public class demo” and “public function quickbox()” to “public function demo()”. I suggest the first one though. :)

  14. Astro75

    on August 28, 2009 at 4:19 pm

    If this QuickBox2d doesn’t work for you then try this http://www.box2d.org/forum/viewtopic.php?t=3101

  15. shabasky

    on August 31, 2009 at 12:22 am

    Could someone show me how to setup QuickBox2d??? like what to download and which folders go where and what do i need?

  16. Xodus

    on August 31, 2009 at 1:56 pm

    It seems like there isnt any spin, when you hold up the ball, it doesnt spin around. Also, the ball doesnt roll on the ground…

    Anyway, very good idea, box2d seems so much simpler!

  17. Zevan

    on September 11, 2009 at 12:24 am

    Thanks for mentioning QuickBox2D Emanuele.

    Hey Xodus… thats because Emanuele has set fixedRotation = false on the circle… I assume he has done this to show the flexibility of QuickBox2D

  18. TimeFuzz

    on November 4, 2009 at 2:43 pm

    This library is fantastic for people without much as3 experience. However, I do have a problem/question:

    Is there a way of easily resetting the simulation using quickbox2D? I have a game almost complete using the quickbox library, but now need to basically clear the screen in between the levels, but this is where I am having troubles.

    I have tried qb.destroy() which should work but this only stops the simulation and all the boxes and circles are still on screen.

    This is probably a really dumb and easy thing to do but I’m not the greatest at as3, so any help would really be appreciated!

    Thanks

    TimeFuzz

  19. pretorian

    on January 11, 2010 at 11:43 am

    how can i destroy QuickBox2D stageWalls?

  20. jon

    on March 26, 2010 at 6:56 pm

    Thanks to zevan for this great enhancement.

  21. Project: Volvo S60 Pinball game – Things I learnt « Tahir Ahmed's thoughts

    on October 12, 2010 at 7:47 pm

    [...] task with Box2DFlash would look like with that of QuickBox2D, Emanuele Feronato has written a blog tutorial using QuickBox2D and a tutorial done earlier using Box2DFlash alone. It clearly shows you how easy it is to setup [...]

  22. robert

    on December 11, 2010 at 1:26 am

    How can I add an event listener to an object?

  23. robet

    on December 11, 2010 at 9:33 am

    Can I get help adding a touch event to the ball so I can touch and drag on my android?

  24. robet

    on December 12, 2010 at 4:17 am

    I got touch functionality listener added however it doesnt respond and move. Any one can help me with this..

    code below..
    package {
    import flash.display.MovieClip;
    // importing required library
    import com.actionsnippet.qbox.*;
    // it must be a MovieClip!! With a Sprite it won’t work!

    //touch import
    import flash.events.TouchEvent;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;

    public class Box2d extends MovieClip
    {
    public var ball:QuickObject
    // setting up the QuickBox2D world
    public var qb:QuickBox2D;

    public function Box2d()
    {
    Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;

    addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);

    qb=new QuickBox2D(this);

    // default fill color
    qb.setDefault({lineAlpha:0, fillColor:0xff0000});
    // automatic stage walls creation!!!!!!!
    qb.createStageWalls();
    // adding a ball… that easy…
    ball = qb.addCircle({x:3,y:3,/*restitution:0.9,*/lineAlpha:1,fillColor:0x00ff00,allowSleep:false,fixedRotation:true});
    // automatic mouse dragging!!!
    qb.mouseDrag();
    // starting the simulation
    qb.start();
    }

    public function onTouchBegin(e:TouchEvent):void
    {
    e.target.startTouchDrag(e.touchPointID);
    e.target.alpha = 0.2;
    trace(“touch begin”);

    }
    }
    }

  25. Mandeep

    on March 8, 2011 at 3:10 am

    Since the author of QuickBox2D has moved on to other projects I’m wondering if it might be better to take the time to learn Box2DFlash directly? I’m trying to make a small game with QuickBox2D now and it’s hard to find any better documentation than reading the source code.

  26. Tim

    on November 23, 2011 at 4:32 pm

    Has anyone seen a version of QuickBox2D using the alchemy port from the World Construction Kit? I’ve starting using Starling and QuickBox2D is about a thousand times easier to integrate than WCK but I would like to make use of the Alchemy port. I’ve tried integrating QuickBox2D and Box2D Alchemy myself but it’s not as simple as it might be and I was hoping someone else might have done it!

  27. » Blog Archive » Sofra’s FixFit 02 ~ Testing, Debugging and Polishing

    on January 20, 2012 at 1:50 pm

    [...]  Indrayasa as the programmer has done a very good job to finish all 50 levels codes. He use quickbox2D to help him simulate in game physics. Now, we are in the stage of testing, debugging and polishing [...]