Ios SpriteKit:如何将触碰转化为匀速的矢量?

Ios SpriteKit:如何将触碰转化为匀速的矢量?,ios,objective-c,swift,sprite-kit,game-physics,Ios,Objective C,Swift,Sprite Kit,Game Physics,我正在使用触摸设置运动中的SpriteKit节点。目前我这样做: //(previous code turns current touch and last touch into thisP and thatP)... CGPoint velocity = CGPointMake(thisP.x - lastP.x, thisP.y - lastP.y); //If the velocity is non-zero, apply it to the node: if (!CGPoint

我正在使用触摸设置运动中的SpriteKit节点。目前我这样做:

 //(previous code turns current touch and last touch into thisP and thatP)...

 CGPoint velocity = CGPointMake(thisP.x - lastP.x, thisP.y - lastP.y);

//If the velocity is non-zero, apply it to the node:

if (!CGPointEqualToPoint(velocity, CGPointZero)) {
    [[MyNode physicsBody] applyForce:CGVectorMake(velocity.x, velocity.y)];
}
这对于让节点根据用户的滑动速度移动不同的速度非常有效。哦,安娜,要是那是我想要的就好了

我实际想要的效果是,每个节点都以统一的速度移动,不管它们被刷得有多快。看起来我必须进行某种计算,从向量中去除幅值,但保留方向,这是我无法理解的


这有可能吗?

你走对了方向;你要找的东西叫做你已经在计算的触摸运动矢量的大小。您可以通过计算起始向量的长度并将其每个分量除以该长度,然后将它们乘以希望最终向量具有的长度(或在本例中为速度)来获得该值。代码:

float length = hypotf(velocity.x, velocity.y); // standard Euclidean distance: √(x^2 + y^2)
float multiplier = 100.0f / length; // 100 is the desired length
velocity.x *= multiplier;
velocity.y *= multiplier;

你在正确的轨道上;你要找的东西叫做你已经在计算的触摸运动矢量的大小。您可以通过计算起始向量的长度并将其每个分量除以该长度,然后将它们乘以希望最终向量具有的长度(或在本例中为速度)来获得该值。代码:

float length = hypotf(velocity.x, velocity.y); // standard Euclidean distance: √(x^2 + y^2)
float multiplier = 100.0f / length; // 100 is the desired length
velocity.x *= multiplier;
velocity.y *= multiplier;

你在正确的轨道上;你要找的东西叫做你已经在计算的触摸运动矢量的大小。您可以通过计算起始向量的长度并将其每个分量除以该长度,然后将它们乘以希望最终向量具有的长度(或在本例中为速度)来获得该值。代码:

float length = hypotf(velocity.x, velocity.y); // standard Euclidean distance: √(x^2 + y^2)
float multiplier = 100.0f / length; // 100 is the desired length
velocity.x *= multiplier;
velocity.y *= multiplier;

你在正确的轨道上;你要找的东西叫做你已经在计算的触摸运动矢量的大小。您可以通过计算起始向量的长度并将其每个分量除以该长度,然后将它们乘以希望最终向量具有的长度(或在本例中为速度)来获得该值。代码:

float length = hypotf(velocity.x, velocity.y); // standard Euclidean distance: √(x^2 + y^2)
float multiplier = 100.0f / length; // 100 is the desired length
velocity.x *= multiplier;
velocity.y *= multiplier;


首先,安娜是谁?第二,你是否尝试过使用手势识别器,并根据滑动方向应用设定的速度?@sangony,“哦,安娜,要是……”听起来像是对电影《冻结》的暗示。我强烈推荐。@NoahWitherspoon-学到了一些新东西。@NoahWitherspoon:在鼻子上!首先,安娜是谁?第二,你是否尝试过使用手势识别器,并根据滑动方向应用设定的速度?@sangony,“哦,安娜,要是……”听起来像是对电影《冻结》的暗示。我强烈推荐。@NoahWitherspoon-学到了一些新东西。@NoahWitherspoon:在鼻子上!首先,安娜是谁?第二,你是否尝试过使用手势识别器,并根据滑动方向应用设定的速度?@sangony,“哦,安娜,要是……”听起来像是对电影《冻结》的暗示。我强烈推荐。@NoahWitherspoon-学到了一些新东西。@NoahWitherspoon:在鼻子上!首先,安娜是谁?第二,你是否尝试过使用手势识别器,并根据滑动方向应用设定的速度?@sangony,“哦,安娜,要是……”听起来像是对电影《冻结》的暗示。我强烈推荐。@NoahWitherspoon-学到了一些新东西。@NoahWitherspoon:在鼻子上!还有一个
hypot()
函数,在这里很方便。@MartinR:好的评论,让我搜索如何使用它。如果有人需要参考资料,请在本页卢修斯的评论中注明:谢谢马丁!不知怎的,我从来没有遇到过那个。更新了答案。你们干得好!这是最好的。还有一个
hypot()
函数,在这里很方便。@MartinR:好的评论,让我搜索如何使用它。如果有人需要参考资料,请在本页卢修斯的评论中注明:谢谢马丁!不知怎的,我从来没有遇到过那个。更新了答案。你们干得好!这是最好的。还有一个
hypot()
函数,在这里很方便。@MartinR:好的评论,让我搜索如何使用它。如果有人需要参考资料,请在本页卢修斯的评论中注明:谢谢马丁!不知怎的,我从来没有遇到过那个。更新了答案。你们干得好!这是最好的。还有一个
hypot()
函数,在这里很方便。@MartinR:好的评论,让我搜索如何使用它。如果有人需要参考资料,请在本页卢修斯的评论中注明:谢谢马丁!不知怎的,我从来没有遇到过那个。更新了答案。你们干得好!所以在最好的时候。