Whilst porting my game framework to Haxe, the collision detection system started acting weird, I spent a little bit of time trying to fix it but realised it was going to require more time. This, however, was a good thing, rather than getting stuck straight in and trying to fix it, I thought it would be a great opportunity to learn Nape; a relatively new physics engine built in Haxe (utilising the speed benefits Haxe offers) but compiled for ActionScript. And if all goes well, I would replace my current collision methods with Nape.
I haven’t really used Box2D or any other physics engine but I once built a Peggle-esc demo game using Box2D and there were two things that bugged me about it, firstly its real-world unit system, everything was measured in metres which meant all my object positions and sizes had to be adjusted with a multiplier, not really a big issue but just isn’t nice to have to do. Second, the verbose code, just to make a box appear on the screen requires something like 9 lines of code. I guess I also have a third issue which is I just want more, more boxes, more collisions, more destruction. Perhaps this is not entirely down to Box2D’s.
Now comparing Nape to Box2D, firstly it works in pixels which is just so much nicer and easier to work with. Second, the api is clean and simple and much faster to implement. And for my third issue, Nape is faster so I can have more boxes, more collisions and more deconstruction.
But as with everything I do, I like to shop around first and before I integrated Nape into my framework, I wanted to make sure I had picked the best engine. Because I am working with Haxe I wanted to compare Nape with a Haxe port of Box2D, I also wanted to test out Physaxe, a Haxe based physics engine written by the very talented creator of Haxe, Nicolas Cannasse, it has to be good! There are several ports of Box2D for Haxe but I chose Joshua Granicks port because of his great work on NME and because he is always willing to help out a Haxe/NME n00b(me).
Each test built with Haxe and deployed to Flash, JavaScript (Via NME Jeash) and Android (via NME). The test is pretty crude involving dropping 2000 randomly rotated primitives onto a floor. I have no hard numbers, but here is a rough roundup of what I experienced, also I have put links to the tests at the bottom so you could run them yourself.
- Flash Test., Nape outperformed the other two by quite some margin. Box2D and Physaxe performed comparably.
- Android Test. All three performed comparably here. The optimizations Nape uses when targeting Flash cannot be used here, which probably explains why it does not outperform the other two.
- Javascript (HTML5) Test: Sadly I could not get Nape to work correctly. In Chrome nothing is displayed and in Firefox some stuff is displayed but generally really buggy.
I was a little saddened by these results, currently Flash is my primary target and the fact that Nape outperforms the other on this target along with its clean api, it would have been a perfect choice, but not being able to run in JavaScript kind of defeats the whole purpose of me using Haxe. I didn’t want to have to use Box2D and whilst Physaxe was fast, the physics seemed a little spongy (something I’m sure could be tweaked) and the library is quite limited, whilst the objects would bounce off each other, there are no events or signals fired for these collisions meaning if you wanted to use it in a game you would have to poll the objects to test if they were colliding which seems a bit pointless. I should also mention that although Nape is built in Haxe, I did have a bit of trouble getting it to work with NME but that’s was more to do with loading a Flash SWF into NME.
So where does this leave me, well back at square one. I will have to go back to my collision detection and finish porting it to Haxe. There’s so much development going on right now that I’m sure if I were to re-create these tests in a couple of months I would get totally different results.
Below are some screenshots I took of the tests running and also there are links to the tests at the bottom. It’s worth noting is that the JavaScript versions generally had half the performance of the Flash versions and that Nape had twice the performance of Box2D and Physaxe.
Click the images to open the test.
Box2D Haxe Flash
Box2D Haxe Javascript
Box2D Nape
Physaxe Flash
Physaxe Javascript
Share Your Thoughts