Game Development Reference
In-Depth Information
It's used to define that 32 pixels on the screen equal 1 meter in Box2D. A box-shaped
body that's 32 pixels wide and high will be 1 meter wide and high. A body that's 4×4
pixels in size will be 0.125×0.125 meters in Box2D, whereas a relatively huge object of
256×256 pixels will be 8×8 meters in Box2D and still well within acceptable Box2D
dimensions. The PTM_RATIO lets you scale the size of Box2D objects down to the di-
mensions within which Box2D works best, and a PTM_RATIO of 32 is a good com-
promise for a screen area that may be as large as 1024×768 points on the iPad.
Converting Points
Note that the b2Vec2 struct is different from CGPoint , which means you can't
use a CGPoint where a b2Vec2 is required, and vice versa. In addition, Box2D
points need to be converted to meters and back to pixels. To avoid making any mis-
takes, such as forgetting to convert from or to meters or simply making a typo and us-
ing the x coordinate twice, wrapping this repetitive code into convenience methods like
these is highly recommended:
-(b2Vec2) toMeters:(CGPoint)point
{
return b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO);
}
-(CGPoint) toPixels:(b2Vec2)vec
{
return ccpMult(CGPointMake(vec.x, vec.y), PTM_RATIO);
}
This allows you to write the following code to easily convert between CGPoint and
pixels to b2Vec2 and meters:
// Box2D coordinates to Cocos2D point coordinates
b2Vec2 vec = b2Vec2(200, 200);
CGPoint pointFromVec = [self toPixels:vec];
// Cocos2D point coordinates to Box2D coordinates
CGPoint point = CGPointMake(100, 100);
b2Vec2 vecFromPoint = [self toMeters:point];
Search Nedrilad ::




Custom Search