Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.)

share|improve this question
1  
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 –  Allan Oct 25 '10 at 2:07
add comment

3 Answers

up vote 1 down vote accepted

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)

   package{

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
            setupWorld();

            //2. create walls and floor         
            createWallsAndFloor();

            //3. make create            
            addACrate();

            setupDebugDraw();

            addEventListener(Event.ENTER_FRAME, newFrameEvent);
        }


private function setupDebugDraw():void {

        var spriteToDrawOn:Sprite = new Sprite();
        addChild(spriteToDrawOn);

        var artistForHire:b2DebugDraw = new b2DebugDraw();
        artistForHire.SetSprite(spriteToDrawOn);

        artistForHire.SetDrawScale(RATIO);
        artistForHire.SetFlags(b2DebugDraw.e_shapeBit);
        artistForHire.SetLineThickness(2.0);
        artistForHire.SetFillAlpha(0.6);

        _world.SetDebugDraw(artistForHire);
}


private function newFrameEvent(e:Event):void {


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

    _world.DrawDebugData();

    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;

            groundBody.CreateFixture(fixtureDef);

            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;

            _fallingCrate.CreateFixture(dynamicfixtureDef);

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




        }








    }
}
share|improve this answer
    
:D Thanks, but your formatting's a little broken. :p –  Ullallulloo Oct 22 '10 at 18:12
    
Thanks, Tetrad. :) –  Ullallulloo Oct 26 '10 at 1:35
add comment

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;

body->CreateFixture(&fixtureDef);

Hope this helps.

share|improve this answer
add comment

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);
share|improve this answer
    
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. –  Ullallulloo Aug 21 '10 at 14:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.