O'Reilly logo

Cocos2d-x Game Development Essentials by Gareth Jones, Arutosh Gurung, Frahaan Hussain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required