A newbie needs help conceptualizing Box2D

Disclaimer: I'm actually using Box2DX with C# but there is no forum for that project so I'm posting here and my questions are about Box2D in general.

I have read the (excellent) documentation and still don't understand the following. I realize some of these questions may sound ridiculous but humor me. 🙂

1. Does 1 world pixel = 1 screen pixel? Must I define a scaling factor or is it optional?

2. It's not crystal clear from the Test demo why we need the listeners. I see the comment on MyDestructionListener but even that is not totally clear. Why would a joint be implicitly destroyed? Newbie questions here. 🙂

3. In the Test demo, why is the ground set at position 0.0, -5.0? Wouldn't that make the ground only 5 pixels under the origin?

4. Is the first body added to a world automatically set as the ground body?

5. If a body is added to a world and its position makes it suspended in mid air and no linear velocity is added (via SetLinearVelocity), then will the object automatically fall towards the ground (given gravity exists)?

6. My gravity is set to the following vector: gravity = new Box2DX.Common.Vector2(0.0f, -5.0f);
But the shapes on my screen are "falling" UPWARDS on the screen. Is this possible in the world or is it due to a rendering problem?

7. To translate Windows coordinates (top left is 0,0) to Box2D coordinates (0,0 is at origin), I created this 2 methods that I think should work but they are not.

private float PhysicsXToLocalX(float x)
{
return (float)(this.Width / 2) + x;
}

private float PhysicsYToLocalY(float y)
{
return (float)(this.Height / 2) - y;
}

private float LocalXToPhysicsX(float x)
{
return x - (float)(this.Width / 2);
}

private float LocalYToPhysicsY(float y)
{
return (float)(this.Height / 2) - y;
}
1. No. The manual says "Box2D is optimized for MKS units (Meter / Kilogram / Second). A box 100 pixels wide gets 100 meters so this is unreasonable. Some people suggest 30 pixels for meter as optimal.
2. You may get out without listeners in most cases. Box2D frees joints automatically when the corresponding bodies gets destroyed. You however may want to know when joints are destroyed, that's why listeners are needed.
3. In the demo, the ground is set as a box.
- Boxes position are set at their center, not their upper left corner.
4. No, ground in the demo is just a normal static body (aka without weight). Box2D however creates one shapeless body "ground" to easy creation of joints. It has nothing to do with the ground in the demo.
5. If you set the body's mass, it will fall. If you don't - it will be static and move nowhere.
6. Gravity for falling downwards is right... Something do deal with the rendering. Check it out, or just simply output the body's position in the console to check whether it's Box2D problem.
7. You'd better
- Check out the body's position in the console to check for Box2D problems
- Check your functions if they work properly independently of Box2D.

I hope I was useful and this information is accurate enough... Cheers
Hey, thanks for the answers! Very useful. You were right - I was rendering things incorrectly so my objects were "falling upwards". It's now working and it was very exciting to see the objects falling down and bouncing off the ground (I randomized restitution and it actually worked, let me tell you it was exciting).

Also, the clarification about a world's ground body was also useful. Saved me debugging time and may help others.

The next question I have is about vertices. In my application I have some images that the user might add and then I want those images to be bouncing around in the simulation. So how can I detect the list of vertices/edges of an image programatically? Do I need to learn how to do edge detection? Or is there an easier workaround?

Thanks again
Well, if it is a real image (eg. bitmap) than proper detection of vertices/edges is worth a Nobel. If it's some kind of vector graphics, than it might be easier... It would be best if you can predefine the vertices for all images you have and than just get those for the appropriate image...
These are very useful answers, thanks.

Another "newbie" conceptual question that is extremely important is the following:

- I've created the circular reference so I can update my rendered objects on each world.Step. That works fine. But what if the user wants to drag an object around. I've seen the way the TestBed does it: the mouse is a joint that gets connected to the object it pressed. Is it possible to just set the position of a body as the mouse drags it around or do you have to create the joint? As the mouse moved, I was trying to use BodyDef.Position.Set(newx, newy) but it wasn't getting updated automatically. Is what I'm doing possible, or should I do it a different way (e.g. the joint way, or add torque/motorspeed to an adjacent joint as in the Car example)?

Thanks!
Directly setting the position would happen by setting the b2Body's XForm, not the b2BodyDef's as you're currently trying to do - don't know the specific method call in C#, probably myBody.SetXForm(position, rotation). But this is bad, because it won't let continuous collision detection do its thing and could potentially mess up other stuff as well. If you don't need perfect collisions while dragging bodies, it might be good enough, but it's pretty much an unsupported approach. Use a mouse joint if it's at all possible, maybe editing the max force and playing with the other mouse joint parameters if you need more dragging power.

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