I was going through a pretty good tutorial on Box2D here, but I ran into a problem when he made a b2PolygonDef, because the tutorials were made in 2.0, and I'm using 2.1. I could download 2.0 I guess, but I'd really like to know how to update how to do this to the newest version.

If you guys don't want to watch the whole video, basically, he does this in this one:

        private function createWallsAndFloor():void
        // Create the shape (polygon) definition
        var bigLongShapeDef:b2PolygonDef = new b2Polygondef();
        bigLongShapeDef.vertexCount = 4;
        b2Vec2(bigLongShapeDef.vertices[0]).Set(0 / RATIO, 0 / RATIO);
        b2Vec2(bigLongShapeDef.vertices[1]).Set(550 / RATIO, 0 / RATIO);
        b2Vec2(bigLongShapeDef.vertices[2]).Set(550 / RATIO, 10 / RATIO);
        b2Vec2(bigLongShapeDef.vertices[3]).Set(0 / RATIO, 10 / RATIO);
        bigLongShapeDef.friction = 0.5;
        bigLongShapeDef.restitution = 0.3;
        bigLongShapeDef.density = 0.0;

        // Create the body definition
        // Create the body
        // Create the shape

I can see that they removed b2PolygonDef and b2ShapeDef because I'm using Flash Develop, so what basically do I do?

(If you don't mind watching videos, he explains what he's doing in the one before this one.)

  • 1
    \$\begingroup\$ I have written a few Box2D tutorials based on 2.1 for Flash. Could be helpful if you get stuck with any other of the changes between the versions in future: blog.allanbishop.com/category/physics/box2d \$\endgroup\$
    – Allan
    Commented Oct 25, 2010 at 2:07

3 Answers 3


The errors are to do with the Box2D engine being updated. The excellent tutorials by FlashyTodd are unfortunately out of date now (22/10/10). Although he does mention this at the top of the videos the link to the things that have changed isn't much use for the novice (myself included).

Also the link above to the Box2D manual from DrDeth is the C++ manual, not AS3 which is what FlashyTodd is working through.

I also found this useful example which notes the changes made from 2.0 to 2.1 code.

For anyone else that comes across this, here is the FlashyTodd example HelloBoxWorld updated to be Box2D 2.1 compliant. (just a floor and dynamic box)


import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;

