Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using joints


Joints are used to connect two physics bodies to each other. Then, you can create a complex shape to join some shapes. In addition, you can create objects such as a gear or a motor to use joints. Cocos2d-x has many different types of joints. In this recipe, we explain a typical joint type.

Getting ready

You will create a method that creates a physics object. That's why you have to create multiple physics objects. This method is called makeSprite. You have to add the following code in HelloWorld.h:

cocos2d::Sprite* makeSprite();

You have to add the following code in HelloWorld.cpp:

Sprite* HelloWorld::makeSprite()
{
    auto sprite = Sprite::create("CloseNormal.png");
    auto physicsBody = PhysicsBody::createCircle(sprite->getContentSize().width/2);
    physicsBody->setDynamic(true);
    physicsBody->setContactTestBitmask(true);
    sprite->setPhysicsBody(physicsBody);
    return sprite;
}

How to do it...

In this recipe, we explain PhysicsJointGear. This joint works to...