Psyked *
it’s easy once you know how.Simple Box2D – Better, cleverer, more optimised.
Posted by James - 03/02/10 at 09:02:44 am
^ 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.



insanely useful!! thank you
Comment by chichlatte — 9 February 2010 #
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!
Comment by James — 10 February 2010 #
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.”
Comment by T — 25 March 2010 #
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.
Comment by James — 25 March 2010 #
all resolved, and got it working, thanks! Just had a conflicting library path
Comment by T — 25 March 2010 #
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.
Comment by domi — 27 March 2010 #
@domi – That’s quite interesting, I’ll have to try it out and see how it goes.
Comment by James — 27 March 2010 #
nevermind, could’nt make it work as a ground, other shapes pass trough. or maybe it’s just me.
Comment by domi — 28 March 2010 #
@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?
Comment by James — 28 March 2010 #
woops, missed the point they have to be convex. Anyway, box2d can’ help me doing my stuff ^^
Comment by domi — 28 March 2010 #