Game Development Reference
In-Depth Information
groundBox.Set(b2Vec2(0, 0), b2Vec2(boxWidth, 0));
groundBody-> CreateFixture(&groundBox, density);
// top
groundBox.Set(b2Vec2(0, boxHeight), b2Vec2(boxWidth, boxHeight));
groundBody-> CreateFixture(&groundBox, density);
// left
groundBox.Set(b2Vec2(0, boxHeight), b2Vec2(0, 0));
groundBody-> CreateFixture(&groundBox, density);
// right
groundBox.Set(b2Vec2(boxWidth, boxHeight), b2Vec2(boxWidth, 0));
groundBody-> CreateFixture(&groundBox, density);
Remember that Box2D is written in C++. To instantiate one of Box2D's classes, you
have to add the new keyword in front of the class's name. If Box2D were written in
Objective-C, the equivalent line might look like this:
world = [[b2World alloc] initWithGravity:gravity allowSleep:allowBodiesToSleep];
In other words, the new keyword in C++ is equivalent to sending the alloc message
to an Objective-C class followed by an init message. That of course means you also
have to deallocate the Box2D world, as seen in Listing 12-2 . In C++ you do this using
the delete keyword:
-(void) dealloc
delete world;
world = NULL;
delete debugDraw;
debugDraw = NULL;
The Box2D world in Listing 12-3 is initialized with an initial gravity vector and allows
bodies to fall asleep.
Sleeping bodies? It's a trick that allows the physics simulation to quickly skip over ob-
jects that don't need processing. A dynamic body goes to sleep when the forces applied
to it have been below a threshold for a certain amount of time. In other words, if the
dynamic body is moving and rotating very slowly or not at all, the physics engine will
Search Nedrilad ::

Custom Search