iDevGames Forums
OpenGL box2d scale factor - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: OpenGL box2d scale factor (/thread-1452.html)



OpenGL box2d scale factor - lifebythedrop - Apr 15, 2009 07:31 AM

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);
glLoadIdentity();
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.


OpenGL box2d scale factor - ThemsAllTook - Apr 15, 2009 08:13 AM

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.


OpenGL box2d scale factor - lifebythedrop - Apr 15, 2009 10:24 AM

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?title=FAQ#How_do_I_convert_pixels_to_meters_units

Thanks,
Chris


OpenGL box2d scale factor - lifebythedrop - Apr 15, 2009 06:02 PM

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!


OpenGL box2d scale factor - mpcarr - Nov 15, 2009 06:39 AM

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