Box2D: tutorial for the absolute beginners

This is a tutorial about Box2D dedicated to the absolute beginners.

I received a lot of request asking for this, so here we go.

What is Box2D

Box2D is a feature rich 2d rigid body physics engine, written in C++ by Erin Catto.

There is a Flash version called Box2DFlashAS3, but among Flash developers it’s called Box2D as well, that you can download from this link.

Being a Flash porting, we will use AS3 to make our project, instead of C++ as required by the original library.

Once you downloaded and unpacked the zip file in a folder, this is what you will get:

You are now ready to begin

Your first Box2D experiment

Start Flash and create a new AS3 Flash file and call it (example) demo.fla. Save it in the same folder you used to unzip Box2D package. Also in your properties panel assign the Class name to (example) demo, this way:

Now create a new actionscript file, call id and save it in the same folder.

Your folder now should look this way:

it’s time to edit

Lines 2-5: some commons Flash libraries used to make the demo

Lines 6-9: Box2D libraries… it’s not that important at the moment to know everything about them… they’re just some libraries

Line 11: Declaring the_world variable, b2World type. b2World is the the main object to deal with the Box2d engine. It stores all the joints and bodies, handles listeners and is responsible for stepping through the simulation.

Line 14: Declaring the_environment, b2AABB type. The physics environment generated by Box2D is not infinite, and b2AABB is the container of such environment. Think about it as a bounding box. Inside this bounding box, the world is ruled by Box2D physics.

Lines 15-16 : Defining the upper and lower corners of the environment bounding box, in meters. 1 meter = 30 pixels. So our box has sides made by 6000 pixels. They’re pretty too much for a single-screen project, but if you are using scrolling, it could be useful to set up a big environment. For more information about pixels and meters refer to Understanding pixels and meters with Box2D and how to select an object with mouse – part 2.

Line 17: Declaring gravity variable, b2Vec2 type. b2Vec2 is a vector with x and y components.

Line 18: Starting up the world: the constructor has three parameters: environment, the worldAABB bounding box, gravity the world gravity vector, and a boolean set to true to improve performance by not simulating inactive bodies.

And now you created the world. Next time I’ll explain the rest of the script

Anyway, this is the result

And this is the source code to download.

