Cocos2d-x study notes (11.2) RotateBy RotateTo

1. RotateBy RotateTo

Two rotation method. RotateBy is the angle of the current angle of rotation is provided. RotateTo directly to the rotation angle setting, follow the direction of "the principle of proximity." Both no inheritance relationship.

1.1 member variables

RotateBy member variables:

BOOL _is3D; 
Vec3 _deltaAngle; // angularly disposed 
Vec3 _startAngle; // angle start

RotateTo member variables:

BOOL _is3D; 
Vec3 _dstAngle; // angularly disposed 
Vec3 _startAngle; // angle start 
Vec3 _diffAngle; // actual rotational angle

1.2 create

RotateBy:

// initWithDuration fang_fa 
ActionInterval :: initWithDuration (in for DURATION)) 
deltaAnglex = = deltaAngle deltaAngley;

RotateTo:

// initWithDuration方法
ActionInterval::initWithDuration(duration))
_dstAngle.x = dstAngleX;
_dstAngle.y = dstAngleY;

1.3 start with target

RotateBy:

ActionInterval::startWithTarget(target);
    if(_is3D)
    {
        _startAngle = target->getRotation3D();
    }
    else
    {
        _startAngle.x = target->getRotationSkewX();
        _startAngle.y = target->getRotationSkewY();
    }

_startAngle get the current rotation angle.

RotateTo:

    if (_is3D)
    {
        _startAngle = _target->getRotation3D();
    }
    else
    {
        _startAngle.x = _target->getRotationSkewX();
        _startAngle.y = _target->getRotationSkewY();
    }

    calculateAngles(_startAngle.x, _diffAngle.x, _dstAngle.x);
    calculateAngles(_startAngle.y, _diffAngle.y, _dstAngle.y);
    calculateAngles(_startAngle.z, _diffAngle.z, _dstAngle.z);

In addition to obtaining the current angle, also called calculateAngles method of calculating _diffAngle.

1.4 "proximity principle": RotateTo method of calculateAngles

First, the angle of the beginning of the remainder of plus or minus 360 degrees:

if (start angle> 0 ) 
    { 
        Start Angle = fmodf (start angle, 360.0f ); 
    } 
    Else 
    { 
        Start Angle = fmodf (start angle, - 360.0f ); 
    }

Then calculate diffAngle:

diffAngle = dstAngle - start angle;

DiffAngle modified to the new value of the calculated value after "proximity principle":

if (diffAngle > 180)
    {
        diffAngle -= 360;
    }
    if (diffAngle < -180)
    {
        diffAngle += 360;
    }

1.5 may

RotateBy, _deltaAngle we set is the angle of rotation:

            if (_startAngle.x == _startAngle.y && _deltaAngle.x == _deltaAngle.y)
            {
                _target->setRotation(_startAngle.x + _deltaAngle.x * time);
            }
            else
            {
                _target->setRotationSkewX(_startAngle.x + _deltaAngle.x * time);
                _target->setRotationSkewY(_startAngle.y + _deltaAngle.y * time);
            }

RotateTo, _diffAngle is the actual angle of rotation:

            if (_startAngle.x == _startAngle.y && _diffAngle.x == _diffAngle.y)
            {
                _target->setRotation(_startAngle.x + _diffAngle.x * time);
            }
            else
            {
                _target->setRotationSkewX(_startAngle.x + _diffAngle.x * time);
                _target->setRotationSkewY(_startAngle.y + _diffAngle.y * time);
            }

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-action-rotateby-rotateto.html