Box2D AS3 port showdown


You might recall all the way back in my first Box2D tutorial, that I briefly mentioned there was two main ports of the Box2D engine for ActionScript 3.0, those being BorisTheBrave’s version and Jesse Sternberg’s alchemy version.

For all these tutorials we have been using BorisTheBrave’s 2.1a, which leads me to discuss a recent video tutorial publish by the Adobe evangelist, Lee Brimelow. Lee’s excellent tutorial shows how to use the WCK framework to create Box2D worlds by laying out the various elements on the stage. This WCK framework makes use of the alchemy version of Box2D which led to a few readers to ask if the alchemy version of Box2D was faster than BorisTheBrave’s version.

Now I have never really tested the two versions before, and so was curious to find out myself, however, before we get to that, a little background information on the alchemy port.

The alchemy version of Box2D leverages Adobe’s Alchmey research project. Richard Szalay sums it up best as

“Simply put, it’s an virtual machine that uses a ByteArray as it’s memory store. The C code compiled by Alchemy has direct access to “memory” (via some opcodes introduced in Flash 10), allowing it to chunk memory around at it’s leisure (including pointers to objects). This results in some, but by no means all, code running faster. Some types of code will actually run slower in Alchemy due to it being a VM running on top of the AVM (another VM). Additionally, Alchemy does not have native access to ActionScript classes and must access them through interop classes.” - Richard Szalay

So while alchemy can provide a speed increase, it is by no means a silver bullet for performance.

My first test was to modify one of my earlier tutorials by increasing the ball count to 400 and test both Box2D ports on a Windows XP machine at work. Early results suggested they were both on par for performance, but surprisingly BorisTheBrave’s version used half as much ram.

These results surprised me, so I decided to recreate a new test from scratch, running on my home machine which is using Windows 7 64bit. BorisTheBrave’s version averaged about 14 FPS and used on average 17MB of ram. The Alchemy port averaged about 19 FPS and used on average 13MB of ram. Apart from outpeforming BorisTheBrave’s port, it was also much more consistent with memory usage. As it is currently the holidays I have been unable to reexamine my original test, but I feel these latest tests are more accurate. So please, feel free to post your results.

source files

source files

I should point out that you cannot simply plug in the Alchemy port of Box2D when you have written code for BorisTheBrave’s port and vice versa. That being said, it is not every hard to tweak it. The biggest change I found was regarding the usage of Vectors.

With BorisTheBrave’s version we always use a b2Vec2 class. With Alchemy we sometimes use the b2Vec2 and sometimes use another new class called V2. V2 is what you create instances of, you never create instances of b2Vec2 anymore. This is because it now only accepts one parameter which acts like a pointer. However, there are still classes that use the b2Vec2 so simply just change the x and y properties and don’t assign a new instance to it.

Apart from that, the other changes are typically properties that were actually functions that are now real properties. For example, b2ChircleShape, the radius is set by setting the m_radius property. Feel free to browse my code to see the changes. If you have any comments, just post them below.

  • Mystalkerway

    alchemy Boris
    FPS 12-14 6-8
    RAM 38(max =41) 47(max = 52)

  • Mystalkerway

    for alchemy – FPS = 12-14, Ram=38(max = 41)
    for Boris – FPS = 6-8, Ram=47(max=52)

  • http://twitter.com/AllanBishop Allan Bishop

    Thanks. What OS was this on?

  • Dan4ez

    os: Windows 7
    AlchemyTest: FPS 12-13 & 14-16; RAM: 12.5-14.5
    BorisTheBraveTest: FPS 6-7; RAM: 15-19

  • producerism

    I had similar results as Dan4ez

    os: Windows 7
    AlchemyTest: FPS 12-15; RAM: 12-14
    BorisTheBraveTest: FPS 9-10; RAM: 14-20

  • Kaliostro

    XP

  • Aymeric

    On OS X 10.6
    AlchemyTest : FPS 12-14; RAM : 18
    BorisTheBraveTest : FPS 5-11; RAM : 22

  • http://twitter.com/AllanBishop Allan Bishop

    Thanks everyone for posting their results. So it seems pretty conclusive that the Alchemy port is faster and uses less ram.

  • producerism

    Greetings Allan, just wondering if you have tested another physics engine called NAPE. It’s not based on box2d, its actually based on chipmunk and a few other things. May find it interesting, as it’s been getting a bit of buzz lately.

    http://code.google.com/p/nape/

  • Anonymous

    Interesting, I had not heard of NAPE before. Seems to be quite promising (and the documentation decent). Will definitely look into it more. Cheers

  • m1gu3l

    win7 64 @ i7-720m / fp 10.2 @ chrome
    alchemy port: 19fps 16mb
    as3 port: 17fps 20mb

    later it looks like the as3 version has some memory leak

  • Asdf

    I had 3 fps better performance with BorisTheBrave port.

  • Lukas

    windows 7 64bit, my 5 years old dell laptop:

    alchemy: 17-19 fps
    another: 20-23 fps
    hope it helps :)