Simple Box2D – Better, cleverer, more optimised.

^ This time, that’s an image – saves on the page rendering time!

Another update on the Box2D classes I’m working on.  No new features, but some pretty cool expansions and improvements to the library object creation.

If you check out the previous post, you’ll see that the the dynamically drawn shapes weren’t exactly perfect; nor were they very efficient.  Oh they worked fine, and traced the detail of the shape very well – but it took a decent chunk of system resources to animate and ended up with some overlaid shapes; which all made the Box2D a bit sluggish and the behaviours buggy.

So, stealing incorporating ideas from other projects has been the name of the game – namely the ear clipping optimisation code from, combined with some of my own cobbled-together more optimised code for tracing curved edges.

Live demo:

Mouse interaction is enabled in the below example – click and drag the objects to check out how it works.

Download the source code for this demo.

* The source code isn’t exactly cleaned up – sometime, eventually, it will be.


  • chichlatte

    insanely useful!! thank you :)

  • James


    I’ve just managed to get the source files for this demo on GitHub:

    Where I’ll endeavour to keep all the source files from now on!

  • T

    can’t seem to compile the examples (,,, etc.)

    I tried the zip here, and the latest from github – I think it may be because I’m using FlashDevelop instead of Flex? Not sure, I haven’t had many problems compiling actionscript files.

    When I try to compile any of them, I get
    “psyked-Simple-Box2D\Box2D\Collision\Shapes\ col: 36 Error: Type was not found or was not a compile-time constant: b2FilterData.”

  • James

    I’m not sure about the or file, they’re both in the zip I’ve just downloaded from GitHub and work when I put it into a new project in Flex Builder.

    I did notice that the KeyboardManager class was missing for one of the examples, so I’ll get that updated on GitHub asap.

  • T

    all resolved, and got it working, thanks! Just had a conflicting library path

  • domi

    hi just wanted to let you know that we can change the max of Polygon Vertices in the file

    I changed it to 500 and it allowed me to build a perfect sinus wave with that code :

    var pt:int = 500;//pour + de 8 changer b2settings
    maShapeDef.vertexCount = pt;
    for (var i:int = 0; i < pt; i++)

    I don’t know about the performances, so far I’ll use this trick for a static ground shape.

  • James

    @domi – That’s quite interesting, I’ll have to try it out and see how it goes.

  • domi

    nevermind, could’nt make it work as a ground, other shapes pass trough. or maybe it’s just me.

  • James

    @domi – Remember that custom shapes in Box2D have to be concave and specified in a clockwise fashion, otherwise the shape doesn’t get created properly – have you checked that’s ok?

  • domi

    woops, missed the point they have to be convex. Anyway, box2d can’ help me doing my stuff ^^

  • tresloukadu

    hello, the box2d library you use in your example, is a modified one? Because when i tried run your example with my latest box2d library (the stable one not the alpha) it gave lots of errors and warnings.

  • James

    It’s using Box2D version 2.0 – a cached copy of the files are stored alongside the example files.

    The latest version of Box2D is 2.1, but I haven’t been able to resolve the compatibility problems with the latest version and this example – apologies for that.

  • adn

    How i can create (static) complex polygon??

  • RaVeL
  • adn

    Ravel z forum nutera? Już sobie poradziłem z tym… ;)

  • RaVeL

    yes Sir :)

  • adn

    Co durnia zgrywasz ? :P Czemu na forum nie napisałeś?

  • Hyakkidouran

    That is pretty amazing!
    I tried to take a look at the source and I started wondering… How do you create a box 2D shape from a shape that is in the same swf than the program?
    I only found a function to import from an external .swf.

    Your import library is pretty amazing, but I think it forces to put several .swf at a given spot when you want to publish your game, which won’t work on newgrounds or any flash game site that isn’t your own.

    If I want to put everything in only one swf, how should I use or edit your library?

    Thanks in advance for any indication!

  • James

    @Hyakkidouran – Because of the slightly hacky way the library is put together, the only way it’s possible to load library objects from the current SWF is to effectively re-load the current SWF, by specifying its filename and file path.

    To avoid having to hardcode filenames or locations, it’s probably possible to use the LoaderInfo class to retrieve URLs.

  • Hyakkidouran

    I see. I’ll try to look into that. Thanks for the help.

  • Pingback: palmer