2

Could anyone tell me about the relation between the dimensions of a body and number of pixels(in box2d). How many pixels contributes to one meter length? To be more specific, I'm using the below line of code in my program

polygon_body.SetAsBox(1, 1);

In this code what are the parameters of SetASBox. I mean whether it is a pixel value or some other units.

jjpp
  • 1,298
  • 1
  • 15
  • 31

1 Answers1

0

one meter is about 32 pixels.

without looking at the manual, my guess is that since SetAsBox() is a box2d function, it takes parameters in meters. So if you have pixel values, you could just use this definition to easily convert to meters:

#define PTM_RATIO 32

including this in your files allows you to forget the ratio and just use this definition whenever you need to convert between pixels and meters. when you actually do the conversion, you can just divide or multiply by this ratio (32/1) depending on the context. Eg:

CGSize winSize = [[CCDirector sharedDirector] winSize];
b2Body *body = world->GetBodyList();

if(body->GetPosition().x*PTM_RATIO > winSize.width/2){
    // do stuff....
}

This conditional wouldn't work if you compared meters (GetPosition) to pixels (winSize), so this is a great example of where this conversion is useful.

Or, in your case:

float pixel_width, pixel_height;
polygon_body.SetAsBox(pixel_width/PTM_RATIO, pixel_height/PTM_RATIO);
Emmett Butler
  • 5,969
  • 2
  • 29
  • 47