Game Development Reference
In-Depth Information
strictly required. Feel free to change these values and see if you tell any difference in the simulation.
For an example this simple, they seemed to have no effect I could discern, though I am sure they
are critical to making a complex scene efficient enough to play. Let's now look at how we define our
main view controller to make use of this new Box2D world.
Extending GameController
Let's look at how we start to use this Box2D world created in
createPhysicsWorld
by seeing where it
is called. We call
createPhysicsWorld
from the task
doSetup
of the class
PhysicsViewController
, as
shown in Listing 13-2.
if ([super doSetup]){
[self setGameAreaSize:CGSizeMake(1024, 768)];
[self createPhysicsWorld];
for (int i=0;i<30;i++){
float startY = rand()%(int)self.gameAreaSize.height;
float startX = rand()%(int)self.gameAreaSize.width;
CGPoint center = CGPointMake(startX, startY);
int level = 1 + rand()%4;
Asteroid* asteroid = [Asteroid asteroidOfLevel:level At: center];
[self addActor: asteroid];
}
return YES;
}
return NO;
}
In Listing 13-2, we specify the game area size and create our physics world. The game area size
is not considered by Box2D. Box2D does not contain the idea of a fixed boundary for the game to
happen within, except for limits of a float value to represent coordinates within the world. Next, we
create 30 Asteroids and position them randomly in the scene. Each Asteroid is then added with the
familiar
addActor:
method.
You may notice at this point that we have not really done anything with Box2D except create the
world object. We appear to be adding actors to a
GameController
, just as we did in previous
chapters. The reason it looks this way is that we have done two things to enable physics, while
not upsetting our framework too much. We have overridden a few of
GameController's
tasks in
PhysicsViewController
and have created a new actor type called
PhysicsActor
, which Asteroid now
extends. These two modifications work together to enable physics. Let's start with modifications to
GameController
and understand how this all fits together.