Drag 'n' Drop MovieClips / Box2D Bodies How To (*UPDATED*)

Has anyone ported this to Version 2? I guess v2 for AS3 just came out, so it might be wishful thinking. 🙂

I took a stab, but I'm relatively new to Box2D in general, and am still getting familiar with the previous version. 😛
it didn't seem to like it when I over did the squares:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Box2D.Collision::b2PairManager/::AddPair()
at Box2D.Collision::b2PairManager/AddBufferedPair()
at Box2D.Collision::b2BroadPhase/CreateProxy()
at Box2D.Collision.Shapes::b2PolyShape$iinit()
at Box2D.Collision.Shapes::b2Shape$/Create()
at Box2D.Dynamics::b2Body$iinit()
at Box2D.Dynamics::b2World/CreateBody()
at Body/initialize()
at World/onActivate()


Any ideas why?
Yep. There's a "max pairs" variable in the settings file (b2settings.as) that you'll have to increase. Can't remember the exact variable name. I run into this every once in a while. Us ActionScript folk are used to arrays of dynamic length, but since Box2d comes from C, a lot of the array / linked list variables have a fixed length.
Has any more work been done on Mayo's excellent tool?
Yes, I've got an up-to-date version. Even managed to get drag 'n' drop joints working and paths that bodies can move along - all definable from within Flash. It's got some bugs that I've been putting off fixing though. I'll see if I can clean it up and upload sometime over the weekend.
That's great news Mayo. Thanks.

Does the latest version work with v2 or only v1.4?
Hey, I hadn't seen this post before. I've already made my own version of something very like this. It works fairly well. I hadn't thought to use scaleX and scaleY to determine the actual width and height of the movieclips; I use a complicated bit of code:
Code:
      function findExtents(){
         var box = this.getChildAt(0);
         var pt1:Point = new Point(box.getChildByName("tLeft").x,box.getChildByName("tLeft").y); //Location of the top left corner
         var pt2:Point = new Point(box.getChildByName("tRight").x,box.getChildByName("tRight").y); //Loc of top right corner
         var pt3:Point = new Point(box.getChildByName("bRight").x,box.getChildByName("bRight").y); //Loc of bottom right corner
         pt1 = box.localToGlobal(pt1); //Global coord transform
         pt2 = box.localToGlobal(pt2);
         pt3 = box.localToGlobal(pt3);
         wid = dist(pt1,pt2); //Find the transformed width/height
         hght = dist(pt2,pt3);
      }


I have two classes, CollisionEngine and CollisionField. CollisionField just holds all the objects and initializes them like this:
Code:
      public function CollisionField(){
         for(var i=0;i<numChildren;i++){
            child = getChildAt(i)
            if(child is VisObject){ //All the objects extend VisObject; some children are joints and other things that don't, are handled separately, and are put in different arrays.
               platforms.push(child);
            }
         }


CollisionEngine loops through the platforms array:
Code:
         for (var j in platforms ){
            bodyDef = new b2BodyDef();
            bodyDef.position.x = platforms[j].x*scale;
            bodyDef.position.y = platforms[j].y*scale;
            bodyDef.angularDamping = 0.01;
            bodyDef.AddShape(platforms[j].collideObj);
            bodyDef.rotation = platforms[j].rotation*3.14159/180;
            bodyDef.userData = platforms[j];
            platforms[j].collideObj = m_world.CreateBody(bodyDef);
            for(var n in forces){
               if(platforms[j].hitTestObject(forces[n])){//forces[n].pt1.x,forces[n].pt1.y,true)){
                  trace("Force:"+platforms[j]);
                  platforms[j].collideObj.ApplyImpulse(forces[n].calcForce(),new b2Vec2(forces[n].x,forces[n].y))
               }
            }
//            trace(platforms[j]+":"+platforms[j].dispObj);
         }

Each object just creates a shape, not a bodydef, so that's done separately. I probably ought to put that into the VisObject class, to be honest. Overall I like your solution a lot better, I'll try and fiddle around with mine and rearrange it a bit. I still like having a separate "engine" class, though, so I can load in different CollisionFields (at least in theory) while having all the same logic running over it. I hadn't thought of using components to add editable properties like you have, that's really cool. I was wondering how I would be able to set properties of individual objects; my only idea was to use the movieclips' names to store info and then parse it in code. I would be rather ugly, and I never bothered.
Hey Mayo, have you made any progress on your excellent tool? I'm itching to play with a version that works with Box2D v2.
Yeah... I gotta stop adding stuff and just post it already. Haven't forgotten though - will post soon.
I've made a ton of improvements to this little project and gave it a name:

Box2d Flash World Construction Kit

You can view the demos / download from here:

http://www.sideroller.com/wck/

All of the demos (shapes, bodies, joints, gravity, etc.) were created in Flash without writing any code.

Some new features:

    Updated for 2.1
    Create joints from within Flash: Distance, Revolute, Prismatic & RevolutePin.
    Create paths for bodies to move along.
    Make revolute & prismatic joints act as springs.
    User-drawable shapes.
    Polygon decomposition within flash.
    Radial gravity

I've also included a custom ContactListener that dispatches Contact events through Flash's native event dispatching mechanism. This is still a work-in-progress, though. Haven't built a demo that uses it yet.

There's still some stuff that could be cleaned up a bit, it's seriously lacking in documentation, and I just threw the demos together without any thought. Thought I'd post it anyway though to get some feedback.

Original thread: https://box2d.org/forum/viewtopic.php?t=367