Search code examples
swiftmacossprite-kitskshapenode

SKShapeNode Strokes show gaps when drawn with CGPath


I am trying to draw a shape using CGPath and SKShapeNode. However, whatever the lineJoin or lineCap combinations I try to use, the lines have gaps.

enter image description here

import PlaygroundSupport
import SpriteKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        super.didMove(to: view)
        let path = CGMutablePath()
        path.move(to: NSPoint(x: -66.585, y: 1.125))
        path.addCurve(to:  NSPoint(x: -60.585, y: -41.765), control1: NSPoint(x: -66.585, y: 1.125), control2: NSPoint(x: -65.835, y: -32.735))
        path.addCurve(to:  NSPoint(x: 66.585, y: -20.3150000000001), control1: NSPoint(x: -60.585, y: -41.765), control2: NSPoint(x: -3.39500000000001, y: -1.88499999999999))
        path.addCurve(to:  NSPoint(x: 56.545, y: 18.235), control1: NSPoint(x: 66.125, y: -7.09500000000003), control2: NSPoint(x: 62.695, y: 6.16499999999996))
        path.addCurve(to:  NSPoint(x: -66.585, y: 1.125), control1: NSPoint(x: 56.055, y: 19.1849999999999), control2: NSPoint(x: -15.415, y: 41.765))
        path.closeSubpath()
        let shapeNode = SKShapeNode(path: path)
        shapeNode.fillColor = .red
        shapeNode.lineWidth = 10
        shapeNode.lineJoin = .round
        shapeNode.strokeColor = .white
        addChild(shapeNode)
    }
}

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
if let scene = GameScene(fileNamed: "GameScene") {
    scene.scaleMode = .aspectFill
    sceneView.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

What am I doing wrong here?


Solution

  • This looks odd to me:

        path.move(to: NSPoint(x: -66.585, y: 1.125))
        path.addCurve(to:  NSPoint(x: -60.585, y: -41.765), control1: NSPoint(x: -66.585, y: 1.125), control2: NSPoint(x: -65.835, y: -32.735))
    

    You move to (-66.58., 1.125). Then you add a curve where the first point is (-60.585, -41.765), and the first control point is the same as the original point you moved to. It seems like that will cause some odd issues. I'd think you'd want to have the point you move to be the first point of the curve, not its control point.