import Box2D.Dynamics.b2World;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2Fixture;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Collision.b2AABB;
import Box2D.Dynamics.b2DebugDraw;

    public class HelloBoxWorld extends MovieClip{

        protected var _world:b2World;

        //ratio of pixels to meters.
        public static const RATIO:Number = 30;
        private var _fallingCrate:b2Body;

        private var timeStep:Number = 1.0 / 30.0;
        private var velocityIterations:int = 6;
        private var positionIterations:int = 2;

        public function HelloBoxWorld() {

            //1. set up the world

            //2. create walls and floor         

            //3. make create            


            addEventListener(Event.ENTER_FRAME, newFrameEvent);

private function setupDebugDraw():void {

        var spriteToDrawOn:Sprite = new Sprite();

        var artistForHire:b2DebugDraw = new b2DebugDraw();



private function newFrameEvent(e:Event):void {

    //variables assigned at the top
    _world.Step(timeStep, velocityIterations, positionIterations);


    trace("Falling Craete is at x:"+_fallingCrate.GetPosition().x +",y:"+_fallingCrate.GetPosition().y);

        private function setupWorld():void {

            //1. set up the size of the universe
            //var universeSize:b2AABB = new b2AABB();
            //universeSize.lowerBound.Set (-3000/RATIO,-3000/RATIO);
            //universeSize.upperBound.Set (3000/RATIO,3000/RATIO);

            //2. Define gravity

            var gravity:b2Vec2 = new b2Vec2(0, 9.8);

            //3. Ignore sleeping objects.
            var ignoreSleeping:Boolean = true;

            _world = new b2World(gravity, ignoreSleeping);

            trace ("My world has "+_world.GetBodyCount()+" bodies");

        private function createWallsAndFloor():void {

            //1. Define a body with a position      
            var groundBodyDef:b2BodyDef = new b2BodyDef();
            //x,y of centre. set as integers, not floats
            groundBodyDef.position.Set(250 / RATIO, 400 / RATIO);

            //2. Use the world object to create the body using the body definition above
            var groundBody = _world.CreateBody(groundBodyDef);

            //3. Define fixtures with a shape, friction, density, etc.
            var groundBox:b2PolygonShape =  new b2PolygonShape();
            //width ,height. set as integers, not floats
            groundBox.SetAsBox(200 / RATIO, 20 / RATIO);

            var fixtureDef:b2FixtureDef = new b2FixtureDef();
            fixtureDef.shape = groundBox;
            fixtureDef.density = 0.0;//density is set to 0.0 by default so this isnt really needed here
            fixtureDef.friction = 0.5;


            trace ("My world has "+_world.GetBodyCount()+" bodies");


        private function addACrate():void {

            //moving object
            var movingBodyDef:b2BodyDef = new b2BodyDef();

            //set the type of body here
            movingBodyDef.type = b2Body.b2_dynamicBody;//b2_dynamicBody is a property of b2Body. !manual is confusing here.
            movingBodyDef.position.Set(100.0 / RATIO, 100.0 / RATIO);
            movingBodyDef.angle = 30 * Math.PI / 180;

            _fallingCrate = _world.CreateBody(movingBodyDef);

            var dynamicBox:b2PolygonShape =  new b2PolygonShape();
            dynamicBox.SetAsBox(50.0 / RATIO, 50.0 / RATIO);

            var dynamicfixtureDef:b2FixtureDef = new b2FixtureDef();
            dynamicfixtureDef.shape = dynamicBox;
            dynamicfixtureDef.density = 1.0;
            dynamicfixtureDef.friction = 0.3;


            trace ("My world has "+_world.GetBodyCount()+" bodies");


  • \$\begingroup\$ :D Thanks, but your formatting's a little broken. :p \$\endgroup\$ Commented Oct 22, 2010 at 18:12

You should really check out the Box2D Manual page. It's well written and easy to understand.

To more directly address your question, it looks like you no longer use shape definitions to define shapes. You would simply create the shapes themselves, and use a fixture to attach the shape to a body.

b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.0f);

b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;


Hope this helps.


Here's a snippet of code directly from the game I'm working on:

        // Create the shape
        b2PolygonShape shape;
        // Create a "capsule" shaped object
        b2Vec2 vertices[8];
        vertices[0].Set( (-mWidth/2 + 5.0f) / PhysicsManager::mPhysicsScale, (mHeight/2) / PhysicsManager::mPhysicsScale);
        vertices[1].Set( (-mWidth/2) / PhysicsManager::mPhysicsScale, (mHeight/2 - 5.0f) / PhysicsManager::mPhysicsScale);
        vertices[2].Set( (-mWidth/2) / PhysicsManager::mPhysicsScale, (-mHeight/2 + 5.0f) / PhysicsManager::mPhysicsScale);
        vertices[3].Set( (-mWidth/2 + 5.0f) / PhysicsManager::mPhysicsScale, (-mHeight/2) / PhysicsManager::mPhysicsScale);
        vertices[4].Set( (mWidth/2 - 5.0f) / PhysicsManager::mPhysicsScale, (-mHeight/2) / PhysicsManager::mPhysicsScale);
        vertices[5].Set( (mWidth/2) / PhysicsManager::mPhysicsScale, (-mHeight/2 + 5.0f) / PhysicsManager::mPhysicsScale);
        vertices[6].Set( (mWidth/2) / PhysicsManager::mPhysicsScale, (mHeight/2 - 5.0f) / PhysicsManager::mPhysicsScale);
        vertices[7].Set( (mWidth/2 - 5.0f) / PhysicsManager::mPhysicsScale, (mHeight/2) / PhysicsManager::mPhysicsScale);
        shape.Set( vertices, 8 );

        // Create the fixture def
        b2FixtureDef fixtureDef;
        fixtureDef.shape = &shape;
        fixtureDef.density = mDensity;
        fixtureDef.friction = mFriction;
        fixtureDef.restitution = mRestitution;
        fixtureDef.isSensor = mIsSensor;
        if (contactGroup) {
            fixtureDef.filter.categoryBits = contactGroup->mCategoryBits;
            fixtureDef.filter.maskBits = contactGroup->mMaskBits;

        // Create the fixture
        b2Fixture *fixture = mBody->CreateFixture(&fixtureDef);
  • \$\begingroup\$ wait, I'm having some troubles...when I try running code like your guy's code I get errors like this: Syntax error: expected a definition keyword (such as function) after attribute b2PolygonShape, not shape. \$\endgroup\$ Commented Aug 21, 2010 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .