Clamp rotation in cocos2d-x 3.0 beta2

Clamp rotation in cocos2d-x 3.0 beta2
0.0 0


I have a problem clamping rotation for a PhysicsBody.
I want its rotation to stay between -30 and 90. But in cocos2d-x 3.0 setRotation is a private member. How could I do it?


Use Node:setRotation instead.


Thanks for your reply, but it doesn’t work.
I tried doing it, and they don’t rotate in the proper way.
I may be missing something, so here is the source code of an example of the problem:

Scene* HelloWorld::createScene()
// ‘scene’ is an autorelease object
auto scene = Scene::createWithPhysics();
// ‘layer’ is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene

// return the scene
return scene;


// on “init” you need to initialize your instance
bool HelloWorld::init()
// 1. super init first
if ( !Layer::init() )
return false;

Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();

// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
this->addChild(menu, 1);

// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

auto label = LabelTTF::create("Ehi culo!", "Arial", 9);

// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);

// add "HelloWorld" splash screen"
auto sprite = Sprite::create("fly1.png");

// position the sprite on the center of the screen
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
hero = sprite;
hero->setPhysicsBody(PhysicsBody::createEdgeBox(Size(66,46) ));
// add the sprite as a child to this layer
this->addChild(sprite, 0);


return true;


HelloWorld::update(float dt){
hero->setRotation(clampf(hero->getPhysicsBody()->getRotation(), -90, 90));

void HelloWorld::menuCloseCallback(Object* pSender)



Probably i’m doing something wrong, because if I add in the update method:
this->setPositionX(getPositionX()- 509*dt);
the physics body doesn’t move. Still i don’t understand where my error is.


may it be related to this bug?


These are two strange issues. Still I can’t understand wether it is some sort of bug or i’m doing something wrong


I downloaded the version on gitHub, and the clamp problem is solved. But there still remains the problem that physics body doesn’t move with the sprite, if i move the layer!