Book Image

Cocos2d-X Game Development Blueprints

By : Karan Sequeira
Book Image

Cocos2d-X Game Development Blueprints

By: Karan Sequeira

Overview of this book

Table of Contents (17 chapters)
Cocos2d-x Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the walls


The first physics body we create is the body to represent the walls in our game. As you saw in the screenshot, the clown is restricted on both sides by walls. So let's create these walls in the CreateWall function in GameWorld.cpp:

void GameWorld::CreateWall()
{
  // wall will be a static body placed at the origin
  b2BodyDef wall_def;
  wall_def.position.Set(0, 0);
  wall_ = world_->CreateBody(&wall_def);

  // get variables for the wall edges
  float left_wall = SCREEN_TO_WORLD(WALL_WIDTH);
  float right_wall = SCREEN_TO_WORLD(SCREEN_SIZE.width - WALL_WIDTH);
  float top = SCREEN_TO_WORLD(SCREEN_SIZE.height);
  
  // create and add two fixtures using two edge shapes
  b2EdgeShape wall_shape;
  wall_shape.Set(b2Vec2(left_wall, 0), b2Vec2(left_wall, top));
  wall_->CreateFixture(&wall_shape, 0);
  wall_shape.Set(b2Vec2(right_wall, 0), b2Vec2(right_wall, top));
  wall_->CreateFixture(&wall_shape, 0);
}

We start by creating a b2BodyDef class, which basically...