I hope this will help you start playing with Box2D.

  • Superdean

    finally a box 2d tutorial i can understand!
    this actually might make me migrate to AS3…

    • shaharia

      how to run the demo ????

  • Awesome!!
    Thanks a lot!

  • You are really the rockstar of programming

  • make it PROgramming

  • thanx emanuele for making a tut for Box 2D that is for begginers
    i haven’t started AS3 yet, but i think this will help…
    do u have a beggining AS3 tutorial

  • Very basic for my taste. But you can’t satisfy everyone :)
    Glad you got a good response from your other users.
    Keep up the more advanced tutorials too.

  • Pingback: Emanuele Feronato()

  • Xodus


  • Excellent!

    I shall be closely following both the basic Box2D tutorials tonight and start playing around with them!

  • Pingback: Weekly Shared Items - 30. January, 2009 « toxin 2.0()

  • Koczo

    It looks great, but when i’m trying to open “demo.fla” file in Adobe Flash CS3, I got the message “Unexpected file format”. Do you know why?

    • Draco18s

      Because it’s a CS4 file.

  • Pingback: A smart way to manage sleeping objects with Box2D : Emanuele Feronato()

  • Pingback: Erase Box, a Box2D game made with my tutorials : Emanuele Feronato()

  • Hi,

    I wanted to know if there was an easy way to add a texture to those ‘boxes’..

  • lol

    uuuh, yeeeah, that’s so PROOO Mr. PROgrammer

  • Tobias C.

    Thanks a lot!!!!

  • Fighterlegend

    Can you put it in a Flash CS3 format?

    Or do I NEED Flash CS4?

    If I do, that would suck! :(

  • Pingback: AS3 Flash Physics Engine Box2D | [mck]()

  • Destruction

    Nice guide, might play around with it sometimes.
    Also, hi Fighterlegend!

  • Thempc

    to Fighterlegend:
    no you do not NEED CS4 for this,
    this can also be done in CS3 with ease.

    PS: awesome tutoial 5/5

  • Androoo

    Why don’t you reply to peoples questions?

  • Nice tutorial. I’m still learning as3 and this will be very useful. Thanks!

  • Pingback: Box2DFlash - Falling Text | Cyan[c] Design()

  • This one help me start off on the right foot. And all your other tutorials are awesome! I was able to expand on this to turn the boxes into dynamic falling text.
    (^_^)// Cheers.

  • shuping chen

    Thank you very much.^_^

  • Thanks for these, really finding them useful.

    One thing though, you mentioned that 1 meter = 30 pixels… is that a default or do you have to set that ratio somewhere in the code?

    Thanks again.

  • Efe

    Thank you for the lovely tutorial, its beyond amazingly useful. As a low to mid-level Flash progrommer / animator, getting a handle on the Box2D stuff through just the examples is actually quite difficult (and not helped by the slightly more ambiguous naming conventions it uses)

    But seriously, amazing stuff. Thanks :)

  • Sam

    I don’t seem to have flash, to create a new AS3, I need help. Can you give me a link?

  • bubloobasu

    Can u pls explain the rest of the script

  • Errm.PART 2!!!!!!!!!!!!!!!!!!!!!?????????????????

  • clayton

    i followed the directions exactly as listed, however when i try to run the program, it gives me a bunch of errors starting with:

    1046: Type was not found or was not a compile-time constant: b2World.
    1046: Type was not found or was not a compile-time constant: b2AABB.
    1046: Type was not found or was not a compile-time constant: b2Vec2.

    anyone have any ideas?

  • @clayton

    your classpaths arent correct configured..
    these errors happen when flash or your code editor cannot find the class you are trying to use..
    So check your linkage to the classes and try again :)

    it also helps to get a basic understanding of AS3.0 and OO before you start a tutorial like this one.


  • ok. I tried this and it was an epic fail.

    The idiots who made Box2d decided to randomly change the whole thing! The box2d looks nothing like the screenshots you had. Could you plz make a new tutorial?! thanks.

  • Xalvez

    I got the same error with #26.
    Help pls ><

  • Pingback: Box2D tutorial for the absolute beginners – revamped : Emanuele Feronato - italian geek and PROgrammer()

  • Sam

    Hi, guys.

    You need to have the following in order to successfully run Demo.fla.

    1. Adobe Flash CS4
    2. Box2DFlashAS3_2.0.2

    Download Box2DFlashAS3_2.0.2 in here.

    If you have any questions, ask. :-)

    Thank you for the great tutorial, Emanuel! You rock!


  • merve

    i liked very much the scene i see in the last animation screen.

    now i want to ask whether it is possible to write an application without using flash development environment? i am a newbee in software developing but i like flash applications too. but i am a student and no have money for flash development environment.
    is there a compiler for action script desperate from flash development environment?
    i googled a bit but find nothing.

    thanks for tutorial and any possible advises.

  • Gab

    I tryed this code with Adobe Flash IDE CS4 : It’s okay.

    When I try with an AS3 project with Flash Develop, debugDraw doesn’t display any objects.

    I use : FlashDevelop 3.0.6/Box2D 2.0.2/Flex 3.4

  • Tim

    Learning AS3 and your tutorial is an awesome way to experiment. Really one of the best sites I’ve found about Flash, Actionscript, and games. Thanks for sharing your knowledge.

  • reb1rth

    hii those whor getting error just like #26 and #29 check out this link
    i had the same error i got it sorted nw.

  • one1989

    Great tutorial. I’m very excited in box2d. Hope you can give more..

  • Pingback: Physics Engine für Flash: Box2D | Multitouch and Beyond()

  • Hey Emanuele, Nice tutorial though the box 2d files i downloaded had nearly 60 to 70 syntax errors like the one below all was same error

    1084: Syntax error: expecting identifier before lessthan.
    public var m_vertices:Vector.;
    the use of this bracket caused a lot of problems

  • claudio

    Doesnt work i dont know why
    do i have to put it in a special rute in flash installation directory?

  • Roshan C

    A very simple and nice tutorial and followed all the instruction as said in tutorial but i got 10 compile time errors :- 1084: Syntax error: expecting identifier before lessthan. all the errors were same. I am using FLASH CS3. I created demo myself since i am not having CS4. Thanks for such a b’ful tutorial.

  • Great tutorial, with the latest version there were a lot of errors, but after downloading the previous version Box2DFlashAS3_2.0.2 all worked great without any errors.
    Thanks for this tutorial!

  • Alexandre Colella

    Thanks! Great easy-to-learn tutorial!

  • shefyg

    hi Emanuele,

    10x for this tutorial

    I think you should consider update it. I saw that in the latest B2Box – there is now more public b2DebugDraw.m_sprit instead we have b2DebugDraw.SetSprite()

    If either of you (readers of this tutorial) encountered this problem – I hope I helped.

    The way I solved this, is by going to decleration of class b2DebugDraw, and looking for m_sprite.

    Best Regards, Shefy

  • Pingback: Useful Links « Mobile training support()

  • Neko

    Warning, don’t waste your time with this tutorial – Box2D has been updated in the 2 years since this was written and this tutorial is currently broken. :(

  • great tutorial!

  • stan

    You’re just taking tutorials from here and there and adding them to your site as your own tutorial.

    You are not even explaining the code( which Im sure you don’t know yourself seeing as they are copied from other tutorials).

    Why not make a simple original tutorial?

  • Draco18s

    Finally, something that might actually help me figure out how to START a project with Box2D. Unfortunately, they updated the engine to a new version, and this demo can’t be converted:

    b2Body no longer has a CreateShape function. Or anything that appears to do even remotely the same thing.

    At least the old package is still available.

  • satya pandey

    hi sir , Good morning
    While working with above tutorial, i am facing problem with following
    1. debug_draw.m_sprite=debug_sprite;
    2. var the_box:b2PolygonDef;

    it’s showing compile time constant, definition not found & Attempted access of inaccessible property m_sprite through a reference with static type Box2D.Dynamics:b2DebugDraw.

    please sir help to resolve these problem.

    Thanking you……….

    • Bryce Neal


      This tutorial is out of date. Somewhere along the line defs were renamed to shapes. Try b2PolygonShape.

  • Hey Awesome tut!

    I have a problem though. I am getting a “Scene 1, Layer ‘Layer 1’, Frame 1, Line 1 1037: Packages cannot be nested.” Compiler Error.

    Any ideas why or how to fix it?

  • Rafael

    To make this tutorial Download this file:

    not the latest.

    Ty Feronato

  • George

    I’m new to all this. I’ve been trying to follow your tutorials but they don’t seem to be in order? I found the first one thru the web and this one by chance. I would really like to learn how to use this powerful tool and see what I can come up with. Could you please email me the link or links to all of these tutorials you are offering?
    Also, since I have not found all the tutorials are you explaining how to create a game from start to finish using this box2d method?
    George =)

  • Evgeney

    Thanks a lot.

  • Kin Jung Il

    I can’t do it D:

  • Pingback: Portfolio Mike Dammers » Blog Archive » Spelen met physics()

  • Pingback: Farseer tutorial for the absolute beginners | Code to Preload()

  • You’re a fucking idiot

    Your tutorials are so unintuitive and badly explained, I’m surprised any beginner would be able to understand them. Here’s a suggestion: When writing a tutorial, actually EXPLAIN the process as you go along rather than getting people to write out masses of code verbatim which doesn’t work after the masses of time it takes to write.

    Also if this code doesn’t work with the latest version of Box2D, then add an update or even better, update your defunct tutorial so I don’t have to scratch my head for ages trying to work out what I’ve done wrong WHEN I HAVEN’T DONE ANYTHING WRONG. I think you should stop writing tutorials because you are absolutely shit at it. :(

  • thanx a lot, it really helped me a lot.

    regards maria

  • Sachin

    I have downloaded the version from your site for flash. but I am not able to run this .fla file in flash cs5. It’s giving me lots of error. Can u please give me provide me working version? thnx

  • JohnWise

    Hi Friends ..
    What should I do in Adobe Flash CS5 ?
    I faced with so many errors after doing that !
    Thanx ..

  • Pingback: Box2D C++?? ?????? | Software MyZone()

  • Pingback: Box2D C++ ??-?? | Oh!Coder()

  • Joe the plumber

    Please note that this tutorial only works with the older version of box2d version 2.02

    I see alot of angry comments here and I have to say if you’re not prepared to put some time into doing some research into why things don’t work then Programming isn’t for you. Game development has a steep learning curve that takes years of practice and experience it doesn’t come by just looking at a few web tutorials.

  • Noobman

    Thank you in advance :)

  • Tony

    It gives me a lot of errors such as “Couldn’t found definition of Box2D.Dynamics” when I try to debug it and didn’t work, but when I save it, swf file is works. What should I do?

  • Tony

    error 1046 on line “public function on_time(e:Event) {“

  • Tony

    even source code gives me error 1046!

  • Pingback: Box2D C++??-?? | ?????()

  • Arch

    hello, i am a truely beginner to box2D and need some major help with this box2D feature. I am actually using the code for drawing line,forming a wall for the ball to bounce and roll on which you have provided in another tutorial. However, I am quite confused on how to set a angled(SLANTED) BOUNDARIES/WALLs so the ball will not roll out of the stage.

    Is there any way for me to use my own graphics (Movieclip etc) as the WALL?
    Is it possible for the BOUNDARIES/WALL to have a function that allows me to create effect like sound or something when the ball touches it?

    Hope you can help me understand this complicated plugin better~ THANKS!

  • Pingback: Flash Game Development Inter-web mash up: Jan 31, 2009 «

  • Jack

    Yeah. Thanks. I get nothing.

    Just a white screen. Or a slew of errors.
    Might want to explain how to link the library so flash CS6 can recognize it.

    • Jack

      Sorry for that ignorant comment. Flash is incredibly frustrating.

  • missoum

    Your program don’t work with Box2D_v2.1