All articles

Advanced

Make Water Ripple and Portals Distort with SpriteKit

Shift a few points on a SpriteKit warp grid to create moving water or make a portal pulse before it opens.

Warp geometry changes the shape of artwork at render time. Begin with a single sprite and a small grid so you can see what moving each point does.

Animate a distortion and return to rest

A grid with two rows and two columns has nine vertices. This example generates a regular grid, moves the center vertex horizontally, and keeps the left and right edges pinned. Start from an unwarped grid so SpriteKit has a shape to interpolate from.

import SpriteKit
import simd

@MainActor
func animateRipple(on sprite: SKSpriteNode) {
    let source = (0...2).flatMap { row in
        (0...2).map { column in
            SIMD2<Float>(Float(column) / 2, Float(row) / 2)
        }
    }
    var destination = source
    destination[4].x += 0.12

    let rest = SKWarpGeometryGrid(columns: 2, rows: 2,
                                  sourcePositions: source,
                                  destinationPositions: source)
    let ripple = SKWarpGeometryGrid(columns: 2, rows: 2,
                                    sourcePositions: source,
                                    destinationPositions: destination)
    sprite.warpGeometry = rest
    guard let bend = SKAction.warp(to: ripple, duration: 0.35),
          let settle = SKAction.warp(to: rest, duration: 0.35) else { return }
    sprite.run(.repeatForever(.sequence([bend, settle])), withKey: "ripple")
}

Keep the borders fixed for a tile that must meet its neighbors. For a portal, vary several interior points and run the action once when the player approaches. Use a texture with visible detail so the distortion reads on screen.

Separate the artwork from the trigger

Put the portal sensor on a stable node and the warping sprite on a child. The physics body and contact masks determine what the hero can touch. Use the portal’s game state to decide whether a contact should open the gate or transport the hero. For water, the same setup can keep the swim region fixed while the surface shimmers.

To try a portal, attach the sprite to a sensor node and run the warp example on art. In your scene’s contact delegate, check whether one body belongs to the hero and the other belongs to portal. Begin with a log message before wiring up a teleport.

let portal = SKNode()
portal.position = CGPoint(x: 240, y: 120)
portal.addChild(art) // A new sprite centered at .zero
let sensor = SKPhysicsBody(rectangleOf: art.size)
sensor.isDynamic = false
sensor.categoryBitMask = portalCategory
sensor.collisionBitMask = 0
sensor.contactTestBitMask = heroCategory
portal.physicsBody = sensor
scene.addChild(portal)

Use distinct category bits for portalCategory and heroCategory. The hero needs a dynamic body in the hero category for the contact test. Walk through the portal while the art is bent and again at rest. The contact boundary should stay in the same place.

Enable showsPhysics in the SpriteKit view and check the trigger at both ends of the animation. If you want to warp an emitter or shape node, place it inside an SKEffectNode and apply the warp there.

Apple’s SpriteKit warp animation guide