如何根据平移手势速度将角度脉冲应用于Sprite Kit节点 [英] How can I apply an angular impulse to a Sprite Kit node based on a pan gesture velocity

查看:119
本文介绍了如何根据平移手势速度将角度脉冲应用于Sprite Kit节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里要做的是围绕其锚点旋转SKSpriteNode并使其速度和方向与平移手势相匹配。因此,如果我的平移手势是精灵顺时针方向,那么精灵顺时针旋转。

What I'm looking to do here is to spin a SKSpriteNode around its anchor point and have its speed and direction match a pan gesture. So if my pan gesture is clockwise around the sprite then then sprite spins clockwise.

我的代码问题是它适用于左边精灵下面的平底锅从右/向右,但当我尝试垂直平移时它根本没有,如果我在精灵上方平移,它会使精灵旋转错误。

The problem I have with my code is that it works great for pans below the sprite from left to right/right to left, but not at all when I try and pan vertically and it makes the sprite spin the wrong way if I pan above the sprite.

这是什么我到目前为止 -

Here's what I've got so far -

let windmill = SKSpriteNode(imageNamed: "Windmill")

override func didMoveToView(view: SKView) {
    /* Setup gesture recognizers */
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
    self.view?.addGestureRecognizer(panGestureRecognizer)

    windmill.physicsBody = SKPhysicsBody(circleOfRadius: windmill.size.width)
    windmill.physicsBody?.affectedByGravity = false
    windmill.name = "Windmill"
    windmill.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
    self.addChild(windmill)
}

func handlePanGesture(recognizer: UIPanGestureRecognizer) {
    if (recognizer.state == UIGestureRecognizerState.Changed)
    {
        pinwheel.physicsBody?.applyAngularImpulse(recognizer.velocityInView(self.view).x)
    }
}

我知道它没有旋转垂直平底锅的原因我只得到了x值所以我想我需要以某种方式将它们结合起来。

I know the reason it's not spinning with vertical pans in that I'm only getting the x value so I figure I need to combine these somehow.

我也尝试使用applyImpulse:atPoint:,但这会导致整个精灵被扫除。

I have also tried this using applyImpulse:atPoint:, but that results in the whole sprite being swept away.

推荐答案

以下步骤将根据平移手势旋转节点:

The following steps will rotate a node based on a pan gesture:


  1. 存储来自中心的向量节点到平移手势的起始位置

  2. 从节点中心到平移手势的结束位置形成一个向量

  3. 确定tw的交叉产品的标志o向量

  4. 计算平移手势的速度

  5. 使用速度和方向将角度脉冲应用于节点

  1. Store a vector from the center of the node to the starting location of the pan gesture
  2. Form a vector from the center of the node to the ending location of the pan gesture
  3. Determine the sign of the cross product of the two vectors
  4. Calculate the speed of the pan gesture
  5. Apply angular impulse to the node using the speed and direction

以下是Swift中如何做到这一点的一个例子......

Here's an example of how to do that in Swift...

// Declare a variable to store touch location
var startingPoint = CGPointZero

// Pin the pinwheel to its parent
pinwheel.physicsBody?.pinned = true
// Optionally set the damping property to slow the wheel over time
pinwheel.physicsBody?.angularDamping = 0.25

声明pan处理程序方法

Declare pan handler method

func handlePanGesture(recognizer: UIPanGestureRecognizer) {
    if (recognizer.state == UIGestureRecognizerState.Began) {
        var location = recognizer.locationInView(self.view)
        location = self.convertPointFromView(location)
        let dx = location.x - pinwheel.position.x;
        let dy = location.y - pinwheel.position.y;
        // Save vector from node to touch location
        startingPoint = CGPointMake(dx, dy)
    }
    else if (recognizer.state == UIGestureRecognizerState.Ended)
    {
        var location = recognizer.locationInView(self.view)
        location = self.convertPointFromView(location)

        var dx = location.x - pinwheel.position.x;
        var dy = location.y - pinwheel.position.y;

        // Determine the direction to spin the node
        let direction = sign(startingPoint.x * dy - startingPoint.y * dx);

        dx = recognizer.velocityInView(self.view).x
        dy = recognizer.velocityInView(self.view).y

        // Determine how fast to spin the node. Optionally, scale the speed
        let speed = sqrt(dx*dx + dy*dy) * 0.25

        // Apply angular impulse
        pinwheel.physicsBody?.applyAngularImpulse(speed * direction)
    }
}

这篇关于如何根据平移手势速度将角度脉冲应用于Sprite Kit节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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