swift - 如何将物理体赋予弧线?

标签 swift sprite-kit skphysicsbody

我有两条弧形成一个圆(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来构造圆弧路径不是必须的,但通常使用起来更方便。扩展后,可以从代码中的任何位置调用新的类方法。这是一个例子:

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)

关于swift - 如何将物理体赋予弧线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35161824/

相关文章:

swift - 我如何返回到 Main.storyboard,其中包含来自另一个 Storyboard 的 TabBarController

ios - 如何在没有 segue 的情况下管理多 View 应用程序

swift - collisionBitMask 不起作用

ios - 如何防止 SKSpriteNode 的子节点与其父节点一起旋转?

ios - SKPhysicsContactDelegate 协议(protocol)方法未被调用

swift - 未检测到 SpriteKit 碰撞

ios - 我如何正确使用 mapbox 的新 MGLOfflinePackDelegate?

swift - 如何在场景转换尝试中处理 "no scene"?

ios - 如何解决 CGPath 性能不佳的问题? [iOS 8/SpriteKit 中的 Curve Fever 克隆]

swift - 从 Swift 调用 C++ - 什么是 std::vector<T> 的等价物