## OpenGL box2d scale factor

Nibbie
Posts: 3
Joined: 2009.04
Post: #1
Hi,

I'm trying to use box2d physics engine in a simple iphone game, but having problems with the scale factor. Since the general approach is to use 30 pixels per meter for most games, that is what i tried to do.

So if I want a 30 pixel per meter scale, should I do this?

float scale = 30.0f;
glMatrixMode(GL_PROJECTION);
glOrthof(0, scale, 0, scale * 1.5f, -1.0f, 1.0f);

This seems to work ok when rendering opengl shapes. But when I add a sprite, the sprite is huge! I have a 30*30px square sprite should map to a 1*1 meter box, but when the sprite renders it is way bigger than the 1 meter box. I know this has something to do with have I've called glOrthof, but not sure what I should do.

I seem to have a fundamental misunderstanding of how this is supposed to work. Any help is greatly appreciated.
Moderator
Posts: 1,563
Joined: 2003.10
Post: #2
What you've set up is a projection which is 30 units wide and 45 units tall. This means that if there are 30 units per meter, a 1-meter-wide object will take up the entire width of your projection. Since you're talking about pixels, it sounds like you simply want to do glOrtho(0, pixelsWide, 0, pixelsHigh, -1.0f, 1.0f);. This way, one unit in your projection corresponds to one pixel on your screen; the ratio of pixels to meters doesn't come into the equation for setting up your projection.
Nibbie
Posts: 3
Joined: 2009.04
Post: #3
Thanks for the quick reply. I'm sorry, I'm still am missing that 'aha'.

Since Box2D wants dynamic bodies between .1 and 10 meters, I create all my dynamic bodies within this optimal 'tuned' range. If I use glOrthof(0, 480, 0, 320, -1, 1);, it seems to have the effect of mapping 1 pixel to 1 box2d unit(meter). The net result, the physics bodies are tiny (between .1 and 10 pixels), but the sprites are now correctly sized.

If I don't use glOrtho to apply a pixel-to-meters unit ratio, what is the correct approach ?

This link describes the scaling factor at the very bottom.
http://www.box2d.org/wiki/index.php?titl...ters_units

Thanks,
Chris
Nibbie
Posts: 3
Joined: 2009.04
Post: #4
Ok, I thought more about your suggestion, and tried experimenting with glScalef instead of trying to use the projection to handle the pixel-meter ratio. And it worked great. I think that has solved the issue.

Thanks!
Nibbie
Posts: 2
Joined: 2009.11
Post: #5
Hi,

I'm in the same boat as you were,

I can setup my glOrthof like so

glOrthof(-240.0f,240.0f,-160.0f,160.0f, -1.0f, 1.0f);

results in textures being the right size and box2d shapes being tiny

glOrthof(-10.0f,10.0f,-15.0f,15.0f, -1.0f, 1.0f);

results in box2d being the right size and textures being huge.

I've tried scaling my textures using

glScalef(sx,sy,1);

but they look rubbish when scaling a 512x512 background image to 32x32 so it fits the box2d world.

Could you post how you solved the problem?

Thanks