I’ve been fiddling around a while with trying to make some sort of flash based physics game. I started out with AS3 + Box2D. Then I found out about haXe and PhysaXe. I was really impressed by it’s perfomance, so I decided to convert my code to haXe and throw out Box2D. But after a while I realized that while physaxe is very snappy, it does lack a lot of features and isn’t very well documented.
So now I wanted to switch back to Box2D again. The problem with that is that there is no Box2D port available for haXe. After some googling i found out that it is possible to use AS3 libs from haXe by incorporating an existing swf into the one compiled by haXe. This is done using the –swf-lib directive in the .hxml file and using the –gen-hx-classes switch to generate haXe class headers for that swf.
So I did that with Box2D. But guess what. Box2D does not follow the AS3 naming convention, which is apparently enforced in haXe. The Box2D classes look like this: “Box2D/Dynamics/b2World.as” and so on. haXe requires that package names start with lower case letters and class names with upper case, i.e “box2d/dynamics/B2World.as”. So in order to get it working I had to change the naming of all the Box2D classes and paths to match haXes requirements. Once that was taken care of it worked great!
Here is the Box2D swf together with the hx classes that I generated. To use it, put the box2d directory in your class path and put –swf-lib path/to/Box2D.swf in your .hxml file. Then you can simply use “import box2d.dynamics.B2Body;” and so on for the box2d classes that you need, and use box2d as you would from AS3. But remember that all the box2d class names start with an upper case B, so if you copy and paste AS3 code you’ll have to change that.
Oh, and the Box2D version is 2.0.1 btw.
How much more performance do you think could be gained by actually compiling Box2D in HaXe? If I understood what you did, you compiled it in the AS3 compiler and figured out how to use that swf with your HaXe project?
There is actually a haXe port of Box2D available on google code at http://code.google.com/p/box2dhx/. But simply porting the AS3 version to haXe does not seem to make a huge difference, performance wise. While the haXe compiled Box2D test bed does seem to run slighty faster than the AS3 one, other tests I conducted didn’t run any better at all. So until the haXe port has been properly optimized, the perfomance gain is negligible.
But I suppose the main advantage of having a haXe version of Box2D would be that it could be compiled for other targets as well as flash 9 and 10.
ohh, box2dhx was already existing.. I have also ported box2d and little optimized it (1.5…2 times faster)
http://code.google.com/p/box2d-haxe
Oh, that is nice! I’ll check it out.
Maybe at some point in the future, the best parts of each port could be merged to create a super port :)
Hello
Thank you for sharing this. It is interesting to see that a swf lib in AS3 is such an easy way to go :)
We introduced haXe in Silex (we start a port from all the js/as/php code to haXe js/as/php). And I believe that we could face the naming convention thing… I think that now, N. Cannasse has added a compilation parameter or something… Tell me if you need to know more (or post on silex labs blog/forums)
Bye
lexa