0

is there a trick to show a GKPath?

for example my path:

class ScenarioPath: GKPath {

    //SET
    //   |  |  |  |  |  |
    // --A--B--C--D--E--F--
    //   |  |  |  |  |  |
    //6 punti con raggio quasi 600 e lato 500 va bene per scenario 2K * 3K
    static let lato = Float(500.0)
    static let maxY = Float(0.0)
    static let radius : Float = 600.0

    static let maxYNormalize = maxY - radius

    static let pts = [
        vector_float2(-lato,maxYNormalize),
        vector_float2(+lato,maxYNormalize),
        vector_float2(-2*lato,maxYNormalize),
        vector_float2(+2*lato,maxYNormalize),
        vector_float2(-3*lato,maxYNormalize),
        vector_float2(+3*lato,maxYNormalize)]


    init () {
        super.init(points: UnsafeMutablePointer(ScenarioPath.pts), count: ScenarioPath.pts.count, radius: ScenarioPath.radius, cyclical: true)
    }


}

I'm looking for a simple func to show this path. Thanks

Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30

1 Answers1

1

GameplayKit is not a graphics framework. It's a low-level toolkit you can use with any graphics framework. It doesn't have any features for drawing GKPaths because there are at least as many ways of interpreting a path as there are graphics toolkits and coordinate systems to do it with.

Since you're working with SpriteKit, it's not too hard to make a CGPath using the same set of points as your GKPath, then assign that to an SKShapeNode to place in your scene.

You can find Apple sample code that does just that inside two of their larger demo projects: AgentsCatalog and DemoBots.

Here's a bit to get you started:

// BTW, vector_float2 just a typealias for float2,
// and is ArrayLiteralConvertible, so you can write your array shorter:
let pts: [float2] = [
    [-lato,maxYNormalize],
    [+lato,maxYNormalize],
    // ...
]

// CGPoint extension to make the conversion more readable and reusable
extension CGPoint {
    init(_ vector: float2) {
        self.init(x: CGFloat(vector.x), y: CGFloat(vector.y))
    }
}

// make array of CGPoints from array of float2
let cgPoints = pts.map(CGPoint.init)
// use that array to create a shape node
let node = SKShapeNode(points: UnsafeMutablePointer(cgPoints), count: cgPoints.count)

This, of course, assumes that your path is specified in the same coordinate system as your scene. If not you'll need some transformations.

rickster
  • 124,678
  • 26
  • 272
  • 326