如何在接触角上向节点施加脉冲 [英] How to apply impulse to the node on touch angle

查看:26
本文介绍了如何在接触角上向节点施加脉冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望节点向正确的方向移动,但脉冲要具有设定的强度.

I want the node to move in the right direction but the impulse to be with set strength.

let node: SKSpriteNode!;

node = SKSpriteNode(color: UIColor.greenColor(), size: CGSizeMake(50, 50));
node.physicsBody = SKPhysicsBody(rectangleOfSize: node.size);
node.physicsBody?.affectedByGravity = false;
node.physicsBody?.allowsRotation = false;

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    node.physicsBody?.velocity = CGVectorMake(0, 0);   

    // ver 1     
    node.physicsBody?.applyImpulse(CGVectorMake((0.4) * (location.x - node.position.x), (0.4) * (location.y - node.position.y)), atPoint: CGPointMake(position.x,position.y));

    // ver 2
    let offset:CGPoint = self.vecSub(location, b: ghost.position);
    let direction: CGPoint = self.vecNormalize(offset);
    var len: CGPoint = self.vecMult(direction, b: 40);
    let impulseVector:CGVector = CGVectorMake(len.x, len.y);
    ghost.physicsBody?.applyImpulse(impulseVector);

}

func vecAdd(a: CGPoint, b:CGPoint) -> CGPoint {
    return CGPointMake(a.x + b.x, a.y + b.y);
}

func vecSub(a: CGPoint, b:CGPoint) -> CGPoint {
    return CGPointMake(a.x - b.x, a.y - b.y);
}

func vecMult(a: CGPoint, b:CGFloat) -> CGPoint {
    return CGPointMake(a.x * b, a.y * b);
}

func vecLenght(a:CGPoint)->CGFloat{
    return CGFloat( sqrtf( CFloat(a.x) * CFloat(a.x) + CFloat(a.y) * CFloat(a.y)));
}

func vecNormalize(a:CGPoint)->CGPoint{
    let len : CGFloat = self.vecLenght(a);
    return CGPointMake(a.x / len, a.y / len);
}

  • 版本 1 太可怕了

    • version 1 is horrible

      版本 2 还可以,但太贵了

      version 2 is okay, but is too expensive

      版本 3:一些不昂贵的东西并且施加 15-100 强度的脉冲,因为如果触摸在屏幕的边缘,节点应该只移动其当前位置的 15-100 而不到达触摸位置

      version 3: something not expensive and to apply impulse with 15-100 strength, because if the touch is at the edges of the screen the node should move only 15-100 of its current possition without reaching the touch position

      推荐答案

      您在上面详述的两种方法都有效,所以我不确定您的问题是什么.另外,我不确定方法二是不是太贵了,你用它有帧率下降吗?

      Both the methods you've detailed above work so I'm not exactly sure what you're problem is. Also, I'm not sure method two is it's too expensive, are you having frame rate drops using it?

      但我有另一种方法可以让你做你想做的事,它基本上是第二个版本,但已经清理过了.但首先,我只想用您当前的代码指出几点:

      But I've got another way you could do what you're after, it's basically the second version but cleaned up. But firstly I just wanted to point out a couple things with your current code:

      1.你不需要;到每一行的末尾.

      1. You don't need to ; to the end of each line.

      2. 除了使用 vecAddvecSub 等,您还可以重载 +-*/ 运算符,它们将使您的代码更清晰、更清晰.这样,运算符也将是全局的,因此您可以在需要操作向量的任何其他地方使用它们.

      2. Instead of using vecAdd, vecSub etc you could overload the +, -, * and / operators which would make your code cleaner and clearer. This way, the operators would also be global so you could use them anywhere else you need to manipulate vectors.

      无论如何,这是我的尝试:

      Anyway, here's my attempt at it:

      首先,扩展 CGVector 以添加您需要的功能.你定义为函数的 length 之类的东西可能是 CGVector 的属性:

      Firstly, extend CGVector to add the functionality you need. Things like length, which you were defining as functions, could be properties of CGVector:

      extension CGVector {
          init(p1: CGPoint, p2: CGPoint) {
              self.dx = p1.x - p2.x
              self.dy = p1.y - p2.y
          }
      
          var length: CGFloat {
              get { return hypot(dx, dy) }
      
              set {
                  normalise()
                  dx *= newValue
                  dy *= newValue
              }
          }
      
          mutating func normalise() {
              dx /= length
              dy /= length
          }
      }
      

      其次,使用新方法:

      var vec = CGVector(p1: location, p2: ghost.position)
      vec.length = 40
      ghost.physicsBody!.applyImpulse(vec)
      

      或者,如果您希望脉冲的大小与用户按下的鬼魂的距离有关,请使用以下内容:

      Alternatively, if you wanted the size of the impulse to relate to how far away from the ghost the user pressed, use the following:

      vec.length *= 0.1 
      

      希望有帮助!

      这篇关于如何在接触角上向节点施加脉冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