Simplifying Box2DAS3…

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.

,

About James

James is a Senior New Media Developer at MMT Digital, and has BA(Hons) in Design for Interactive Media from the University of Gloucestershire. He loves designing and producing all sorts of website and Flash-related things, as well as prattling on about technologies.Day-to-day he works with Flash, Dreamweaver, Director, Microsoft Office Sharepoint Server 2007 (MOSS) and in his spare time he mucks about in Flex and Wordpress.Follow James on Twitter.

10 Responses to “Simplifying Box2DAS3…”

  1. leef 8th January, 2010 at 10:23 am # Reply

    Thanks James! I’ll be watching = )

  2. Andy 8th January, 2010 at 10:44 am # Reply

    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

  3. James 8th January, 2010 at 11:22 am # Reply

    Hmm, maybe that WordPress donations plug-in isn’t a bad idea! I’ll check out FGL as well…

  4. Eric Holm 8th January, 2010 at 11:33 am # Reply

    Have you seen this project?
    http://actionsnippet.com/?page_id=1391

  5. reyco1 8th January, 2010 at 2:32 pm # Reply

    I actually created a wrapper library for Box2DFlashAS3 some time back because of the same reasons :

    http://blog.reyco1.com/box2dwrapper-box2dflashas3-made-simple/

  6. James 8th January, 2010 at 3:55 pm # Reply

    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!

  7. James 10th January, 2010 at 11:48 am # Reply

    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!

  8. mnu7 12th November, 2010 at 12:37 pm # Reply

    how to add any other object other than polygons
    I like to add image in place of polygons.

  9. MC 30th January, 2011 at 9:20 am # Reply

    Great!
    Any chance to port this to the last Box2D version? (2.1a)

    Regards

  10. James 2nd February, 2011 at 10:18 am # Reply

    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.

Leave a Reply

Note: This post is over 2 years old. You may want to check later in this blog to see if there is new information relevant to your comment.