我怎样才能模拟收缩和拉伸绳索 - 斯威夫特

时间:2017-11-01 15:54:02

标签: ios swift sprite-kit game-physics

我如何模拟收缩和拉伸绳索,我想联合两个精灵节点,当越来越近时,绳索应该在它们移动到原始位置后拉伸和缩小,我使用极限和弹簧使用了很多方法关节,但我真的不知道如何动画绳索来完成收缩和拉伸。

尝试1

override func didMove(to view: SKView) {
    self.physicsWorld.contactDelegate = self
    self.addAnchor()
    self.addRope()
    self.addCharacter()

    let jointOneFixed = SKPhysicsJointFixed.joint(withBodyA: anchor.physicsBody!, bodyB: rope.physicsBody!, anchor: anchor.position)
    self.physicsWorld.add(jointOneFixed)

    let jointTwoFixed = SKPhysicsJointFixed.joint(withBodyA: rope.physicsBody!, bodyB: currentCharacter.physicsBody!, anchor: CGPoint(x: rope.frame.midX, y: rope.frame.minY))
    self.physicsWorld.add(jointTwoFixed)

    limitJoint = SKPhysicsJointLimit.joint(withBodyA: anchor.physicsBody!, bodyB: currentCharacter.physicsBody!, anchorA: anchor.position, anchorB: currentCharacter.position)
    //limitJoint.maxLength = self.size.height - 1000
    self.physicsWorld.add(limitJoint)
 }

 func addAnchor(){
    anchor.position = CGPoint(x: self.size.width / 2, y: self.size.height + 1)
    anchor.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    anchor.setScale(1)
    anchor.zPosition = 2
    anchor.name = "anchor"
    anchor.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: anchor.size.width, height: anchor.size.height))
    anchor.physicsBody?.isDynamic = false
    anchor.physicsBody?.affectedByGravity = false
    anchor.physicsBody?.allowsRotation = true
    self.addChild(anchor)
}

func addRope() {
    rope.position = CGPoint(x: anchor.position.x, y: anchor.position.y - 175)
    rope.setScale(0.65)
    rope.zPosition = 1
    rope.name = "rope"
    rope.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: rope.size.width, height: rope.size.height))
    rope.physicsBody?.affectedByGravity = false;
    rope.physicsBody?.mass = 0.5;
    rope.physicsBody?.allowsRotation = true
    self.addChild(rope)
}

func addCharacter() {
    let characterName: String = UserDefaults.standard.string(forKey: "current_character")!
    currentCharacter = SKSpriteNode(imageNamed: characterName);
    currentCharacter.position = CGPoint(x: self.size.width / 2, y: self.size.height - 400)
    currentCharacter.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    currentCharacter.setScale(0.50)
    currentCharacter.zPosition = 2
    currentCharacter.name = "character"
    currentCharacter.physicsBody = SKPhysicsBody(circleOfRadius: max(currentCharacter.size.width / 2, currentCharacter.size.height / 2))
    currentCharacter.physicsBody?.affectedByGravity = false
    currentCharacter.physicsBody?.isDynamic = true
    currentCharacter.physicsBody?.allowsRotation = true

    self.addChild(currentCharacter)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let moveDown = SKAction.moveBy(x: 0, y: -100, duration:0.1)
    currentCharacter.run(moveDown)
}

尝试2 几乎相同,除了创建一个极限关节,我创建了一个Spring关节

override func didMove(to view: SKView) {
    self.physicsWorld.contactDelegate = self
    self.addAnchor()
    self.addRope()
    self.addCharacter()

    let jointOneFixed = SKPhysicsJointFixed.joint(withBodyA: anchor.physicsBody!, bodyB: rope.physicsBody!, anchor: anchor.position)
    self.physicsWorld.add(jointOneFixed)

    let jointTwoFixed = SKPhysicsJointFixed.joint(withBodyA: rope.physicsBody!, bodyB: currentCharacter.physicsBody!, anchor: CGPoint(x: rope.frame.midX, y: rope.frame.minY))
    self.physicsWorld.add(jointTwoFixed)

    let springJoint = SKPhysicsJointSpring.joint(withBodyA: anchor.physicsBody!, bodyB: currentCharacter.physicsBody!, anchorA: anchor.position, anchorB: currentCharacter.position)
    //springJoint.frequency = 1.0
    //springJoint.damping = 0
    self.physicsWorld.add(springJoint)

}    

0 个答案:

没有答案