Player collision detection

The only collision detection that needs to be performed is between the players, the space pod, and the moving asteroids that the player has to avoid. If the player collides with an asteroid, then the game will transition to the Game Over scene.

Setting up collision detection

In the GameScene.h file, add the following lines:

void setPhysicsWorld(cocos2d::PhysicsWorld* world)
{
  mWorld = world;
  mWorld->setGravity(cocos2d::Vect(0, 0));
}
bool onContactBegin(cocos2d::PhysicsContact& contact);
cocos2d::PhysicsWorld* mWorld;

The first statement declares and initializes the physics world method that assigns the physics world and sets the gravity to 0 because the game doesn't require gravity as it's in space. So, the physics engine will simply be used for collision detection.

The second statement declares the onContactBegin() method that will be called when a collision has taken place.

The third statement creates the local physics world.

The GameScene.h file should look like the following screenshot once the preceding code has been added to the header:

Setting up collision detection

Implementing the collision detection

The next step is to implement the physics into the GameScene.cpp file using the following steps:

  1. Modify the init() method as follows:
    • Change auto scene = Scene::create(); to auto scene = Scene::createWithPhysics();
    • Add layer->setPhysicsWorld(scene->getPhysicsWorld()); below the layer declaration

    The following screenshot is how the GameScene.cpp file will look after these modifications:

    Implementing the collision detection
  2. Add the following code lines after the player sprite initialization inside the init() method:
    auto body = PhysicsBody::createCircle(playerSprite->getContentSize().width / 2);
    body->setContactTestBitmask(true);
    body->setDynamic(true);
    playerSprite->setPhysicsBody(body);

    The following screenshot is what the updated GameScene.cpp file will look like:

    Implementing the collision detection

    Let's go over the preceding code lines that have just been added:

    • The first statement creates a physics body for the space pod, which will be a circle for the purpose of this game
    • The second statement sets the body up for collision detection
    • The final statement assigns the body to the player's sprite
  3. Add the following code lines before the return statement inside the init() method:
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(GameScreen::onContactBegin, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

    The following screenshot is how this will look in the file:

    Implementing the collision detection
  4. The previous code adds a listener to actively listen for collisions; if a collision is detected, the onContactBegin method will be called.
  5. Using the following code, add a physics body for the asteroids every time they are spawned. Add this after pushing the asteroid to the asteroids' vector.
    auto body = PhysicsBody::createCircle(asteroids[asteroids.size() - 1]->getContentSize().width / 2);
    body->setContactTestBitmask(true);
    body->setDynamic(true);
    asteroids[asteroids.size() - 1]->setPhysicsBody(body);

    The following screenshot is how the file will look after adding the preceding code:

    Implementing the collision detection

    Note

    Nothing is done differently for the asteroid body than the space pod apart from changing the size and position.

  6. The final step is to implement the onContactBegin method, which will call the GoToGameOverScene method that was implemented earlier in the book:
    bool GameScreen::onContactBegin(PhysicsContact& contact)
    {
        GoToGameOverScene(this);
    
        return true;
    }

Though only circular collision detection was used in this chapter as it was sufficient, when using more complex objects and shapes, the need for better and bespoke shapes can arise, and then tools such as PhysicsEditor can be used to create custom shapes. PhysicsEditor is an amazing tool that allows you to create custom shapes using the built-in tracer or manipulating the physics body shape using a mouse.

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

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