Build Wind Zones, Vortices, and Gravity Wells in SpriteKit
Let the same level push a glider sideways, curl debris around a vortex, and pull objects into a gravity well.
This technique uses SpriteKit’s SKFieldNode and works in a plain SKScene. Start with a physics-driven object and one visible wind corridor.
Push only the bodies that should feel the wind
Give the field a region and a category bit. Put that bit in the glider’s fieldBitMask. Set the masks explicitly on bodies that should pass through unaffected. Field masks are separate from collision and contact masks.
import SpriteKit
import simd
@MainActor
func addWindLane(to scene: SKScene, glider: SKSpriteNode) {
let windBit: UInt32 = 1 << 0
let wind = SKFieldNode.linearGravityField(withVector: SIMD3<Float>(1, 0, 0))
wind.position = CGPoint(x: 280, y: 180)
wind.region = SKRegion(size: CGSize(width: 160, height: 280))
wind.categoryBitMask = windBit
wind.strength = 0.8
scene.addChild(wind)
let body = SKPhysicsBody(rectangleOf: glider.size)
body.affectedByGravity = false
body.fieldBitMask = windBit
glider.physicsBody = body
}
Add the glider to the scene and steer with forces or impulses. If your controller overwrites physicsBody.velocity every frame, it can erase the wind’s effect. Skypirates moves its plane by setting position, so a field-driven version would give the plane’s physics body control of translation.
Turn the corridor into a hazard or shortcut
- Use
vortexField()to deflect debris around a center. Try it on a projectile for a shot the player must lead. - Use
radialGravityField()to draw bodies inward. Tune its strength and falloff so players can escape near the edge. - Give an emitter a matching
fieldBitMaskto let its particles reveal the flow.
To try the vortex variant, start in addWindLane(to:glider:) above. Replace the field constructor with the following two lines. Keep the existing region and mask, then give two test bodies the same initial impulse. Assign the field bit to only one and compare their paths.
let wind = SKFieldNode.vortexField()
wind.falloff = 1
Test with two otherwise identical bodies, with only one subscribed to the field. Send them through the middle, then near the boundary. A clear visual marker helps players predict when the course will push them sideways.