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.

Tags:

  • http://coob.co.uk chichlatte

    insanely useful!! thank you :)

  • http://www.psyked.co.uk/ James

    Thanks!

    I’ve just managed to get the source files for this demo on GitHub: http://github.com/psyked/Simple-Box2D

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

  • http://producerism.com T

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

  • http://www.psyked.co.uk/ James

    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.

  • http://producerism.com T

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

  • http://domi7777.olympe-network.com domi

    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.

  • http://www.psyked.co.uk/ James

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

  • http://domi7777.olympe-network.com domi

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

  • http://www.psyked.co.uk/ 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?

  • http://domi7777.olympe-network.com domi

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

  • http://www.tresloukadu.com.br 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.

  • http://www.psyked.co.uk/ 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

    Hi,
    How i can create (static) complex polygon??

  • http://ww.kursflash.pl RaVeL
  • adn

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

  • http://ww.kursflash.pl RaVeL

    yes Sir :)

  • adn

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

  • http://hyakkidour4n.deviantart.com/ 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!

  • http://www.psyked.co.uk/ 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.

  • http://hyakkidour4n.deviantart.com/ Hyakkidouran

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

  • Pingback: palmer