Apple's SpriteKit game framework allows the developer to use different kinds of physics fields through SKFieldNode
. To visualize these fields, SKView
has a showsFields
property, but unfortunately it is not customizable and doesn't always work properly.
This package was created to easily add field visualization to a Swift project, inspired by the code used in the Field Control Game.
SpriteKit fields are not simulated/sampled correctly if the scene has no nodes to interact with them. This means that the package will not work properly if there are only fields in the scene. That's also why the example project spawns balls with charges in new touch and does not contain only fields that follow the player's touches.
The ShowFieldsExample
project provides an example on how to add the package components to your SpriteKit project.
-
Add a
DrawLinesView
to your view hierarchy behind yourSKView
, as seen inShowFieldsExample/GameView.swift
. -
Add a
FieldSampler
to yourSKScene
. A reference to theDrawLinesView
must be passed in the sampler initializer so that it can tell the view what to draw. -
Call
updateLines()
in your update loop (typically in your scene'supdate(_:)
method).
To measure the scene's fields, the FieldSampler
uses the sampleFields(at:)
method of SKPhysicsWorld. The field is sampled in several points in the scene and then translated into lines to be drawn by DrawLinesView
in UIKit in every cycle.
During the first implementation of this idea, attempts to draw the lines with SpriteKit were made, but the performance was a huge issue.