cocos2d-x三角函数和反三角函数
//计算角度
float CMgrSkill::getAngle( CCPoint & frome, CCPoint & to )
{
double len_y = to.y - frome.y;
double len_x = to.x - frome.x;
if( 0 == len_x && frome.y <= to.y )
{
return 0;
}
else if( 0 == len_x && frome.y > to.y )
{
return 180;
}
double tan_yx = tan_yx = std::abs(len_y)/std::abs(len_x);
float angle = 0;
if(len_y > 0 && len_x < 0) {
angle = atan(tan_yx)*180/M_PI - 90;
} else if (len_y > 0 && len_x > 0) {
angle = 90 - atan(tan_yx)*180/M_PI;
} else if(len_y < 0 && len_x < 0) {
angle = -atan(tan_yx)*180/M_PI - 90;
} else if(len_y < 0 && len_x > 0) {
angle = atan(tan_yx)*180/M_PI + 90;
}
return angle;
}
//计算步长
void CBattleField::streakUpdate( float delta )
{
if( NULL != m_pTarget )
{
int r = random()%5;
int k = random()%2;
float e = CC_DEGREES_TO_RADIANS(m_pTarget->getRotation());
float x = r * cos(e);
float y = r * sin(e);
CCLOG( "x:%f y:%f", x, y );
m_pStreak->setPosition( ccp( m_pTarget->getPositionX()+(1==k?x:-x), m_pTarget->getPositionY()+(1==k?y:-y) ) );
}
}