Creating the clown

Now that we have built the boundary within which we will restrict the clown's movements, and a perfectly bouncy trampoline, it is time to actually create the clown. We will create a class to handle the daredevil personality of our clown and will very intelligently name the class Clown.

Our Clown class will inherit from GameObject, so we need not worry about updating the position of the clown with respect to his body. However, we will define the clown's state machine in this class. For now, let's look at the CreateClown function in the GameWorld.cpp file:

void GameWorld::CreateClown()
{
  // clown will be a dynamic body
  b2BodyDef clown_def;
  clown_def.type = b2_dynamicBody;
  // clown will start off at the centre of the screen
  clown_def.position.Set(SCREEN_TO_WORLD(SCREEN_SIZE.width/2), 
    SCREEN_TO_WORLD(SCREEN_SIZE.height/2.75));
  b2Body* clown_body = world_->CreateBody(&clown_def);

  // create clown, set physics body & add to batch node
  clown_ = Clown::create(this);
  clown_->SetBody(clown_body);
  sprite_batch_node_->addChild(clown_, E_LAYER_CLOWN);
}

We begin by creating the body definition for the clown's body, marking it as dynamic and setting the initial position before using it to fetch a new body from the world. We then create an auto-release Clown object and pass the newly created b2Body into its SetBody function before adding the clown to the game's batch node. We haven't specified anything about the clown's appearance. Well, look no further than the SetBody function that you will find in Clown.cpp:

void Clown::SetBody(b2Body* body)
{
  // create a box shape
  b2PolygonShape clown_shape;
  clown_shape.SetAsBox(SCREEN_TO_WORLD(m_obContentSize.width * 0.5), 
    SCREEN_TO_WORLD(m_obContentSize.height * 0.5f));

  // create a fixture def with the box shape and high restitution
  b2FixtureDef clown_fixture_def;
  clown_fixture_def.shape = &clown_shape;
  clown_fixture_def.restitution = 0.5f;
  body->CreateFixture(&clown_fixture_def);
  // don't let the clown rotate
  body->SetFixedRotation(true);

  // call parent class' function
  GameObject::SetBody(body);
}

We override the SetBody function from GameObject and as you can see, this function accepts a b2Body object as an argument. This is because every GameObject object will be provided a body by GameWorld and must simply add its respective fixtures—and that is exactly what we do by first defining a shape for the clown.

We make use of the b2PolygonShape class and call its convenient SetAsBox function, passing in half the width and half the height for the desired box. Notice how we create the box for the clown based on his content size while converting from screen to world dimensions.

We then create a fixture definition for the clown body's fixture, set the shape created previously, and give it a restitution coefficient of 0.5f. We then pass the newly created fixture definition to the body's CreateFixture function.

We finally call the SetFixedRotation function for the body, passing in true. This ensures that the body will not rotate under the influence of forces. In essence, the angular velocity for this body will always remain zero. We wind up this function by calling the SetBody function of the parent class. Now that we have defined what the clown will be made up of, let's take the time to define the various states that make him the star of the circus.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.12