Sign in to follow this  
wicked357

Box2D Pixel to Meter conversion problem

Recommended Posts

I am working on learning Box2D but since it works in meters and centers of mass, it is a bit confusing to say the least especially when it comes to placing objects with SFML graphics, although I can get it to work, it takes a lot of trial and error figuring things out. So when you create a world,
//Create the size of the world
b2AABB worldAABB;

//Upper left corner of the world
worldAABB.lowerBound.Set(-3000, -3000);

//Lower right corner of the world
worldAABB.upperBound.Set( 3000,  3000);

Now I know, I know it is confusing lowerBound is actually the upper left corner and the upperBound is the lower right corner for specifying the world size. Now I was thinking what is the conversion rate between pixels = 1 meter, I read somewhere it was 30 pixels to 1 Meter in Box2D so if that is the case I could do something like,
//Variables for Pixel to Meter ratio
const int RATIO = 30;

So in theory I should be able to divide the -3000 and 3000 by RATIO, to looks like this.
//Upper left corner of the world
worldAABB.lowerBound.Set(-3000 / RATIO, -3000 / RATIO);

//Lower right corner of the world
worldAABB.upperBound.Set( 3000 / RATIO,  3000 / RATIO);

So my question, would that world properly for being able to draw in so if I wanted to draw a shape at X and Y pixels like (10, 10) I could do this to figure out the world position in Box2D (10 / RATIO, 10 / RATIO) is this correct? Or do I do (10 * RATIO, 10 * RATIO)? Would either of these work out how I am explaining?

Share this post


Link to post
Share on other sites
Never mind I got it all figured out, I have the graphics drawing how they should for SFML and reacting properly to the Box2D physics, I also figured out the point of using RATIO and changing the number down and how Box2D specifically uses the center of objects, I noticed a few things that went together every time I had to change something in the shape's center you would have the to change the box2D polyDef.SetAsBox(SFML shapes centerX / RATIO, SFML shapes centerY / RATIO). When I get it all together properly and clean up the code a bit I will post it to my projects for full source download for example review. Make sure you check back from time to time to see what I got new coming or completed. Check the sig for the site link.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this