【问题标题】:Understanding CGPoint method了解 CGPoint 方法
【发布时间】:2013-09-24 03:22:12
【问题描述】:

我无法理解以下教程中的一些数学:

Sprite Kit Tutorial

我不确定如何理解偏移量。教程进行到一半时,Ray 使用了以下代码:

UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

// 2 - Set up initial location of projectile
SKSpriteNode * projectile = [SKSpriteNode spriteNodeWithImageNamed:@"projectile"];
projectile.position = self.player.position;

// 3- Determine offset of location to projectile
CGPoint offset = rwSub(location, projectile.position);

rwSub 在哪里

static inline CGPoint rwSub(CGPoint a, CGPoint b) {
    return CGPointMake(a.x - b.x, a.y - b.y);
}

我知道这段代码有效,但我不明白。我尝试 NSLogging 触摸点和偏移点,它们并没有形成如图所示的三角形:


(来源:raywenderlich.com

这是我从输出中得到的:

Touch Location
 X: 549.000000 Y: 154.000000
Offset
 X: 535.500000 Y: -6.000000

这不会形成正确方向的向量..但它仍然有效吗? 谁能解释一下偏移是如何工作的?

【问题讨论】:

  • 原始教程包含一个指向向量数学页面的链接。你读过吗?
  • 是的,我有。我也做过自己的计算。我只是不明白相对于屏幕的输出。

标签: objective-c geometry cgpoint sprite-kit


【解决方案1】:

偏移量是与忍者的差异,以及你所触及的点。因此,您记录的触摸是向右 535 分,向下 6 分 (-6)。

所以它朝着正确的方向前进,相对于玩家。

本教程还强制忍者之星离开屏幕,通过

 // 6 - Get the direction of where to shoot
CGPoint direction = rwNormalize(offset);

// 7 - Make it shoot far enough to be guaranteed off screen
CGPoint shootAmount = rwMult(direction, 1000);

// 8 - Add the shoot amount to the current position       
CGPoint realDest = rwAdd(shootAmount, projectile.position);

画一些图,会帮助你理解的。

【讨论】:

    【解决方案2】:

    在这种情况下,偏移量仅表示与角色相关的触摸位置,并让您知道弹丸将瞄准的位置。

    在教程中,您可以在接下来的几行中看到:

    // 4 - Bail out if you are shooting down or backwards
        if (offset.x <= 0) return;
    

    在此示例中,offset.x &lt; 0 表示射弹在 x 轴上瞄准忍者身后的某物,其中 0 是角色的 x 坐标。

    这里的想法是在角色自己的参考中转换射弹的目标坐标,以便更好地了解彼此的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 2014-07-20
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多