自定义物理体添加不需要的额外行

Custom physics body adding unwanted extra line

我制作了一个包含三个自定义物理体的节点,但问题是每个物理体都有一条额外的线。

我可以删除这些行吗?我要删除的行用红色圈起来。

我用this editor制作了物理体。

使用我创建此代码的路径(swift & sprite kit):

//Wheel
    let wheel = SKSpriteNode(imageNamed: "wheel")
    let scaleWheel = frame.width / wheel.frame.width * 0.8
    wheel.name = "wheel"
    wheel.position = view.center

    //Scale constants
    let sprite = SKSpriteNode()
    sprite.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame))
    let offsetX: CGFloat = sprite.frame.size.width * sprite.anchorPoint.x
    let offsetY: CGFloat = sprite.frame.size.height * sprite.anchorPoint.y

    //path
    let path: CGMutablePathRef = CGPathCreateMutable()

    CGPathMoveToPoint(path, nil, -172 - offsetX, 143 - offsetY)
    CGPathAddLineToPoint(path, nil, -151 - offsetX, 122 - offsetY)
    CGPathAddLineToPoint(path, nil, -167 - offsetX, 100 - offsetY)
    CGPathAddLineToPoint(path, nil, -178 - offsetX, 81 - offsetY)
    CGPathAddLineToPoint(path, nil, -186 - offsetX, 60 - offsetY)
    CGPathAddLineToPoint(path, nil, -191 - offsetX, 38 - offsetY)
    CGPathAddLineToPoint(path, nil, -193 - offsetX, 16 - offsetY)
    CGPathAddLineToPoint(path, nil, -193 - offsetX, -7 - offsetY)
    CGPathAddLineToPoint(path, nil, -193 - offsetX, -25 - offsetY)
    CGPathAddLineToPoint(path, nil, -188 - offsetX, -47 - offsetY)
    CGPathAddLineToPoint(path, nil, -182 - offsetX, -69 - offsetY)
    CGPathAddLineToPoint(path, nil, -173 - offsetX, -88 - offsetY)
    CGPathAddLineToPoint(path, nil, -164 - offsetX, -105 - offsetY)
    CGPathAddLineToPoint(path, nil, -151 - offsetX, -124 - offsetY)
    CGPathAddLineToPoint(path, nil, -134 - offsetX, -143 - offsetY)
    CGPathAddLineToPoint(path, nil, -150 - offsetX, -168 - offsetY)
    CGPathAddLineToPoint(path, nil, -170 - offsetX, -147 - offsetY)
    CGPathAddLineToPoint(path, nil, -190 - offsetX, -121 - offsetY)
    CGPathAddLineToPoint(path, nil, -207 - offsetX, -89 - offsetY)
    CGPathAddLineToPoint(path, nil, -218 - offsetX, -58 - offsetY)
    CGPathAddLineToPoint(path, nil, -224 - offsetX, -23 - offsetY)
    CGPathAddLineToPoint(path, nil, -224 - offsetX, 25 - offsetY)
    CGPathAddLineToPoint(path, nil, -216 - offsetX, 62 - offsetY)
    CGPathAddLineToPoint(path, nil, -208 - offsetX, 85 - offsetY)
    CGPathAddLineToPoint(path, nil, -192 - offsetX, 116 - offsetY)

    CGPathCloseSubpath(path)

    //pathTwo
    let pathTwo: CGMutablePathRef = CGPathCreateMutable()

    CGPathMoveToPoint(pathTwo, nil, -93 - offsetX, 170 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -117 - offsetX, 193 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -93 - offsetX, 205 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -66 - offsetX, 215 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -43 - offsetX, 221 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -21 - offsetX, 224 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -2 - offsetX, 226 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 22 - offsetX, 226 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 46 - offsetX, 221 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 63 - offsetX, 216 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 85 - offsetX, 209 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 111 - offsetX, 197 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 137 - offsetX, 179 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 160 - offsetX, 160 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 181 - offsetX, 135 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 200 - offsetX, 106 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 214 - offsetX, 74 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 223 - offsetX, 37 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 192 - offsetX, 37 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 186 - offsetX, 65 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 170 - offsetX, 99 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 139 - offsetX, 137 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 95 - offsetX, 169 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, 46 - offsetX, 191 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -4 - offsetX, 195 - offsetY)
    CGPathAddLineToPoint(pathTwo, nil, -61 - offsetX, 183 - offsetY)

    CGPathCloseSubpath(pathTwo)

    //pathThree
    let pathThree: CGMutablePathRef = CGPathCreateMutable()

    CGPathMoveToPoint(pathThree, nil, 192,-38 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 223,-38 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 219,-61 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 209,-91 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 196,-116 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 176,-145 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 149,-172 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 111,-198 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 79,-213 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 46,-222 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 15,-227 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, -19,-226 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, -49,-222 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, -71,-216 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, -87,-209 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, -72,-183 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, -39,-194 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 5,-197 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 52,-189 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 99,-170 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 141,-136 - offsetY)
    CGPathAddLineToPoint(pathThree, nil, 176,-87 - offsetY)

    CGPathCloseSubpath(pathThree)

    //Physics bodies
    let bodyOne = SKPhysicsBody(polygonFromPath: path)
    let bodyTwo = SKPhysicsBody(polygonFromPath: pathTwo)
    let bodyThree = SKPhysicsBody(polygonFromPath: pathThree)

    wheel.physicsBody = SKPhysicsBody(bodies: [bodyOne, bodyTwo, bodyThree])
    wheel.physicsBody!.angularDamping = 5
    wheel.physicsBody?.pinned = true
    wheel.physicsBody?.affectedByGravity = false
    wheel.physicsBody?.restitution = 1
    wheel.setScale(scaleWheel)
    addChild(wheel)

Picture of the problem

找到了!我做了一些额外的研究,结果 this post 出现了。

解决方法:

Use bodyWithEdgeLoopFromPath: or bodyWithEdgeChainFromPath: to be able to create concave collision shapes. These bodies will not be dynamic and won't be able to move by themselves but they are the perfect solution for arbitrary collision shapes with a static playfield.