【问题标题】:iOS Sprite Kit how to make a projectile point at target in landscape orientation?iOS Sprite Kit如何在横向目标上制作射弹点?
【发布时间】:2014-01-15 04:04:14
【问题描述】:

我的 sprite kit 游戏中有一个图标,我打算在一个角色射击另一个角色时将其用作动画弹丸。我正在尝试将这个射弹指向目标。

我将它旋转 pi/4.0 的基本角度,使其直接指向右侧。然后我希望箭头从这个位置旋转并指向目标。

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

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

编辑:下面的代码现在可以工作了,只需要执行 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];

    }

【问题讨论】:

    标签: objective-c ios7 rotation geometry sprite-kit


    【解决方案1】:

    我有一个类似的问题:大炮必须“跟踪”一个目标。 在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;
    }
    

    catchTargetAtPoint 必须在场景的update 方法中调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多