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.
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:
The next step is to implement the physics into the GameScene.cpp
file using the following steps:
init()
method as follows:auto scene = Scene::create();
to auto scene = Scene::createWithPhysics();
layer->setPhysicsWorld(scene->getPhysicsWorld());
below the layer declarationThe following screenshot is how the GameScene.cpp
file will look after these modifications:
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:
Let's go over the preceding code lines that have just been added:
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:
onContactBegin
method will be called.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:
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.