Box2d meters, pixels, scaling and everything in between

Hi everyone,
I'm moving to Box2d from Chipmunk and I'm having trouble understanding the whole "meters, not pixels" thing.
To complicate it (a bit) further, I'm not using opengl but Indielib, a game engine that uses DirectX.
I've implemented the DebugDraw class successfuly, but I do not understand how the testbed implements the "zoom" functionality.

Let's say I have a box 1mx1m, and I want to render it on the screen where 1m=100 pixels. How do I go about doing that? I see that the testbed has a ConvertScreenToWorld method - how does that come into play?
The code is not well documented and it's hard for someone with no openGl experience to follow through...

I've also read somewhere that the easiest thing to do is set a global transform with your scaling ratio. That makes sense if you're just drawing lines and boxes, but it wouldn't work if I'm rendering sprites (everything will be stretched horribly).

So I guess I need a primer on how to deal with meters->pixels conversion, and also where that should happen and how I can make it work for sprites as well as primitives.

Thanks!
This is very simple. Every time you take a box2d unit, and wish to draw it, scale by the scaling factor. And visa versa.

So if Box2D says a body is at (1,1), that'd be in meters, and would correspond to (100,00) in pixels, which is what you'd use for drawing. If you have a sprite you wish to draw at width 150 pixels, the body width would have to be 1.5 to correspond, right?

As almost all robust drawing systems allow for arbitrary scaling (e.g. view transforms in openGL), it usually easiest to use this. It won't result in blurry sprites, because you are ultimately drawing them at the correct size.
Thanks, that does help, but i'm still not sure I get it.
If I understand correctly, each coordinate should be scaled by the factor decided upon, so position 1,1 is in fact 100,100 in our pixel representation.
That is achieved by setting a scaling factor using view transforms? can you point me to the testbed code where that happens? also, you say "It won't result in blurry sprites, because you are ultimately drawing them at the correct size" and I'm afraid I still can't wrap my head around this. If i'm scaling everything by a magnitude of a hundred, how will that not affect sprites?

Sorry for being so dense, but this whole meters thing is so cumbersome for me... 🙂
umm.. anyone?
sorry for bumping, I'd really appreciate any help on this...
The easiest way is to have a variable called scale, so something like "f32 scale = 100.0", now when you draw the sprite, you will draw it at position * scale.
When setting the sprite's initial position you will have, for example: position.x = screenCenter.x / scale etc.
(I've noticed 80.0 and lower are popular values)

Funnily enough I didn't want to convert from meters to pixels at the time. But we coders have to adapt to what we're not used to, sooner or later.
ehudros wrote:
If i'm scaling everything by a magnitude of a hundred, how will that not affect sprites?


You don't generally scale the sprites. Or at least you wouldn't if they're "old-style pixels" that don't scale well. Hand-drawn sprites should be the size (in pixels) you want them to be on screen. Thus you scale the vertexes of the sprite, in order to draw it in the proper location, but not the sprite itself.

Bill
Thanks everyone, you helped me sort it all out.

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