Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

Box2D Vs Nape: Hello both worlds

Nape is an open source physics engine written I Haxe by Luca Deltodesco which is also available for AS3 as swc library and is going to generate some interest about it.

In this post, I am creating a simple Hello World for both engines, with a dynamic square falling over a static floor, and explain the difference between the scripts.

Box2D

This is the script:

and this is the result:

Here we go with next engine:

Nape

This is the script:

and this is the result

Now, let’s see the differences:

World creation

Box2D: everything is managed by a b2World object, which wants the gravity in the constructor.

Nape: the world is a Space object, which wants the gravity in the constructor:

Vec2 is basically the same as b2Vec2. Also notice gravity is defined using different scales, I personally prefer Box2D approach as it’s intended as real world gravity.

Debug Draw

Box2D: debug draw in Box2D allows great customization, including what to display (shapes, joints, and so on), scale and fill alpha, but you’ll have to write some lines of code:

Nape: debug draw in Nape is easier to setup but it does not allow customization. Just declare a variable defining the viewport and the main color:

and then you are ready to add debug draw to Display List:

I’m personally finding Box2D debug draw more elegant, thanks to filled shapes.

Adding a simple box

Box2D: even adding a box can be quite a task for beginners, you have to create a body definition, then shape, then a fixture to attach the shape to the body.

Not the most intuitive thing, but it allows a lot of customization. Also, Box2D works in meters and you will have to convert it to pixels.

Nape: basic shapes are really easy to crate with Nape you just need to define a body and a shapes.

And as you can see it works with pixels.

World update

Updating the world is a critical process as it renders the world in our Flash movie

Box2D: in three lines we make the simulation run and draw:

The core lies in Step method which runs in this case the simulation at 1/30 seconds at time.

Nape: it’s basically the same, step method handles the simulation.

And that’s all at the moment. As you can see, there isn’t that much difference between these two engines when dealing with basic projects. During next days I’ll show you something more complicated.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (21 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 13 comments

  1. GamyGuru

    on July 30, 2012 at 11:54 pm

    Great Article…. Keep up writing…. :)

    Regards,
    GamyGuru (http://gamyguru.wordpress.com)

  2. sv

    on July 31, 2012 at 7:44 am

    Looking forward to the more complicated examples.

  3. Veeramani

    on August 1, 2012 at 10:54 am

    Which is the best one?…. Emanuele

  4. Veeramani

    on August 1, 2012 at 11:00 am

    How to download the NAPE AS Folder emanuele

  5. Julian

    on August 1, 2012 at 7:45 pm

    @Veeramani: Just switch to Haxe! It’s awesome. Don’t you bother with adobe ;). Also with Haxe you can still generate swf files if you want to. I haven’t that much experience with nape but as far as I can see you will save some code with nape and it has a few features that box2d doesn’t have.

    In future projects we will also try to use nape. Looks like a great phy engine with great additions compared to box2d. Thanks for the tut!

  6. Bengan

    on August 2, 2012 at 9:12 am

    Box2dweb vs flash, which is faster ?
    The game at http://pixsansar.com/box2dweb-jumping-and-puzzle-ball-menu is very cool and tough which is built using box2dweb.

  7. Luca Deltodesco

    on August 4, 2012 at 3:19 pm

    The nape debug drawers do permit customisation: there are flags that can be set to enable/disable the drawing of different things and you can define your own colouring by setting the colour dynamic method, or set the transform object to scale/translate/rotate the debug view operations.

    You cannot ask it to draw shapes with fills but there’s no reason I cannot add that option if people want it, it was not included because the nape demos tend to have too many objects so that drawing fills (or even using flash api in the first place instead of the BitmapDebug drawer) becaomes prohibitively slow.

    Also the colour argument is not the main colour to draw with. it is a background colour (which in the case of shape debug is only used when it comes to deciding what colour to tint objects with when they go to sleep which is why in your image they have turned green; this is to avoid the use of alpha which again is slower. Had the background colour been set to black, then when the object goes to sleep they would have been darkened instead; for instance the constraints demo http://deltaluca.me.uk/docnew/swf/Constraints.html the background colour is set as 0×333333 and the objects that go to sleep are tinted so as to blend into the background)

  8. fred

    on August 4, 2012 at 3:36 pm

    Nape is about a zillion times faster than box2d.

  9. tle

    on August 7, 2012 at 3:00 am

    I have ever used Nape before, and I like Nape more than Box2d.

  10. Maras

    on August 22, 2012 at 8:22 pm

    I’m using only Box2D but some Nape performance demos are really impressive!

  11. Selim Abidin

    on October 18, 2012 at 5:28 pm

    i am one of fans of Box2D physics lib. I’ve never used Nape Physics Engine but it is obvious the fastest Nape and it is seems easier compored Box2D. Tests i have examined a few comparison tests between Nape and Box2D Alc. Nape was faster compored Box2D alc. Maybe when we dig more between two engine, we can see good and bad sides better But after Adobe PF, Nape deserves to be more popular. I suppose i will continue with Nape.

    http://deltaluca.me.uk/forum/index.php/t/385/d0988a65879a4912b6bcb7cd892323bf/

  12. ADev

    on February 23, 2013 at 9:02 pm

    I saw a great box2d/nape side-by-side demo where you can click the mouse to create creates that fall down to the floor and stack up. It had a black background.

    Anyone see that demo? I’d love to check it out again.

    Thanks!

  13. Using the Nape v2.0+ Physics Engine in ActionScript 3 | Jean-Philippe Côté

    on April 5, 2013 at 4:58 am

    [...] I highly recommend you try it out. For a more detailed comparison between the two, check out this comparison by Emanuele [...]