1

I am trying to work with SpriteKit and create an elbow joint that bends to allow movement of another joint. The problem is that when I create the joint, the parts all rotate 360 degrees as I swipe across the screen to action the movement, so there is no fixed joint that is kept (i.e. an elbow that has a range of movement). I have attached some of my code - some parts are commented out as I've been experimenting.

var lowerTorso: SKNode!
var upperTorso: SKNode!
var upperArmFront: SKNode!
var lowerArmFront: SKNode!
var upperArmBack: SKNode!
var lowerArmBack: SKNode!
var fistFront: SKNode!
var cricketBat: SKNode!
var bottom: SKNode!

let upperArmAngleDeg: CGFloat = 10
let lowerArmAngleDeg: CGFloat = 150
let batAngleDeg: CGFloat = 270

override func didMove(to view: SKView) {

physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector(dx: 0, dy: -20)

physicsWorld.speed = 0.1
//2
lowerTorso = childNode(withName: "torso_lower")

upperTorso = lowerTorso.childNode(withName: "torso_upper")
upperArmFront = upperTorso.childNode(withName: "arm_upper_front")
lowerArmFront = upperArmFront.childNode(withName: "arm_lower_front")

upperArmBack = upperArmFront.childNode(withName: "arm_upper_back")
lowerArmBack = upperArmBack.childNode(withName: "arm_lower_back")

cricketBat = lowerArmFront.childNode(withName: "cricketBat")
fistFront = cricketBat.childNode(withName: "fist_front")

let joint = SKPhysicsJointPin.joint(withBodyA: upperArmFront.physicsBody!,
                                    bodyB: lowerArmFront.physicsBody!,
                                    anchor: CGPoint(x: upperArmFront.frame.midX, y: upperArmFront.frame.midY))
physicsWorld.add(joint)


let joint2 = SKPhysicsJointPin.joint(withBodyA: lowerArmFront.physicsBody!,
                                    bodyB: cricketBat.physicsBody!,
                                    anchor: CGPoint(x: lowerArmFront.frame.maxX, y: lowerArmFront.frame.maxY))
physicsWorld.add(joint2)

/*
lowerArmFront.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(50).degreesToRadians(), upperAngleLimit: 160)
upperArmFront.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(-90).degreesToRadians(), upperAngleLimit: CGFloat(120).degreesToRadians())

lowerArmBack.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(50).degreesToRadians(), upperAngleLimit: 160)
upperArmBack.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(0).degreesToRadians(), upperAngleLimit: CGFloat(10).degreesToRadians())


*/

 }

func punchAt(_ location: CGPoint) {
    // 1
    let punch = SKAction.reach(to: location, rootNode: upperArmFront, duration: 0.1)

    /*
    // 2
    let restore = SKAction.run {
        self.upperArmFront.run(SKAction.rotate(toAngle: self.upperArmAngleDeg.degreesToRadians(), duration: 0.1))
        self.lowerArmFront.run(SKAction.rotate(toAngle: self.lowerArmAngleDeg.degreesToRadians(), duration: 0.1))
        self.cricketBat.run(SKAction.rotate(toAngle: self.batAngleDeg.degreesToRadians(), duration: 0.1))
    }
    */
    fistFront.run(punch)

    // 3
    //fistFront.run(SKAction.sequence([punch, restore]))
}

// 3
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    touched = true
    for touch: AnyObject in touches {
        location = touch.location(in: self)

    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
       location = touch.location(in: self)
    }
}
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
Prateek
  • 1,724
  • 3
  • 15
  • 27

1 Answers1

0

According to the documentation here: https://developer.apple.com/reference/spritekit/skphysicsjointpin

you can set rotation limits on the pin joint via the joint's properties:

var shouldEnableLimits: Bool A Boolean value that indicates whether the pin joint’s rotation is limited to a specific range of values.

var lowerAngleLimit: CGFloat The smallest angle allowed for the pin joint, in radians.

var upperAngleLimit: CGFloat The largest angle allowed for the pin joint, in radians.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55