Game Development Reference
In-Depth Information
The second and third parameters to the Step method are the number of iterations.
They determine the accuracy of the physics simulation and also the time it takes to cal-
culate the movement of the bodies. It's a trade-off between speed and accuracy. In the
case of position iterations, you can safely err on the side of speed and require only a
single iteration—you don't normally need more position accuracy in games unless you
experience objects not coming to rest, colliding in unnatural ways, or missing collision
entirely. Velocity is more important, however; a good starting point for velocity itera-
tions is eight. More than ten velocity iterations have no discernible effect in games, but
just one to four iterations won't be enough to get a stable simulation. The fewer the ve-
locity iterations, the more bumpily and restlessly the objects will behave. I encourage
you to experiment with these values.
Collision Detection
Box2D has a b2ContactListener class, which you're supposed to subclass if you
want to receive collision callbacks. The following collision detection code is in the
PhysicsBox2D01 project but not the regular cocos2d Box2D project template.
Create a new class in Xcode and name it ContactListener . Then rename the im-
plementation file to so it has the file extension .mm . Then
you can use both C++ and Objective-C code in the same file. Listing 12-6 shows the
ContactListener header file.
Listing 12-6. The ContactListener Class's Interface
#import "Box2D.h"
class ContactListener : public b2ContactListener
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
It's a C++ class, so the class definition is a little different. Note the trailing semicolon
after the last bracket. Forgetting that semicolon is a common error. The Be-
Search Nedrilad ::

Custom Search