Box2D: tutorial for the absolute beginners – step 2

After understanding the meaning of the first lines in Box2D: tutorial for the absolute beginners, it’s time to explain the rest of the script.

Lines 19-27: These lines handle the debug draw. Remember Box2D does not draw anything, it just calculates position, rotation and motion of every object in the world. So it’s up to you to attach real graphic assets to worl objects. Anyway, with some lines you can enable a debug draw mode that will help you during the creation of your script. For more information about debug draw read Understanding Box2D debug draw.

Line 28 declaring final_body, b2Body type. b2Body is the object used to represent one rigid body. A rigid body is a chunk of matter that is so strong that the distance between any two bits of matter on the chunk is completely constant. They are hard like a diamond. Box2D only handles rigid bodies.

Line 29 and 31 declare and create the_body variable, b2BodyDef type. b2BodyDef handles the body definition. With the body definition we can specify the initial position of the body.

Line 30 and 33 declare and create the_box variable, b2PolygonDef type. b2PolygonDef is a polygon definition.

Line 32: setting the initial position of the body. Remember values are in meters and refer to the center of the body.

Line 34: SetAsBox function takes the_box and turns it into a box, passing the half-width and half-height as parameters. Read carefully… half-width and half-height. In meters.

Line 35: defining the friction of the box

Line 36: defining the density of the box. Setting the density of a body to zero, like in this case, will make the body static. A static body is not affected by gravity, collisions and so on. It’s just fixed in the stage. This will represent the ground.

Line 37: CreateBody creates the body previously defined

Line 38: I am creating the polygon shape previously defined with CreateShape on the body previously created with CreateBody

Line 39: Once the shape is attached, we instruct the body to compute its mass properties from the attached shapes using the method SetMassFromShapes. This is when the objects really exists.

Lines 40-42: Adding some listeners

Lines 45-58: This function, to be executed at every second, creates a box in the same way I created the ground, with the only exception at line 54 I am setting a density different than zero, meaning the body is not a static one. Every body with a density will react to collisions, gravity, forces and so on.

Lines 60: We must update the world at every frame. We can do it with Step function. Step has two parameters: the first is the time interval, in seconds. It means at every frame I am going to update the world as if 1/30s passed. The second is the constraint solver. The constraint solver solves all the constraints in the simulation, one at a time. A single constraint can be solved perfectly. However, when we solve one constraint, we slightly disrupt other constraints. To get a good solution, we need to iterate over all constraints a number of times. The suggested iteration count for Box2D is 10.

And now the entire script has been explained.

If you manage to turn this script into something interesting, send me at once and I’ll publish it on the blog.

  • Thanks. This tutorial is amazing. Can you continue doing B2 tutorials for beginners, please?

    @Off-Topic: You should add Gravatar to your blog comments. =D

  • Thanks man. Really nice of you. Can you redo the BitmapData tutorials in as3 time?
    I was trying to understand it through flashgameu but you know tutorials are better.

  • yacine

    but how to creat a 2d car game with suspensions.
    please send me the tutorial with source
    thanks you make great tutorials

  • Arxanas

    YES! I will soon enough figure out AS3!

    Just wondering, but is there any specific reason Box2D was ported into AS3?

    Does AS2 lack the functionality needed to make Box2D work, or is AS3 the choice because it is more powerful?

  • @Arxanas – it was ported to AS3 mainly because AS3 resembled C code in the way it works and occasionally in layout too – making it easier to work and definitely very fast.

    AS2 would probably be too time consuming and slow to use, though I tried porting it once. I should probably finish that…

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

  • Pingback: Box2D - Tutorial para novatos en el manejo de librerías de Física con AS3 « Shift F12()

  • Nils

    1. Can you describe how to set the gravity for the earth?
    2. What are the major diffences between version and

  • Hi. Does anyone know if I can set my Box2D world inside an existing movieclip, instead of in the stage?
    Thank you

  • Vinicius


    just so you know, I’ve been struggling all day with this (otherwise amazing) tutorial, and finally managed to make it work, but some important changes occured in the last version of box2d.

    A list of some of them is available here :

    for example, b2World creator takes now 2 arguments instead of 3, b2World.Steps takes 3 instead of 2, debugDraw properties cannot longer be set dynamicaly but by calling methods (debugDraw.SetFillAlpha(0.3) instead of debug_draw.m_fillAlpha=0.5) and so on.

    Thanks all the same and keep up the good work.


  • 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, Emanuele! You rock!


    PS: Sorry for the same comment post, Emanuele.. (Just in case someone saw this post first.. :P)

  • Pingback: Ninja Robot Dinosaur » Blog Archive » AS3 Boot Camp – Learn to program Flash games in 30 days()

  • Sharan

    Hey…. I found this tutorial really helpful but am facing a problem. When I try compiling any of the files given, I get an error saying:

    2.1a_2\Source\Box2D\Dynamics\, Line 871 1046: Type was not found or was not a compile-time constant: b2Fixture.

    Please do help. I need this for my project really quick.


  • Wooly

    Thank you so much. You explain very good and you give me a really proud motivation.