Using the physics engine
What should you do when you realize that your game needs to simulate real-world situations? You know that the answer is to use a physics engine. When you start using a physics engine, you have to use some new classes and methods. In this recipe, you will learn how to use the basic physics engine in Cocos2d-x.
How to do it...
Firstly, you have to create the physics world in your scene. You can create it by using the following code:
Scene* HelloWorld::createScene() { auto scene = Scene::createWithPhysics(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; }
Next, you have to add the physics bodies in the physics world. A physics body is not visible. It is a physical shape such as a square or a circle or a more complex shape. Here, let's create a square shape. You have to create it and set it to the sprite to be visible.
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director...