SKPhysicsJointPin - nodes getting stuck and joints stretching

115 Views Asked by At

I have a couple of issues here and they may or may not be related. I have a character who is a climber and it has a torso, left arm, right arm, left hand, and right hand. Each hand has a SKPhysicsJointPin to the arm and each arm has an SKPhysicsJointPin to the torso. All but the two hands are affected by gravity so the body hangs from the hands. I use an SKAction to move each hand up and pull the body up. One hand is locked while the other moves freely. The locked hand will represent "holding" on to a rock. I initially used rectangles to test this out. The joints would start to pull apart whenever I "reached up" too far. To fix this, I limited how far it could reach up at a time to keep the joints together. I uploaded some images to replace the rectangles and they use alpha masks to map their real shape. Now when I test, the torso will get stuck as soon as it swings a little left. The joints will start to stretch and the hands and arms start to become separated. Sometimes it clear all this and recollect itself but all the joints still pull apart.

Why would there be such a change when switching from rectangles to images? Is this a bug? How can I use these joints in a way that they will not pull apart like this? I have messed around with all the settings including removing all the constraints and whatnot but still the same issue.

        torso = (self.childNode(withName: "yeti_torso_1") as? SKSpriteNode)!
        //torso.physicsBody = SKPhysicsBody(texture: torso.texture!,size: torso.texture!.size())
        torso.physicsBody?.isDynamic = true
        torso.physicsBody?.usesPreciseCollisionDetection = true
        torso.physicsBody?.collisionBitMask = noCategory
        let constraint = SKConstraint.zRotation(SKRange(lowerLimit: degreesToRadians(degrees: -30.0), upperLimit: degreesToRadians(degrees: 30.0)))
        torso.constraints = [constraint]

        leftHand = (self.childNode(withName: "yeti_left_hand_regular") as? SKSpriteNode)!
        leftHand.physicsBody?.collisionBitMask = noCategory


        leftArm = (self.childNode(withName: "yeti_left_arm") as? SKSpriteNode)!
        leftArm.physicsBody?.collisionBitMask = noCategory
        leftArm.zPosition = 2

        rightHand = (self.childNode(withName: "yeti_right_hand_regular") as? SKSpriteNode)!
        rightHand.physicsBody?.collisionBitMask = noCategory


        rightArm = (self.childNode(withName: "yeti_right_arm") as? SKSpriteNode)!
        rightArm.physicsBody?.collisionBitMask = noCategory
        rightArm.zPosition = 2

         //Left Wrist Joint
         let leftWristJoint = SKPhysicsJointPin.joint(
           withBodyA: leftHand.physicsBody!,
           bodyB: leftArm.physicsBody!,
           anchor: CGPoint(
             x: leftHand.frame.midX-10,
             y: leftHand.frame.midY-10))
         leftWristJoint.shouldEnableLimits = true
         leftWristJoint.lowerAngleLimit = degreesToRadians(degrees: 0.0)
         leftWristJoint.upperAngleLimit = degreesToRadians(degrees: 90.0)
         leftWristJoint.frictionTorque = 1


         self.physicsWorld.add(leftWristJoint)


        //Right Wrist Joint
        let rightWristJoint = SKPhysicsJointPin.joint(
          withBodyA: rightHand.physicsBody!,
          bodyB: rightArm.physicsBody!,
          anchor: CGPoint(
            x: rightHand.frame.midX+10,
            y: rightHand.frame.midY-10))

        rightWristJoint.shouldEnableLimits = true
        rightWristJoint.lowerAngleLimit = degreesToRadians(degrees: 0.0)
        rightWristJoint.upperAngleLimit = degreesToRadians(degrees: 90.0)
        rightWristJoint.frictionTorque = 1

        self.physicsWorld.add(rightWristJoint)

        //Left Arm Joint
          let leftArmJoint = SKPhysicsJointPin.joint(
            withBodyA: torso.physicsBody!,
            bodyB: leftArm.physicsBody!,
            anchor: CGPoint(
              x: torso.frame.minX+100,
              y: torso.frame.maxY-110))
          leftArmJoint.shouldEnableLimits = true
         // leftArmJoint.lowerAngleLimit = degreesToRadians(degrees: -145.0)
          //leftArmJoint.upperAngleLimit = degreesToRadians(degrees: 80.0)
          leftArmJoint.frictionTorque = 1
          self.physicsWorld.add(leftArmJoint)

          //Right Arm Joint
          let rightArmJoint = SKPhysicsJointPin.joint(
            withBodyA: torso.physicsBody!,
            bodyB: rightArm.physicsBody!,
            anchor: CGPoint(
              x: torso.frame.maxX-50,
              y: torso.frame.maxY-110))
          rightArmJoint.shouldEnableLimits = true
          //rightArmJoint.lowerAngleLimit = degreesToRadians(degrees: -80.0)
         // rightArmJoint.upperAngleLimit = degreesToRadians(degrees: 145.0)
          rightArmJoint.frictionTorque = 1

          self.physicsWorld.add(rightArmJoint)

The hand is moved by dragging on the screen to pull hand up

        let td = CGVect(touchBegan,touchCurrent)
        let move = SKAction.move(by: td, duration: 0)
        hand.run(move)

SKPhysicsJointPin stretching

0

There are 0 best solutions below