Playing with Box2DFlashAS3

Some time ago I told you to watch Box2DFlashAS3 physics engine.

I dont' know if you did it, but I did. While the original Box2D project come to 2.0.0 version, the AS3 porting is "still" at 1.4.3, but soon it will be upgraded.

So I think it's absolutely time to play with this library.

While I won't be able to provide a full tutorial before some days, I made an interesting prototype in only 58 lines, brackets included.

ACTIONSCRIPT:
  1. package {
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.     import Box2D.Dynamics.*;
  5.     import Box2D.Collision.*;
  6.     import Box2D.Collision.Shapes.*;
  7.     import Box2D.Common.Math.*;
  8.     public class theclass extends Sprite {
  9.         var body_def:b2BodyDef;
  10.         var shape_def:b2BoxDef;
  11.         public var the_world:b2World;
  12.         public function theclass() {
  13.             var container:b2AABB = new b2AABB();
  14.             container.minVertex.Set(0.0, 0.0);
  15.             container.maxVertex.Set(500.0, 400.0);
  16.             the_world = new b2World(container, new b2Vec2(0, 300.0), true);
  17.             body_def = new b2BodyDef();
  18.             shape_def = new b2BoxDef();
  19.             shape_def.extents.Set(250,10);
  20.             shape_def.friction = 1;
  21.             body_def.position.Set(250,390);
  22.             body_def.AddShape(shape_def);
  23.             body_def.userData = new floor();
  24.             body_def.userData.width = 500;
  25.             body_def.userData.height = 20;
  26.             addChild(body_def.userData);
  27.             the_world.CreateBody(body_def);
  28.             addEventListener(Event.ENTER_FRAME, render);
  29.             addEventListener(MouseEvent.CLICK, mouse_handler);
  30.         }
  31.         private function mouse_handler(event:MouseEvent):void {
  32.             body_def = new b2BodyDef();
  33.             shape_def = new b2BoxDef();
  34.             shape_def.extents.Set(Math.floor(Math.random()*15)+15,Math.floor(Math.random()*15)+15);
  35.             shape_def.friction = 1;
  36.             shape_def.density = 1;
  37.             shape_def.restitution = 0.2;
  38.             body_def.position.Set(Math.random()*400+50,0);
  39.             body_def.AddShape(shape_def);
  40.             body_def.userData = new crate();
  41.             body_def.userData.width = shape_def.extents.x * 2;
  42.             body_def.userData.height = shape_def.extents.y * 2;
  43.             addChild(body_def.userData);
  44.             body_def.userData.gotoAndStop(Math.floor(Math.random()*6)+1);
  45.             the_world.CreateBody(body_def);
  46.         }
  47.         public function render(e:Event):void {
  48.             the_world.Step(1/30, 10);
  49.             for (var x:b2Body = the_world.m_bodyList; x; x = x.m_next) {
  50.                 if (x.m_userData is Sprite) {
  51.                     x.m_userData.x = x.m_position.x;
  52.                     x.m_userData.y = x.m_position.y;
  53.                     x.m_userData.rotation = x.m_rotation * 57.2957795;
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }

And the result is really interesting, and virtually impossible to achieve without the library... just click on the floor and see what happens...

I think it's time to start learning AS3 with this interesting library... meanwhile you can download the source code with the last version of Box2DFlashAS3 library.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5 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.

22 Responses to “Playing with Box2DFlashAS3”

  1. Gabriel Bianconi on March 21st, 2008 8:08 pm

    Wow… Very cool… =D

  2. Kesh on March 21st, 2008 8:16 pm

    Hey emanuele, you just earned 20 bucks from me because i just got 100 dollars… i think u were my referrer. i hope…

  3. EagleVision on March 21st, 2008 9:09 pm

    Nice…Would you know how to change it to AS2?

  4. Emanuele Feronato on March 21st, 2008 9:16 pm

    It’s not possible AFAIK

  5. Mr.doob on March 21st, 2008 10:28 pm

    I guess we all do the same thing as the first test of the engine :)

  6. Orava on March 22nd, 2008 8:05 am

    how do I open that .fla? it says “unexpected file format.” :O

  7. Grifo on March 22nd, 2008 7:57 pm

    You gotta use flash CS3, Orava. That message means you’re using older version.

  8. Sodaplayer on March 23rd, 2008 2:08 am

    Nice. This will help me prevent collisions in my game.

  9. brainclog on March 23rd, 2008 2:53 am

    Box2D is awesome! I just finished making my first Flash game with it, which you can find if you click my name. While theoretically you could port the engine to AS2, it would be pointless because it would run like 10% as fast. Just embrace AS3 and enjoy the speed!

  10. FrozenHaddock on March 23rd, 2008 3:32 am

    I think it was ported to AS2, but alongside every other AS2 physics engine, it has little to no documentation with a steep ass learning curve for newcomers to using libraries.

    Ie, me.

  11. brainclog on March 23rd, 2008 3:51 am

    I believe you are incorrect about it being ported to AS2, if you can show me a link I will happily admit that I’m wrong :) No documentation? You’re kidding right?

    http://box2d.org/manual.html

    Most people are just too lazy to read it. It IS slightly more complicated than APE for instance, but it has many more features and is faster!

  12. Mr. Greedy on March 23rd, 2008 4:07 pm

    What a great idea! Putting advertisements in your source code! I wish more people would nest ads in their “open source” source. After all, it’s about the money…isn’t it?

  13. Emanuele Feronato on March 23rd, 2008 10:56 pm

    lol it’s not my fault, it’s just that kontera put a link in the source.

  14. Graham on April 2nd, 2008 12:18 am

    How do I put Box2D into my classpath? I don’t know where to put it.

  15. Shaun on April 4th, 2008 6:56 pm

    AS3 is awesome. this is so amazing

  16. nikhil on June 29th, 2008 8:03 pm

    one word to explain this”AWESOME”
    would love to learn hw to use this engine

  17. rexus on July 22nd, 2008 8:52 pm

    Damn, it’s totally cool i love it

  18. Gecko on August 4th, 2008 5:54 pm

    You wouldn’t be able to work out how to use flade would you?

    That’s
    http://www.cove.org/flade/

    I love physics but I don’t have flash CS3.

  19. holcomb227 on August 5th, 2008 7:04 am

    I need a beginners tutorial for action script 3. I have no idea how to work box 2D with flash CS3. And I don’t know action script at all so basically i need to know how classes are inputted into a flash file.

Leave a Reply




Trackbacks

  1. Create a Flash ball game using AS3 : Emanuele Feronato - italian geek and PROgrammer on March 25th, 2008 9:38 am

    [...] Actionscript 3. Even if AS2 is way to be dead, is becoming a bit obsolete… every new library like Box2DFlashAS3 only works in AS3, so sooner or later we must learn [...]

  2. Create a Flash game like Totem Destroyer : Emanuele Feronato - italian geek and PROgrammer on July 18th, 2008 9:33 pm

    [...] already talked about it in the post Playing with Box2DFlashAS3, now it’s time to make something more [...]

  3. Adobe Flash CS3- AS3 and AS2 game tutorials roundup | Lemlinh.com on August 19th, 2008 6:30 pm

    [...] already talked about it in the post Playing with Box2DFlashAS3, now it’s time to make something more [...]