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 Splashdust.net, 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.

21 thoughts on “Simple Box2D – Better, cleverer, more optimised.

  1. can’t seem to compile the examples (box2dexperiments.as, complexpolygonexample.as, curvedshapeexample.as, 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\b2Shape.as(81): col: 36 Error: Type was not found or was not a compile-time constant: b2FilterData.”

  2. I’m not sure about the b2Shape.as or b2FilterData.as 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.

  3. hi just wanted to let you know that we can change the max of Polygon Vertices in the file B2D.common.b2eSttings.as.

    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++)
    b2Vec2(maShapeDef.vertices[i]).Set(i/10,Math.sin(i/10));

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

  4. @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?

  5. 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.

  6. 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.

  7. 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!

  8. @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.

  9. Pingback: palmer

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>