Game Development Reference
In-Depth Information
Figure 13-5. Correct type for Object C .m files that import Box2D
Now, you can actually use the Box2D classes in your project. I must warn future readers, however,
that this process was cobbled together from various Googled sources that describe this process for
older versions of both Xcode and Box2D. I assure you this setup will change in future versions of
Xcode.
Now that you know a little about Box2D, and how to get Box2D source code working in our project,
let's take a look at how we can extend our GameController class to take advantage of physics in
our games.
Understanding the Example
In this example, we have a bunch of different-size asteroids in our scene that bounce off each other
in a physical way. From the previous description of Box2D, we know that we need to define our
world. Let's jump right in and see how we do that in the class PhysicsViewController, as shown in
Listing 13-1.
Listing 13-1. createPhysicsWorld (PhysicsViewController.m)
- (void)createPhysicsWorld {
b2Vec2 gravity;
gravity.Set(0.0f, 0.0f);
world = new b2World(gravity);
world->SetAllowSleeping(false);
world->SetContinuousPhysics(true);
}
In Listing 13-1, we start by creating a b2Vec2 named gravity and set its force to 0.0f in both the X
and Y directions. The struct b2Vec2 is used by Box2D for all vector data types; this includes forces,
velocity, and so on. Using gravity, we create a new b2World object and store it at the variable
world. World is defined in the header (not shown) for PhysicsViewController because we need
to keep a reference to this object for the lifetime of our simulation. The last two method calls in
world— SetAllowSleeping and SetContinuousPhysics —are just there for demonstration and not
 
Search WWH ::




Custom Search