A joint that simulates a spring connecting two physics bodies.


class SKPhysicsJointSpring : SKPhysicsJoint


An SKPhysicsJointSpring object simulates connecting two physics bodies together with a spring. The farther the two objects move from each other, the more force is applied to bring the two bodies back together.

Listing 1 shows how you can create a spring joint between sprite nodes. The physics body of staticNode has its isDynamic property set to false, preventing it from being affected by gravity. It is animated with an SKAction to move it upwards.

dynamicNode is joined to staticNode with an SKPhysicsJointSpring named spring, with a frequency of 0.5 and a damping of 0.2.

The final result is that as staticNode moves vertically, dynamicNode follows the upward path with a slight delay and bounce.

Listing 1

Joining two sprite nodes with a spring

let scene = SKScene()
let size = CGSize(width: 50, height: 50)
let staticNode = SKSpriteNode(color: .red,
                              size: size)
let dynamicNode = SKSpriteNode(color: .blue,
                               size: size)
staticNode.physicsBody = SKPhysicsBody(rectangleOf: size)
staticNode.physicsBody?.isDynamic = false
staticNode.position = CGPoint(x: 250, y: 300)
dynamicNode.physicsBody = SKPhysicsBody(rectangleOf: size)
dynamicNode.position = CGPoint(x: 250, y: 200)
let spring = SKPhysicsJointSpring.joint(withBodyA: staticNode.physicsBody!,
                                        bodyB: dynamicNode.physicsBody!,
                                        anchorA: staticNode.position,
                                        anchorB: dynamicNode.position)
spring.frequency = 0.5
spring.damping = 0.2
let move = SKAction.moveBy(x:0, y: 200,
                           duration: 1)


Configuring the Spring Properties

var damping: CGFloat

Defines how the spring’s motion should be damped due to the forces of friction.

var frequency: CGFloat

Defines the frequency or stiffness characteristics of the spring.


Inherits From

Conforms To

See Also

Simulating Physics

class SKPhysicsWorld

An object which encapsulates a scene's physics simulation.

class SKPhysicsBody

An object which adds physics simulation to a node.

class SKPhysicsContact

A description of the contact between two physics bodies.

protocol SKPhysicsContactDelegate

Methods your app can implement to respond when physics bodies come into contact.

class SKPhysicsJoint

The abstract superclass for objects that connect physics bodies.

class SKPhysicsJointFixed

A joint that fixes two physics bodies together.

class SKPhysicsJointLimit

A joint that imposes a maximum distance between two physics bodies, as if they were connected by a rope.

class SKPhysicsJointPin

A joint that pins together two physics bodies, allowing independent rotation.

class SKPhysicsJointSliding

A joint that allows two physics bodies to slide along an axis.

class SKFieldNode

A node that applies physics effects to a portion of the scene.

class SKRegion

A definition of an arbitrary area.