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.

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.

21 Responses to “Simple Box2D – Better, cleverer, more optimised.”

  1. chichlatte 9th February, 2010 at 10:27 pm # Reply

    insanely useful!! thank you :)

  2. James 10th February, 2010 at 12:28 pm # Reply

    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!

  3. T 25th March, 2010 at 2:42 am # Reply

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

  4. James 25th March, 2010 at 12:03 pm # Reply

    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.

  5. T 25th March, 2010 at 1:06 pm # Reply

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

  6. domi 27th March, 2010 at 1:30 pm # Reply

    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.

  7. James 27th March, 2010 at 10:46 pm # Reply

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

  8. domi 28th March, 2010 at 5:33 am # Reply

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

  9. James 28th March, 2010 at 3:24 pm # Reply

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

  10. domi 28th March, 2010 at 4:08 pm # Reply

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

  11. tresloukadu 21st September, 2010 at 12:14 pm # Reply

    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.

  12. James 21st September, 2010 at 10:22 pm # Reply

    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.

  13. adn 20th October, 2010 at 10:50 pm # Reply

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

  14. RaVeL 2nd November, 2010 at 4:16 pm # Reply

    hi adn

    look here
    http://www.box2dflash.org/docs/2.0.2/manual#About_3

    cheers

  15. adn 2nd November, 2010 at 8:44 pm # Reply

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

  16. RaVeL 3rd November, 2010 at 8:50 am # Reply

    yes Sir :)

  17. adn 3rd November, 2010 at 8:39 pm # Reply

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

  18. Hyakkidouran 7th March, 2011 at 1:04 pm # Reply

    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!

  19. James 7th March, 2011 at 10:46 pm # Reply

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

  20. Hyakkidouran 11th March, 2011 at 11:06 am # Reply

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

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.