首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SKPhysicsJoint使旋转不能正常工作

SKPhysicsJoint使旋转不能正常工作
EN

Stack Overflow用户
提问于 2015-08-15 19:02:24
回答 1查看 191关注 0票数 2

使用Swift和Sprite-Kit,我试图在一个名为"pin“的静态SKSpriteNode的位置和用户触摸屏幕的位置之间创建一条具有真实物理意义的绳子。为此,我要添加名为SKSpriteNodes的单个ropeNodes,并将它们与一系列SKPhysicsJointPins链接起来。物理工作得很好,然而,当我试图旋转每个独立的部分,使它们正确地定向时,ropeNodes不再形成一条直线,它们也不会旋转到正确的角度。但是,当我移除SKPhysicsJoints时,旋转就像每个单独的节点一样工作。为每一个人在anchorPoint上走动,ropeNode似乎只会使事情变得更糟。为什么会发生这种情况,我怎么才能着手解决它呢?预先谢谢(:

代码语言:javascript
复制
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        dx = pin.position.x - location.x
        dy = pin.position.y - location.y
        let length = sqrt(pow(dx!, 2) + pow(dy!, 2))
        let distanceBetweenRopeNodes = 40
        let numberOfPieces = Int(length)/distanceBetweenRopeNodes
        var ropeNodes = [SKSpriteNode]()
        
        //adds the pieces to the array and the scene at respective locations
        for var index = 0; index < numberOfPieces; ++index{
            let point = CGPoint(x: pin.position.x + CGFloat((index) * distanceBetweenRopeNodes) * sin(atan2(dy!, -dx!) + 1.5707), y: pin.position.y + CGFloat((index) * distanceBetweenRopeNodes) * cos(atan2(dy!, -dx!) + 1.5707))
            let piece = createRopeNode(point)
            piece.runAction(SKAction.rotateByAngle(atan2(-dx!, dy!), duration: 0))
            ropeNodes.append(piece)
            self.addChild(ropeNodes[index])
        }

        //Adds an SKPhysicsJointPin between each pair of ropeNodes
        self.physicsWorld.addJoint(SKPhysicsJointPin.jointWithBodyA(ropeNodes[0].physicsBody, bodyB: pin.physicsBody, anchor:
            CGPoint(x: (ropeNodes[0].position.x + pin.position.x)/2, y: (ropeNodes[0].position.y + pin.position.y)/2)))
        for var i = 1; i < ropeNodes.count; ++i{
            let nodeA = ropeNodes[i - 1]
            let nodeB = ropeNodes[i]
            let middlePoint = CGPoint(x: (nodeA.position.x + nodeB.position.x)/2, y: (nodeA.position.y + nodeB.position.y)/2)
            let joint = SKPhysicsJointPin.jointWithBodyA(nodeA.physicsBody, bodyB: nodeB.physicsBody, anchor: middlePoint)
            self.physicsWorld.addJoint(joint)
        }
    }
}

func createRopeNode(location: CGPoint) -> SKSpriteNode{
    let ropeNode = SKSpriteNode(imageNamed: "RopeTexture")
    ropeNode.physicsBody = SKPhysicsBody(rectangleOfSize: ropeNode.size)
    ropeNode.physicsBody?.affectedByGravity = false
    ropeNode.physicsBody?.collisionBitMask = 0
    ropeNode.position = location
    ropeNode.name = "RopePiece"
    return ropeNode
}

这是一个图像,当我试图旋转每个单独的ropeNode时会发生什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-16 01:15:14

经过一些思考后,我认为将表示节点作为子节点添加是一个更好的主意,因为您不需要在单独的数组中跟踪额外的节点。若要设置子对象的旋转,只需展开父级的旋转,然后添加新的旋转角:

代码语言:javascript
复制
node.zRotation = -node.parent!.zRotation + newRotation

如果绳子段位于容器SKNode中,则可以通过以下方法对它们进行迭代

代码语言:javascript
复制
for rope in ropes.children {
    if let node = rope.children.first {
        let newRotation = ...
        node.zRotation = -rope.zRotation + newRotation
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32028267

复制
相关文章

相似问题

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