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

Quote:
i've managed to make a new world and adding new movieclips to it but every time i run it everything falls, the objects have all the physic properties but i can't figure out how to make the world to stay where i put it


You probably want to set the "isStatic" property to true for your objects that you want to stay put.

Quote:
, also can someone explain to me how to add functional joints?


Drag a symbol that extends "Joint" (like DemoJoint) onto the stage, and position it on top of whatever object you want to anchor to. The Joint class uses getObjectsUnderPoint to automatically anchor its self to a body. Then select the type of joint you'd like to make from the "type" drop down in the properties panel.

If you pick "RevolutePin", then your joint MC should be over one or more Shape / Body MCs. If it's over two, a revolute joint will be created between those bodies. If it's over one, that body will be connected to the world ground.

If you select "Prismatic", "Revolute", or "Distance", drag another Joint instance onto the stage on top of a different body. Like the first "Joint" instance, this will automatically anchor itself to whatever body it's on top of. DON'T select a "type" for this joint. Instead, give it a name ("instance name"), and then specify that name in the "target" property of the first Joint instance. The first Joint will look for the second one by it's name and connect them together.

If you select "Path"... forget about it. I've come up with another way to do this that's 10x easier.

Some times Joints can't find the body they're supposed to anchor to. If this is the case, you can explicitly set the body to anchor to via the "bodyName" property (this is the "instance name" of the body).

All of the other Joint properties translate pretty much directly to Box2d JointDef vars (enableMotor, enableLimit, etc.).

Quote:
If everything is defined in the authoring environment (properties panel?), how do you change things like the graivity vector (and radial on/off)


World instances have gravityX, gravityY, and gravityRadial properties.

Quote:
, as well as setting body.m_userData properties and static vs dynamic body creation?


The userdata is ALWAYS the movieclip that corresponds to the particular body / shape. The idea is that you extend "Shape" to create stuff with custom behaviors. You can always use the "as" operator to call your custom code. For example:

Code:
var mc = someB2Shape.m_userData as myCustomClassThatExtendsShape
if(mc) {
    mc.myCustomAction();
}
BorisTheBrave wrote:
How do people feel about putting up information on Box2D addons and aids such as this on the wiki?

That sounds perfect. I'm a bit of a wiki noob so I'm going to have to give it another shot later (how do I add a page?).

I was actually "testing the waters" to see if there was enough interest in this to bother polishing it / writing proper documentation. Glad people are finding it useful!
I for one am finding your code extremely helpful - I'm still new to coding and it has really broken down some brick walls for me. I have a basic sports game up and running now with an IK ragdoll, a bat and a ball. I can't thank you enough.

I hope you have time to answer a really basic question: I am having trouble figuring out how to reference individual bodies in my code, but I can't figure out what names they're being given by the code.

In Cursor.as, you assign the target of the mouseDown listener as body2 in the mouse joint. I want to make the mouse joint bind to a particular object (the bat) without having to click it. But I don't know how to name it - if I trace the output of the mouseDown listener in Cursor.as, I just get "[Object Bat]" in the output window which can't be passed to functions. "Bat" is the component name.

I'd appreciate any advice.
mayobutter wrote:
pault107 wrote:
I'm getting a few run time errors when attempting to use some joint types, but I'm guessing that due to my lack of joint knowledge.

... or misuse of the component properties (didn't document them too well), or a genuine bug. What errors are you getting?


Hey Mayo

My apologies for not getting back to you with my run time error details - I was too busy having fun with non-jointed objects.

Your post above answers some of the questions I had, and like you suggested, I'm guessing it was misuse of the properties panel on my part that were causing the errors. I will have another bash at creating my own jointed objects and get back to you.

Like I said earlier, a wiki would be great and as I gain more experience with your tool, I will try to participate.
Quote:
I hope you have time to answer a really basic question: I am having trouble figuring out how to reference individual bodies in my code, but I can't figure out what names they're being given by the code.


Ok, I assume that Bat extends Shape. To "name" your bat, give it an instance name in the properties inspector ("bat" is probably an appropriate name). Then you'll be able to reference the b2Body of your bat like so:

Code:
(world.getChildByName('bat') as Bat).body.b2body


Of course, your world and bat will have to be initialized first.
Works. Genius! Thanks!
This is probably an abuse of your generosity, but anyway:

I can add new shapes to the stage using actionscript when I'm using regular box2dflash. But I can't figure out the correct way to add your components to the stage using actionscript.

What is the code you would use to add a simple DemoBox (from your example .fla) to the stage?
Bennett wrote:
What is the code you would use to add a simple DemoBox (from your example .fla) to the stage?


Code:
var b:DemoBox = new DemoBox();
world.addChild(b);
b.create();


Depending on where you're doing this in code, I'm not sure if the "world" variable will be available, but that should put you in the right direction. You'll probably want to at least set the x, y and rotation properties of "b" before calling create().
I'v been playing with joints today and they work a charm - the runtime errors I mentioned earlier were caused by my misuse of the panel properties, like you predicted.

This is brilliant stuff.

Any plans to add over joint types? Pulleys?
Thanks again! It's all working beautifully now. It performs fine too, even on a mac (which is saying a lot, given the state of the flash player on OSX)

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