How to rotate the sprite alone and sprite with b2Body in cocos2dx | Techbirds

*SPRITE ROTATION WITH SOME PARTICULAR ANGLE IN PARTICULAR DURATION

CCSprite *test=CCSprite::create(“sprite.png”);
  test->setPosition(ccp(winSize.width/2,winSize.height/2));
  test->setAnchorPoint(ccp(0.5,0.5));
  CCRotateBy *rot=CCRotateBy::create(30, 360);

Here,the sprite named as test is positioned in the centre of the screen and its anchor point is set at the centre of the sprite,anchor point is needed to be set somewhere since the sprite will rotate around that anchor point.CCRotateBy function rotate the sprite with some angle completed in some particular duration(Here the 360 degrees completed in 30 sec).This will rotate the sprite only once.If we want to rotate it continuously,we have to use the following line after the CCRotateBy function as:

test->runAction(CCRepeatForever::create(rot));

*b2BODY ROTATION WITH SOME SPRITE ATTACHED WITHIT

b2Body is a box2d body which is not actually visible to the user,it runs in the background.When we attach some sprite alongwith that body,then with the rotation of the sprite,the b2Body rotation is visible.In order to rotate the body continuously,we have to use the update function with continuous scheduling as:

void class_name::update(float dt)
{
 
    world->Step(dt, 10, 10);
    if(b->GetType() == b2_kinematicBody)
   {  
            //for anticlockwise rotation
            if(b->GetUserData()==testsprite)
            {
            
            float32 b2Angle = CC_DEGREES_TO_RADIANS(0.4);
            b->SetTransform(b->GetPosition(), b->GetAngle() + b2Angle);
            CCSprite *tempSprite = (CCSprite*)b->GetUserData();
            tempSprite->setPosition(ccp(b->GetPosition().x*PTM_RATIO,b->GetPosition().y*PTM_RATIO));
                tempSprite->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle()));

             }
           //for the clockwise rotation
            else
                
            {
                float32 b2Angle = CC_DEGREES_TO_RADIANS(0.4);
                b->SetTransform(b->GetPosition(), b->GetAngle() – b2Angle);
                CCSprite *tempSprite = (CCSprite*)b->GetUserData();
                tempSprite->setPosition(ccp(b->GetPosition().x*PTM_RATIO,b->GetPosition().y*PTM_RATIO));
                tempSprite->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle()));
            

            }
            
            
        }

}

Firstly the b2Body is checked for the kinematic body to differentiate it from the dynamic and static bodies so that the particular body is allowed for the rotation.It is defined in the update function so that the calling is for defined no. of times in a second, depends upon the FPS.If we want to rotate some body in clockwise direction and some in anticlockwise direction,then we have to check it for the userdata also as b->GetUserData().

For the rotation,b2 angle is defined in the degrees and since the b2body takes it in the radians,we have to convert it as CC_DEGREES_TO_RADIANS,then the body is transformed with the defined angle.Some temporary sprite is taken for the rotation of the sprite alongwith that b2Body and that sprite gets the user data same as the  b2body.Then the sprite is positioned and rotated as per the gained user data of the body.Since the sprites takes the angle in degrees,we have to convert the b2body angle from radians to degrees with the function CC_RADIANS_TO_DEGREES.-1 is multiplied with the angle for the rotation of the sprite in the direction of the rotation of the b2body

For the clockwise rotation,the b2Angle defined is subtracted from the previous position of the sprite and for the anticlockwise direction,it is added to the previous location of the body.

3,031 total views, 1 views today

Share this Onfacebook-9943901twitter-1676055linkedin-1758900google-6239755 Tags: android, cocos2dx, Game-development, ios