Objective c iOS精灵套件如何使投射物指向横向目标?

Objective c iOS精灵套件如何使投射物指向横向目标?,objective-c,ios7,rotation,geometry,sprite-kit,Objective C,Ios7,Rotation,Geometry,Sprite Kit,我在我的精灵套件游戏中有一个图标,当一个角色射击另一个角色时,我打算用它作为动画投射物。我正试着让这个射弹指向目标 我将它旋转一个π/4.0的底角,使它直接指向右边。然后我希望箭头从这个位置旋转并指向目标 下面的代码几乎可以工作,但在我看来,它总是看起来好像炮弹偏离了正确的角度。如果角度正确,当向箭头提供移动到目标x、y坐标的动作时,箭头将指向移动方向 如何正确计算从一个(x,y)点到另一个(x,y)点的发射角度? //correct for pointing in the bottom ri

我在我的精灵套件游戏中有一个图标,当一个角色射击另一个角色时,我打算用它作为动画投射物。我正试着让这个射弹指向目标

我将它旋转一个π/4.0的底角,使它直接指向右边。然后我希望箭头从这个位置旋转并指向目标

下面的代码几乎可以工作,但在我看来,它总是看起来好像炮弹偏离了正确的角度。如果角度正确,当向箭头提供移动到目标x、y坐标的动作时,箭头将指向移动方向

如何正确计算从一个(x,y)点到另一个(x,y)点的发射角度?

//correct for pointing in the bottom right corner    
    float baseRotation = M_PI/4.0;   
    float zeroCheckedY = destination.y - origin.y;
    float zeroCheckedX = destination.x - origin.x;
    SKAction* rotate = nil;
    if(zeroCheckedX != 0)
    {
//not sure if I'm calculating this angle correctly.
        float angle =  atanf(zeroCheckedY/zeroCheckedX);
        rotate =  [SKAction rotateByAngle:baseRotation + angle duration:0];
    }else
    {
        rotate =  [SKAction rotateByAngle:baseRotation duration:0];

    }
编辑:下面的代码现在可以工作了,只需要执行zeroCheckedY/zeroCheckedX。

//correct for pointing in the bottom right corner    
    float baseRotation = M_PI/4.0;   
    float zeroCheckedY = destination.y - origin.y;
    float zeroCheckedX = destination.x - origin.x;
    SKAction* rotate = nil;
    if(zeroCheckedX != 0)
    {
//not sure if I'm calculating this angle correctly.
        float angle =  atanf(zeroCheckedY/zeroCheckedX);
        rotate =  [SKAction rotateByAngle:baseRotation + angle duration:0];
    }else
    {
        rotate =  [SKAction rotateByAngle:baseRotation duration:0];

    }

我有一个类似的问题:加农炮必须“跟踪”目标。 在
CannonNode
中,我定义了以下方法:

- (void)catchTargetAtPoint:(CGPoint)target {
    CGPoint cannonPointOnScene = [self.scene convertPoint:self.position fromNode:self.parent];
    float angle = [CannonNode pointPairToBearingDegreesWithStartingPoint:cannonPointInScene endingPoint:target];
    if (self.zRotation < 0) {
        self.zRotation = self.zRotation + M_PI * 2;
    }
    if ((angle < self.maxAngle) && (angle> self.minAngle)) {
        [self runAction:[SKAction rotateToAngle:angle duration:1.0f]];
    }
}

+ (float)pointPairToBearingDegreesWithStartingPoint:(CGPoint)startingPoint endingPoint:(CGPoint) endingPoint {
    CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
    float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
    return bearingRadians;
}
-(无效)捕获目标点:(CGPoint)目标{
CGPoint cannonPointOnScene=[self.scene convertPoint:self.positionfromNode:self.parent];
浮动角度=[CannonNode PointPairToBearingDegrees with StartingPoint:cannonPointInScene endingPoint:target];
if(自旋转<0){
self.zRotation=self.zRotation+M_PI*2;
}
如果((角度self.minAngle)){
[自运行:[SKAction rotateToAngle:角度持续时间:1.0f];
}
}
+(浮动)点对承载度数与起点:(CGPoint)起点终点:(CGPoint)终点{
CGPoint originPoint=CGPointMake(endingPoint.x-startingPoint.x,endingPoint.y-startingPoint.y);//通过从起点减去终点得到原点到原点
float bearingRadians=atan2f(originPoint.y,originPoint.x);//以弧度获取方向角
回归英格拉迪亚;
}
必须在场景的
update
方法中调用
catchTargetPoint