3d 鱼的旋转角度

enum FishSiwmDirection
{
    SWIM_FRONT = -1,
    SWIM_BACK = 1,
};
void HelloWorld::update(float dt)
{

    int  _direction = SWIM_BACK;//设置鱼是向玩家游来 还是背离玩家游
	Vec2 _offset = m_sprite->getPosition() - _old;//鱼的新旧坐标差

 	float _radianX = atan2(_offset.y, _offset.x);//旋转x轴的弧度
 	float _angleX = CC_RADIANS_TO_DEGREES(_radianX);//旋转x轴的角度
    
    //使鱼的x轴旋转只能在50和-50度之间
	if (abs(_angleX) > 90)
	{
		if (_angleX>0)
		{
			_angleX = 180 - _angleX;
		}
		else
		{
			_angleX = -180 - _angleX;
		}

		
	}
	if (_angleX > 50)
	{
		_angleX = 50;
	}
	if (_angleX < -50)
	{
		_angleX = -50;
	}
    
    

    //计算y轴旋转角度
	float _radianY =  atan2(_offset.x, _direction * _offset.y);
	float _angleY = CC_RADIANS_TO_DEGREES(_radianY);
	
   
    //设置鱼的旋转角度
    log("%f", _angleY);
    m_sprite->setRotation3D(Vec3(-_angleX,  _angleY ,m_sprite->getRotation3D().z));


    //设置鱼的大小(远近效果)
    float _s;
    if (_direction == SWIM_FRONT)
    {
        
        _s = 90 - _angleY;
        _s = _s / 90/3 +0.7;
        m_sprite->setScale( _s);

    }
    else
    {
        _s = 90 - _angleY;
        if (_angleY < 0)
        {
            _s -= 360;
        }
        
        
        _s = _s / 90/3 + 1.3;
    }
    m_sprite->setScale( _s);
    
    //设置老坐标为新坐标
	_old = m_sprite->getPosition();

}

你可能感兴趣的:(3d 鱼的旋转角度)