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.
They can be easily customized to meet the unique requirements of your project.















(22 votes, average: 4.09 out of 5)









This post has 27 comments
walrus92
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
walrus92
*wall*
New
Is that really box2d, it’s more like a buggy ASphysics engine… The collision angles are way off sometimes.
manoj sahu
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.
matakukos
+1
Monkios
More like always buggy …
Nice Ebonny ads btw.
Stelimar
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.
Astro75
I have made a similar library. Only mine is smaller and not full. http://www.box2d.org/forum/viewtopic.php?t=3101
Bindriy
????box2d????
abhilash
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?
abhilash
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
abhilash
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’ ;)
Wyatt
- 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. :)
Astro75
If this QuickBox2d doesn’t work for you then try this http://www.box2d.org/forum/viewtopic.php?t=3101
shabasky
Could someone show me how to setup QuickBox2d??? like what to download and which folders go where and what do i need?
Xodus
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!
Zevan
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
TimeFuzz
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
pretorian
how can i destroy QuickBox2D stageWalls?
jon
Thanks to zevan for this great enhancement.
Project: Volvo S60 Pinball game – Things I learnt « Tahir Ahmed's thoughts
[...] 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 [...]
robert
How can I add an event listener to an object?
robet
Can I get help adding a touch event to the ball so I can touch and drag on my android?
robet
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”);
}
}
}
Mandeep
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.
Tim
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!
» Blog Archive » Sofra’s FixFit 02 ~ Testing, Debugging and Polishing
[...] 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 [...]