Iphone 旋转精灵以面向点(cocos2d)

Iphone 旋转精灵以面向点(cocos2d),iphone,ios,ipad,cocos2d-iphone,Iphone,Ios,Ipad,Cocos2d Iphone,我似乎在计算精灵和接触点之间的角度时遇到了问题。我试图让我的精灵在用户触摸屏幕时直接面对触摸点的方向。这是我的密码: -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint tapPosition; for (UITouch *touch in touches){ CGPoint location = [touch locationInView:[touch view]

我似乎在计算精灵和接触点之间的角度时遇到了问题。我试图让我的精灵在用户触摸屏幕时直接面对触摸点的方向。这是我的密码:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint tapPosition;
    for (UITouch *touch in touches){
        CGPoint location = [touch locationInView:[touch view]];
        tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];
    }

    float angle = CC_RADIANS_TO_DEGREES(ccpAngle(fish.position, tapPosition));
    [fish runAction:[CCRotateTo actionWithDuration:0.5 angle:angle]];
}

有什么想法吗?谢谢,试试这个,首先你不需要这个循环,因为你只需要最后一次触摸的位置。您也可以使用下面的[Touchs anyObject]

第二,我不知道ccpAngle在我脑海中做了什么,但当这样的宏不起作用时,有时自己做数学就更容易了

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   CGPoint tapPosition;
   UITouch *touch = [touches anyObject];

   CGPoint location = [touch locationInView:[touch view]];
   tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];

   float dY = fish.position.y - tapPosition.y;
   float dX = fish.position.x - tapPosition.x;
   float offset = dX<0 ? 90.0f : -90.0f;
   float angle = CC_RADIANS_TO_DEGREES(atan2f(dY, dX)) + offset;

   [fish runAction:[CCRotateTo actionWithDuration:0.5 angle:angle]];
}
-(void)cctouchsbegind:(NSSet*)接触事件:(UIEvent*)事件
{
点位;
UITouch*touch=[触摸任何对象];
CGPoint location=[touch locationInView:[touch view]];
tapPosition=[self-convertToNodeSpace:[[CCDirector-sharedDirector]convertToGL:location]];
float dY=fish.position.y-tapPosition.y;
float dX=fish.position.x-tapPosition.x;

浮动偏移量=dXCCPoint pos1=[鱼尾位置]; CCPoint pos2=触摸位置

float theta = atan((pos1.y-pos2.y)/(pos1.x-pos2.x)) * 180 * 7 /22;

float calculatedAngle;

if(pos1.y - pos2.y > 0)
{
    if(pos1.x - pos2.x < 0)
    {
        calculatedAngle = (-90-theta);
    }
    else if(pos1.x - pos2.x > 0)
    {
       calculatedAngle = (90-theta);
    }       
}
else if(pos1.y - pos2.y < 0)
{
    if(pos1.x - pos2.x < 0)
    {
       calculatedAngle = (270-theta);
    }
    else if(pos1.x - pos2.x > 0)
    {
       calculatedAngle = (90-theta);
    }
}
float theta=atan((pos1.y-pos2.y)/(pos1.x-pos2.x))*180*7/22;
浮动计算角;
if(pos1.y-pos2.y>0)
{
if(pos1.x-pos2.x<0)
{
计算角度=(-90θ);
}
else if(pos1.x-pos2.x>0)
{
计算角度=(90θ);
}       
}
else if(pos1.y-pos2.y<0)
{
if(pos1.x-pos2.x<0)
{
计算角度=(270θ);
}
else if(pos1.x-pos2.x>0)
{
计算角度=(90θ);
}
}

在你的跑步动作中使用这个计算出的角度。希望这有帮助…:)

将这个添加到Nikhil答案的末尾,以避免在触摸位置位于精灵右下角时出现负角度

if (calculatedAngle < 0) 
{
     calculatedAngle+=360;
}
if(计算角度<0)
{
计算角度+=360;
}

嘿,谢谢你的尝试。我试着插入了你的代码,但没有效果。似乎旋转不稳。有什么想法吗?谢谢我需要更多的细节,到底发生了什么?当屏幕被点击时,它会旋转,但不是到正确的角度。有时它的角度不是很接近正确,如果不看的话,不可能找出错误所在g它。尝试记录角度,然后在你知道角度应该是什么的地方点击,例如0。你应该看到偏移应该是什么。我看了看,因为某种原因它偏离了266度。所以我只是将它修改为角度:266度。非常随机,我想这可能会阻止精灵旋转到一边(右侧精灵跟随正确,但左侧精灵的后部朝向该点旋转)