World Coordinates to Screen Coordinates

Hello this is my first post and let me say I think Box2D is awesome (even though I don't know how to use it yet lol)

This is another Pixel To Meter related question.
For what I have understand you have to scale pixel to meters to match your visual representation. lets say 1 meter equals 50 pixels
according to this http://box2d.org/2011/12/pixels/.

my problem is the actual 'screen position' of the objects. not the scale
If I have a box that is 2x2mts in size (100x100 pixels). at (0,4) in box2D World how do I translate that to x,y position of my screen... considering
that Y axis is fliped?

I've seen A library called PBox2d that uses a PApplet.map function to map coordinates from world space to screen space it's a wrapper for the java port of box2d... and since I'm not an OpenGL guy I dont quite understand many functions from the testbed. I'm trying to get Box2D to work using D3DXSprite an directx enviroment.

thanks in advance for any help
Some people (like myself) flip the y-axis so positive y is downwards so it's easy to translate to screen coordinates. Then it's just a matter of offsetting the drawings from Box2D coordinates to the screen. This means that downwards gravity (if any) is positive and everything would be in a left-handed coordinate system (increasing angle is clockwise). This doesn't affect physics in anyway and should be the same if the y-axis was not flipped.

If you want to keep positive y upwards and still draw on the screen, you just have to use the negative value of the object's y-coordinate in Box2D. You still have to offset the drawings from Box2D coordinates to screen coordinates. For example, if you want the (0,0) Box2D coordinates to be in the middle of the screen (400,300) and draw your 2x2m box at (0,4) Box2D coordinates, it will be something like this (pseudocode):
Code:
meterToPixel = 50.0; //50 pixels to a meter
offsetX = 8.0; //x offset in meters (400/50 = 8). This will put the 0 x-coordinate in the middle of the screen horizontally.
offsetY = 6.0; //y offset in meters (300/50 = 6). This will put the 0 y-coordinate in the middle of the screen vertically.
drawPositionX = (box.x + offsetX) * meterToPixel; //( (0m) +  8.0m )* 50 = 400 pixels
drawPositionY = (-box.y + offsetY) * meterToPixel; //( -(4m) + 6.0m ) * 50 = 100 pixels

And then use drawPositionX and drawPositionY as your screen coordinates for drawing.

I hope this helps.
wow! that was extremely helpful! thanks a lot.
I wrote a small application to do this calculations for me but now I'm having troubles to do the inverse process.

Edit: Inverse process worked out. here is the formula. I't seems to work just fine although I'm not quite sure yet.
please take a look.
Code:
//Screen to World
//draw x:400 y:100 w:100 h:100
Box2d(0,0) = Fw(400,300)
meterToPixel = 50; //50 pixels to a meter
offsetX = 400; //x offset in Pixels centerX
offsetY = 300; //y offset in Pixels centerY
box.x = (drawPositionX - offsetX) / meterToPixel; //( (400px) -  400px )/ 50 = 0 mts
box.y = (-drawPositionY + offsetY) / meterToPixel; //( -(100px) + 300px ) / 50 = 4 mts


thanks a lot!
This post might help someone using box2d for HTML5 canvas. It covers coordinate system in detail and how to place objects accordingly.

Box2d coordinate system for HTML5 Canvas

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