如何将物理体赋予弧线?

How to give a physics body to an arc?

我有两条弧线组成一个圆圈(SKCropNode),每条弧线都有不同的颜色。我知道我可以给整个圆一个 circleOfRadius,但我想知道是否可以只给一个圆弧一个物理体来适应圆弧的形状。

非常感谢帮助。

您可能希望避免使用 SKCropNode 来构建弧线。来自 Apple 的文档,

Use clipping and effect nodes sparingly. Both are very powerful, but can be expensive, especially when nested together within the node tree.

或者,您可以构建一个弧形核心图形路径,然后从该路径创建一个形状节点。然后您可以使用 CG 路径创建物理体。

要构建弧形路径,

  1. 从起始角到结束角添加一条内弧
  2. 从内弧的终点到外弧的终点加一条线
  3. 从结束角到起始角加上外弧
  4. 关闭路径(连接圆弧起点)

扩展CGPath构建圆弧路径不是必须的,但通常使用起来更方便。扩展后,新的 class 方法可以从代码中的任何位置调用。这是一个例子:

extension CGPath {
    static func arcWithWidth(arcWidth:CGFloat, start:CGFloat, end:CGFloat, radius:CGFloat, clockwise:Bool) -> CGPath {
        // The radius parameter specifies the middle of the arc; adjust this as needed
        let innerRadius:CGFloat = radius - arcWidth / 2.0
        let outerRadius:CGFloat = radius + arcWidth / 2.0

        // Note the arc is upside down because CGPath uses UIKit coordinates
        let path = UIBezierPath()
        // Add inner ring.
        path.addArcWithCenter(CGPointZero, radius: innerRadius, startAngle: start, endAngle: end, clockwise: clockwise)
        let x = outerRadius * cos(end)
        let y = outerRadius * sin(end)

        // Connect the inner to the outer ring
        path.addLineToPoint(CGPointMake(x, y))

        // Add outer ring
        path.addArcWithCenter(CGPointZero, radius: outerRadius, startAngle: end, endAngle: start, clockwise: !clockwise)

        path.closePath()

        return path.CGPath
    }
}

使用扩展,您可以创建顶部和底部弧线:

    // Top arc
    var path = CGPath.arcWithWidth(20, start:0, end: CGFloat(M_PI), radius: 100, clockwise: true)

    let topArc = SKShapeNode(path: path)
    topArc.position = view.center
    topArc.fillColor = SKColor.redColor()
    topArc.strokeColor = SKColor.clearColor()

    // Add a physics body to the top half
    topArc.physicsBody = SKPhysicsBody(polygonFromPath: path)
    topArc.physicsBody?.affectedByGravity = false

    addChild(topArc)

    // Bottom arc
    path = CGPath.arcWithWidth(20, start:0, end: CGFloat(M_PI), radius: 100, clockwise: false)
    let bottomArc = SKShapeNode(path: path)
    bottomArc.position = view.center
    bottomArc.fillColor = SKColor.blueColor()
    bottomArc.strokeColor = SKColor.clearColor()

    addChild(bottomArc)