Box2DFlash 2.1a released – what changed
Some days ago Boris the Brave released the new version of the popular physics library.
There are some critical changes that won’t make old projects run in the new environment.
The most important ones are:
- It is now not necessary to specify a size for your world, it’ll always be large enough.
- Improved collisions system
- You must specify if a body is dynamic, no matter if its mass is greater than zero
- Fixtures now determine material properties of shapes, such as density, friction and so on
But I think an example will be more explicative than a thousand words, so I created a simple vehicle you can control with left and right arrow keys. It uses revolute joints and motors.
This is the code you would need in the old Box2D version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class car extends Sprite { public var world:b2World; public var car_body:b2Body; public var front_wheel:b2Body; public var rear_wheel:b2Body; public var front_motor:b2RevoluteJointDef = new b2RevoluteJointDef(); public var rear_motor:b2RevoluteJointDef = new b2RevoluteJointDef(); public var rear_motor_added:b2RevoluteJoint public var front_motor_added:b2RevoluteJoint public function car():void { // world setup var worldAABB:b2AABB = new b2AABB(); worldAABB.lowerBound.Set(-100, -100); worldAABB.upperBound.Set(100, 100); world=new b2World(worldAABB,new b2Vec2(0,10.0),true); debug_draw(); // variables used for bodies and motors var body:b2Body; var bodyDef:b2BodyDef= new b2BodyDef(); var boxDef:b2PolygonDef = new b2PolygonDef(); var circleDef:b2CircleDef= new b2CircleDef(); var revoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef(); // adding the ground bodyDef.position.Set(250/30, 200/30); boxDef.SetAsBox(600/30, 20/30); boxDef.friction=1; boxDef.density=0; body=world.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes(); bodyDef.position.Set(0/30, 200/30); boxDef.SetAsBox(50/30, 50/30); boxDef.friction=1; boxDef.density=0; body=world.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes(); bodyDef.position.Set(500/30, 200/30); boxDef.SetAsBox(50/30, 50/30); boxDef.friction=1; boxDef.density=0; body=world.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes(); // adding car body bodyDef.position.Set(250/30, 90/30); car_body=world.CreateBody(bodyDef); boxDef.density=1; boxDef.friction=1; boxDef.restitution=0.1; boxDef.SetAsBox(50/30, 10/30); car_body.CreateShape(boxDef); car_body.SetMassFromShapes(); // adding wheels circleDef.radius=20/30; circleDef.density=1; circleDef.friction=5; circleDef.restitution=0.1; // front wheel bodyDef.allowSleep = false; bodyDef.position.Set(car_body.GetWorldCenter().x + 40/30, car_body.GetWorldCenter().y); front_wheel=world.CreateBody(bodyDef); front_wheel.CreateShape(circleDef); front_wheel.SetMassFromShapes(); // rear wheel bodyDef.position.Set(car_body.GetWorldCenter().x - 40/30, car_body.GetWorldCenter().y); rear_wheel=world.CreateBody(bodyDef); rear_wheel.CreateShape(circleDef); rear_wheel.SetMassFromShapes(); // front joint front_motor.enableMotor=true; front_motor.maxMotorTorque=10; front_motor.Initialize(car_body, front_wheel, front_wheel.GetWorldCenter()); front_motor_added=world.CreateJoint(front_motor) as b2RevoluteJoint; // rear joint rear_motor.enableMotor=true; rear_motor.maxMotorTorque=1; rear_motor.Initialize(car_body, rear_wheel, rear_wheel.GetWorldCenter()); rear_motor_added=world.CreateJoint(rear_motor) as b2RevoluteJoint; // addEventListener(Event.ENTER_FRAME, update); stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down); } public function key_down(event:KeyboardEvent):void { switch (event.keyCode) { case 39 : rear_motor_added.SetMotorSpeed(10); front_motor_added.SetMotorSpeed(10); break; case 37 : rear_motor_added.SetMotorSpeed(-10); front_motor_added.SetMotorSpeed(-10); break; } } public function debug_draw():void { var m_sprite:Sprite; m_sprite = new Sprite(); addChild(m_sprite); var dbgDraw:b2DebugDraw = new b2DebugDraw(); var dbgSprite:Sprite = new Sprite(); m_sprite.addChild(dbgSprite); dbgDraw.m_sprite=m_sprite; dbgDraw.m_drawScale=30; dbgDraw.m_alpha=1; dbgDraw.m_fillAlpha=0.5; dbgDraw.m_lineThickness=1; dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit; world.SetDebugDraw(dbgDraw); } public function update(e : Event):void { world.Step(1/30, 10); } } } |
Take a look at the compiler errors…

And this is the new one. All changes are commented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class car extends Sprite { public var world:b2World; public var car_body:b2Body; public var front_wheel:b2Body; public var rear_wheel:b2Body; public var front_motor:b2RevoluteJointDef = new b2RevoluteJointDef(); public var rear_motor:b2RevoluteJointDef = new b2RevoluteJointDef(); // this is new!!! public var fixtureDef:b2FixtureDef = new b2FixtureDef(); public var rear_motor_added:b2RevoluteJoint; public var front_motor_added:b2RevoluteJoint; public function car():void { // world setup /*var worldAABB:b2AABB = new b2AABB(); worldAABB.lowerBound.Set(-100, -100); worldAABB.upperBound.Set(100, 100);*/ world=new b2World(new b2Vec2(0,10.0),true);// was world=new b2World(worldAABB,new b2Vec2(0,10.0),true); debug_draw(); // variables used for bodies and motors var body:b2Body; var bodyDef:b2BodyDef= new b2BodyDef(); var boxDef:b2PolygonShape = new b2PolygonShape();// was var boxDef:b2PolygonDef = new b2PolygonDef(); var circleDef:b2CircleShape=new b2CircleShape(20/30);// was var circleDef:b2CircleDef = new b2CircleDef(); var revoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef(); // adding the ground bodyDef.position.Set(250/30, 200/30); boxDef.SetAsBox(600/30, 20/30); // this is new!! fixtureDef.shape=boxDef; fixtureDef.friction=1;// was boxDef.friction=1; fixtureDef.density=1;// was boxDef.density=0; body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef);// was body.CreateShape(boxDef); // remove: body.SetMassFromShapes(); bodyDef.position.Set(0/30, 200/30); boxDef.SetAsBox(50/30, 50/30); fixtureDef.shape=boxDef; fixtureDef.friction=1;// was boxDef.friction=1; fixtureDef.density=1;// was boxDef.density=0; body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef);// was body.CreateShape(boxDef); // remove: body.SetMassFromShapes(); bodyDef.position.Set(500/30, 200/30); boxDef.SetAsBox(50/30, 50/30); // this is new!! fixtureDef.shape=boxDef; fixtureDef.friction=1;// was boxDef.friction=1; fixtureDef.density=1;// was boxDef.density=0; body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef);// was body.CreateShape(boxDef); // remove: body.SetMassFromShapes(); // adding car body bodyDef.position.Set(250/30, 90/30); // this is new!! bodyDef.type=b2Body.b2_dynamicBody; boxDef.SetAsBox(50/30, 10/30); // this is new!! fixtureDef.shape=boxDef; fixtureDef.density=1;// was boxDef.density=1; fixtureDef.friction=1;// was boxDef.friction=1; fixtureDef.restitution=0.1;// was boxDef.restitution=0.1; car_body=world.CreateBody(bodyDef); car_body.CreateFixture(fixtureDef);// was body.CreateShape(boxDef); // remove: car_body.SetMassFromShapes(); // adding wheels // remove: circleDef.radius=20/30; fixtureDef.density=1;// was circleDef.density=1; fixtureDef.friction=5;// was circleDef.friction=5; fixtureDef.density=0.1;// was circleDef.restitution=0.1; // this is new!! fixtureDef.shape=circleDef; // front wheel bodyDef.allowSleep=false; bodyDef.position.Set(car_body.GetWorldCenter().x + 40/30, car_body.GetWorldCenter().y); front_wheel=world.CreateBody(bodyDef); front_wheel.CreateFixture(fixtureDef);// was front_wheel.CreateShape(circleDef); // remove: front_wheel.SetMassFromShapes(); // rear wheel bodyDef.position.Set(car_body.GetWorldCenter().x - 40/30, car_body.GetWorldCenter().y); rear_wheel=world.CreateBody(bodyDef); rear_wheel.CreateFixture(fixtureDef);// was rear_wheel.CreateShape(circleDef); // remove: rear_wheel.SetMassFromShapes(); // front joint front_motor.enableMotor=true; front_motor.maxMotorTorque=10; front_motor.Initialize(car_body, front_wheel, front_wheel.GetWorldCenter()); front_motor_added=world.CreateJoint(front_motor) as b2RevoluteJoint; // rear joint rear_motor.enableMotor=true; rear_motor.maxMotorTorque=1; rear_motor.Initialize(car_body, rear_wheel, rear_wheel.GetWorldCenter()); rear_motor_added=world.CreateJoint(rear_motor) as b2RevoluteJoint; // addEventListener(Event.ENTER_FRAME, update); stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down); } public function key_down(event:KeyboardEvent):void { switch (event.keyCode) { case 39 : rear_motor_added.SetMotorSpeed(10); front_motor_added.SetMotorSpeed(10); break; case 37 : rear_motor_added.SetMotorSpeed(-10); front_motor_added.SetMotorSpeed(-10); break; } } public function debug_draw():void { var m_sprite:Sprite; m_sprite = new Sprite(); addChild(m_sprite); var dbgDraw:b2DebugDraw = new b2DebugDraw(); var dbgSprite:Sprite = new Sprite(); m_sprite.addChild(dbgSprite); dbgDraw.SetSprite(m_sprite); // was dbgDraw.m_sprite=m_sprite; dbgDraw.SetDrawScale(30); // was dbgDraw.m_drawScale=30; //dbgDraw.SetAlpha(1); // was dbgDraw.m_alpha=1; dbgDraw.SetFillAlpha(0.5); // was dbgDraw.m_fillAlpha=0.5; dbgDraw.SetLineThickness(1); // was dbgDraw.m_lineThickness=1; dbgDraw.SetFlags(b2DebugDraw.e_shapeBit); // was dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit; world.SetDebugDraw(dbgDraw); } public function update(e : Event):void { world.Step(1/30,10,10);// was world.Step(1/30, 10); // this is new! world.ClearForces() // this is new!! world.DrawDebugData(); } } } |
Download the source code, new library included.
They can be easily customized to meet the unique requirements of your project.















(22 votes, average: 4.77 out of 5)









This post has 19 comments
Luiz Fernando
Hmm nice. I’ll check it to see if there was any substantial changes on the engine itself…
Bindiry
I only hope release it early.
Vlad
So basically, Emanuelle, if we try to use this version of box2das3 with your previous tutorials… ouch.
Rackdoll
thnx for this tut. Was wondering what i did wrong with all those compiler errors. Now i see the finer of it. Good.
Keep on the tuts \o/
Rackdoll
ricky
Actually, I was about to learn Box2d yesterday and grabbed a copy of your Box2d for absolute beginners when I got surprised that I got lots of errors.I was really disappointed…how come the hello world of Borris don’t seem to work?
Will you do a Box2d for absolute beginners the updated version. I was really waiting for your post on the new Box2d changes. Thanks you make complicated things sound simple.You could be a professor.:)
rishabh
@ricky
he is like a professor to me
i liked the fixtures and all
got major reusability made now
but i think we need another begginer tut on this =[
about the old projects thing
if you have time rename the new version to Box2DA and then change it each and every class
thats what i did so my older projects work (they are not that old but i downloaded the new source today)
Monkios
Nice …
So the guy made a version of his thing wich is not backward-compatible ?
How unuseful !
rishabh
minkios:
it isn’t unuseful at all , it has a lot of changes which help,though i haven’t looked into all
ozdy
Hi guys, sounds great.
Any idea if the flash 10 version has performance enhancements (like using vectors)?
Oscar
HI, looks some code is gone, like boundaryListener how this works now? some idea? Thanks.
akipponn
Hi, what a great entry! This page is so helpful.
from Kyoto, Japan :-)
Box2dflash 2.1a ??????????????????????? | aki note
[...] Box2dflash ? C++ ??????????? Box2d ? Flash ??????????????2008 ??????????????????????????????????????????????????????????????????????????????????? BOX2DFLASH 2.1A RELEASED – WHAT CHANGED @ EMANUELE FERONATO » italian geek and PROgrammer http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/ [...]
box2d flash new release + a minimal sample | aki note
[...] http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/ [...]
Box2D tutorial for the absolute beginners – revamped : Emanuele Feronato - italian geek and PROgrammer
[...] 2.1 release, a lot of things changed, so it’s time to publish another tutorial for the absolute [...]
Create incredible particle effects with Partigen 2 : Emanuele Feronato - italian geek and PROgrammer
[...] In this example, I am going to create a particle effect with the user interface provided in the component, then I’ll export it with XML and finally add it “on the fly” to the Box2D car example. [...]
Understanding Box2D’s one-way platforms, aka CLOUDS : Emanuele Feronato - italian geek and PROgrammer
[...] of the new features introduced with Box2D 2.1a is the improved contact listener class which comes in hand when we want to create one-way [...]
sergey
please explain how works new collisions and how a i can catch them
la
You really saved newbies’ lives!
Viktor
mistake on line 80
80 fixtureDef.density=0.1;// was circleDef.restitution=0.1;