OpenGL box2d scale factor

Posts: 3
Joined: 2009.04
Post: #1

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;
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.
Quote this message in a reply
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.
Quote this message in a reply
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.

Quote this message in a reply
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.

Quote this message in a reply
Posts: 2
Joined: 2009.11
Post: #5

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


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?

Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  opengl es strange 16bit texture scale problem spuckfunkel 6 14,467 Aug 14, 2011 10:44 PM
Last Post: headkaze
  How can I build BOX2D as a static lib for the iPhone? riruilo 3 6,402 Oct 8, 2009 06:27 AM
Last Post: Skorche