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

Listening for collisions


If you remember, right after we created the world, we called its SetContactListener function and passed in a reference to GameWorld. We could do this because our GameWorld class inherits not only from CCLayer, but also from b2ContactListener. As a result of this, GameWorld can receive information about any and all pairs of fixtures colliding by implementing the BeginContact function as follows:

void GameWorld::BeginContact(b2Contact *contact)
{
  b2Body* body_a = contact->GetFixtureA()->GetBody();
  b2Body* body_b = contact->GetFixtureB()->GetBody();

  // only need to observe collisions that involve GameObjects
  if(body_a->GetUserData() == NULL || body_b->GetUserData() == NULL)
  {
    return;
  }

  // identify type of the objects involved in the collision
  EGameObjectType game_object_a_type = 
    ((GameObject*)body_a->GetUserData())->GetType();
  EGameObjectType game_object_b_type = 
    ((GameObject*)body_b->GetUserData())->GetType...