Game Development Reference
#define PTM_RATIO ([GB2ShapeCache sharedShapeCache].ptmRatio * 0.5f)
This improved PTM_RATIO macro now retrieves the points-to-meter ratio from the
GB2ShapeCache class. And that's why you have to make sure the shape cache is ini-
tialized before using the PTM_RATIO macro.
The shape cache's ptmRatio is divided by two (multiplied by 0.5f) because the
shapes created in PhysicsEditor were based on the Retina-resolution images, whereas
cocos2d's resolution-independent point coordinates used for positioning nodes always
assume the iPhone's screen resolution to be 480x320 points.
The initPhysics method still performs the initialization of the Box2D physics en-
gine. The Box2D init code is essentially the same as in the previous chapter, with
two exceptions: the static screen boundary shape that keeps dynamic objects inside the
screen area defines no top or bottom shapes, allowing dynamic bodies to fall outside
the screen through the bottom. We'll need that for the ball to be able to roll into the
table's drain. You don't need the top shape because there will be a pinball table shape
blocking the entire upper area of the screen.
In addition to that, the collision parameters for the left and right boundary are set in the
init code, because these shapes aren't defined with the help of PhysicsEditor. The
categoryBits must be set to the Wall bit (collision bit 4 in PhysicsEditor), and the
maskBits must be set to the Ball bit (collision bit 0 in PhysicsEditor). The bit val-
ues are specified in hexadecimal format, indicated by the leading 0x . You'll find that
PhysicsEditor displays those hexadecimal values below the Cat. and Mask columns un-
der Fixture Parameters.
The following is the initBox2dWorld method, with the changes and additions,
compared to the Box2D initialization code from Chapter 12 highlighted:
world = new b2World(gravity);
world- > SetAllowSleeping(true);
world- > SetContinuousPhysics(true);