The Many Uses of SKConstraints

2016.04.12

At WWDC 2014, SpriteKit introduced SKConstraints: a really nifty feature that is akin to LookAt constraints in SceneKit. There are three main types of constraints: rotation, distance, and position. Constraints consist of two pieces: its type and an SKRange designating how much leeway the constraint has.

Here’s a quick example that shows how to create a node (followerSprite) follow another node (leaderSprite):

//create a range to designate how far of a
//distance to follow from.
let distanceRange = SKRange(lowerLimit: 100.0,
                            upperLimit: 150.0)

//create a range to orient itself to the other node
let orientationRange = SKRange(constantValue: CGFloat(M_2_PI*7))

//create the constraints
let distanceConstraint
= SKConstraint.distance(distanceRange, toNode: leaderSprite)

let orientConstraint   
= SKConstraint.orientToNode(leaderSprite, offset: orientationRange)

//add them to the follow sprite
followerSprite.constraints = [orientConstraint,
                              distanceConstraint]

    //the constraints will be applied in the order
    //they are placed inside the array. in this case,
    //the follower will orient to the leader before the check
    //for distance. this can cause some adverse effects,
    //so be wary of the order.

Note that the constraints are applied after the physics simulation is run, which is great because the constraints will then always be followed. Here are links to the SKConstraint class reference and the SKRange class reference; there are multiple inits for each, so there is a lot of flexibility within. Ray Wenderlich has a great tutorial on inverse kinematics with constraints through the scene editor in Xcode – check it out if you’d like to learn more.