One of the downsides to the Box2DAS3 project – and probably one of the major hurdles to most Flash developers – is the fact that it’s inherited a lot of the syntax from the C++ project that it’s derived from. Maybe it’s just because we’re used to it, but Actionscript is pretty easy to understand, and its methods of working pretty tolerant of inefficient coding.
C++ – or whatever Box2D is written in – is not, and it’s a little painful to setup and easy to break. For my sanity as much as anyone else’s I’m working on a set of classes in AS3 that wrap around the Box2DAS3 classes, and provide you with a more familiar syntax for working with Box2D – objects, methods and utilities that makes it quicker to throw things together and don’t require you to rethink the way you work.
Well, that’s the eventual aim anyway.
Here’s the result of the first round of development – creating a Box2D world and adding objects, in about 6 lines of code.
var options:Box2DWorldOptions = new Box2DWorldOptions( 500, 280, 30, 9.8 );
options.setWorldEdges( true, true, true, true );
var world:Box2DWorld = Box2DUtils.createBoxedWorld( options );
world.debugDraw = true;
world.animateOnEnterFrame = true;
addChild( world );
for ( var i:int = 0; i < 30; i++ )
{
world.createCircle( 500 * Math.random(), 280 * Math.random(), 50 * Math.random());
}
And here’s the result:
Not at the point of getting any interaction, or anything more interesting yet, but I’ve create a utility class for creating Box2D worlds and a Box2DWorld class (as opposed to b2World, which it extends). The aim of these classes is to condense about 40 – 60 lines of initialisation and basic scenarios into just a few lines, using default variables whilst still allowing for optional overrides. So if you wanted a world with 0 gravity, that’s just a case of changing it at the start.
I’ll post the classes soon – just as soon as I decide the best way to do so. It’ll all become more clear then, I hope.
Thanks James! I’ll be watching = )
Great stuff, you know you could actually make some money of this, either via donations or some other means… The people at FGL would probably be very interested in this, you should check it out, even sign up
http://www.flashgameslicense.com
Hmm, maybe that WordPress donations plug-in isn’t a bad idea! I’ll check out FGL as well…
Have you seen this project?
http://actionsnippet.com/?page_id=1391
I actually created a wrapper library for Box2DFlashAS3 some time back because of the same reasons :
http://blog.reyco1.com/box2dwrapper-box2dflashas3-made-simple/
It appears the idea of an AS3-syntax wrapper for Box2D is not quite so original as first thought!
QuickBox2D – I saw it when I started learning Box2D, but never really picked it up. Looks good, and definitely has a few ideas I’ll pinch myself. Not quite sure why I didn’t try it out – but at least when I write my own classes I can extend them easier (I have some crazy things planned).
@reyco1 – Cool wrapper library – amazing how similar the solutions are already!
Quick update for y’all – next version has some cleverness for creating complex, convex polygons – from points in an Array and even library shape data. Releasing as soon as I’ve finished cleaning up the code!
how to add any other object other than polygons
I like to add image in place of polygons.
Great!
Any chance to port this to the last Box2D version? (2.1a)
Regards
Unfortunately no – no immediate plans to update to Box2D 2.1. I’ve tried once, but it blew my mind trying to make the changes to 2.1.