Changes to Box2DFlash “Hello World” example.

I’ve talked about the basics of Box2DFlash v1.4.3 in my Introductory tutorial. I showed how to create your world and how to introduce simple bodies into that world. With the release of Box2DFlash v2.0.0 the basics remain mostly the same, there are a few details changed. I need to go over these small changes before I move on to the next step of adding joints and forces to the bodies.

The first and biggest change (although really a small thing) is that we no longer work in pixels! We now work in units that by default equal 30 pixels to 1 unit (think of it as meters; 30px : 1m). With this change our normal gravity is no longer (0, 300) but instead (1, 10). This makes more sense to me at least because gravity in reality is 9.81 m/s/s. This is going to effect almost everything you do, from applying forces to setting the width and height of a box.

The reason for such a change (I believe) is to make it easier for you to dynamically scale a project. Just change the ratio of pixels per unit and everything should scale correctly.

The second change is the way we add objects to the world. The creation of a new shape and object is still very much the same, but we now have the ability to adjust the center of mass. For now, just leaving the center of mass to auto calculate we need to get the body object when adding the new bodyDef to the world.

var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;

// Add ground body
bodyDef = new b2BodyDef();
bodyDef.position.Set(10, 12);  //Small numbers…  remember it’s scaled 30:1
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30, 3);  //THIS IS NEW!
boxDef.friction = 0.3;

// Add sprite to body userData
bodyDef.userData = new PhysGround();
bodyDef.userData.width = 30 * 2 * 30;
bodyDef.userData.height = 30 * 2 * 3;
addChild(bodyDef.userData);
body = m_world.CreateStaticBody(bodyDef);  //THIS IS MOSTLY NEW
body.CreateShape(boxDef);  //THIS IS NEW
body.SetMassFromShapes();  //THIS IS NEW

Haha! I guess I forgot to mention that we no longer have a b2BoxDef anymore… we only have b2PolyDef (renamed b2PolygonDef). We can quickly define it as a box though using the method public function SetAsBox(hx:Number, hy:Number):void.

Anyways, let us bring our attention back to adding the object to the world. When you add the object to the world, you are creating it as either a static (unmoving) body or as a dynamic body (body = m_world.CreateDynamicBody(bodyDef);).
It then returns the new body object BY REFRENCE. It is not giving you a copy of that object. This way when we make changes, such as add the shape and set the mass (center of mass really) we don’t have to go send it back again.

The method public function SetMassFromShapes():void is the way to auto calculate and set your center of mass. Otherwise you have to us public function SetMass(massData:b2MassData):void, but this is a whole new complication.

OK! With that, we are pretty much back up to speed after my last tutorial.

5 Responses to “Changes to Box2DFlash “Hello World” example.”

  1. Luke says:

    Hey Paul, Thanks for doing these tutorials – they’re one of the few Box 2d flash resources on the web that isn’t overly complicated, and using the library as graphics is very helpful. Even with the changes you’ve outlined here, I still can’t get this working for version 2 though. Could you provide the source code or the zipped AS and FLA files? These would be really great.

  2. gordee says:

    Yeah me too please! I have lots of other compile issues with m_world etc

  3. bryan mcdonald says:

    these are terrific tutorials! thank you very much.

    i am not able to get the code working either. the fla files would be most appreciated and Box2d seems to have changed and broken your old code.

    thank you for your consideration.

  4. Paul says:

    Yeah, I wouldn’t be surprised if Box2D has been updated since I wrote this.

  5. bryan mcdonald says:

    Thanks Paul for the response.

    So if one wants some flash code that works with the latest version of Box2D, there is “PhysTest.fla” that comes with the Box2DFlashAS3_2 download from:
    http://box2dflash.sourceforge.net/

Leave a Reply