首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于pan手势速度对雪碧套件节点施加角脉冲?

如何基于pan手势速度对雪碧套件节点施加角脉冲?
EN

Stack Overflow用户
提问于 2014-10-27 15:51:38
回答 1查看 1.8K关注 0票数 1

我在这里要做的是旋转一个SKSpriteNode围绕它的锚点,并有它的速度和方向匹配的潘手势。因此,如果我的盘手势顺时针围绕雪碧,那么精灵会顺时针旋转。

我的代码存在的问题是,它对雪碧下面的平底锅(从左到右/从右到左)很好,但当我尝试垂直平移时就一点都不起作用了,如果我把雪碧移到雪碧上面,它会使它以错误的方式旋转。

到目前为止我得到的是-

代码语言:javascript
复制
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值,所以我想我需要以某种方式组合它们。

我也尝试过使用applyImpulse:atPoint:,但是这会导致整个精灵被冲走。

EN

回答 1

Stack Overflow用户

发布于 2014-10-31 04:47:27

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

  1. 将矢量从节点中心存储到pan手势的起始位置。
  2. 形成从节点中心到pan手势结束位置的向量。
  3. 确定两个向量的交叉乘积的符号
  4. 计算pan手势的速度
  5. 使用速度和方向对节点施加角脉冲。

这里有一个如何在Swift中实现这一点的例子..。

代码语言:javascript
复制
// 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处理程序方法

代码语言:javascript
复制
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)
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26591500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档