【问题标题】:SpriteKit PhysicsBody is not applying for my bezier curveSpriteKit PhysicsBody 不适用于我的贝塞尔曲线
【发布时间】:2014-09-30 04:03:16
【问题描述】:

这里是测试代码:

var paths = UIBezierPath()
var bottomLeft = CGPoint(x: dim.minX, y:dim.minY)
var bottomRight = CGPoint(x: dim.maxX , y: dim.minY )
var topRight = CGPoint(x: dim.maxX, y: dim.maxY )
var topLeft = CGPoint(x: dim.minX, y: dim.maxY)


paths.moveToPoint(bottomRight)
paths.addLineToPoint(topRight)

paths.moveToPoint(topRight)
paths.addLineToPoint(topLeft)

paths.moveToPoint(topLeft)
paths.addLineToPoint(bottomLeft)

paths.moveToPoint(bottomLeft)
paths.addLineToPoint(bottomRight)


var n = SKShapeNode(path: paths.CGPath)
n.strokeColor = UIColor.greenColor()
n.physicsBody = SKPhysicsBody(edgeLoopFromPath: paths.CGPath)
shapeLayer.addChild(n)

它绘制一个标准的矩形。我将在从 topRight 到 topLeft 点的中间添加一些曲线,但我想用简单的矩形来测试它。我正在测试与球的碰撞,它似乎只与从 bottomRight 到 topRight 的线发生碰撞,而忽略了其余部分。有人知道这里发生了什么吗?

【问题讨论】:

  • skView.showsPhysics=YES;可以帮助你,你可以看到你的形状周围的薄物理物体
  • @dragoneye 感谢您的提示。看起来应用的物理体不正确。你有什么其他的建议?我已经用图片更新了原始帖子

标签: swift sprite-kit collision-detection skphysicsbody


【解决方案1】:

虽然我不确定贝塞尔路径的问题和所需的物理碰撞设置代码,但是我能够使用原始路径和添加行来让它工作:

    var polygonPath = CGPathCreateMutable()
    CGPathMoveToPoint(polygonPath, nil, bottomRight.x, bottomRight.y)
    CGPathAddLineToPoint(polygonPath, nil, topRight.x, topRight.y)
    CGPathAddLineToPoint(polygonPath, nil, topLeft.x, topLeft.y)
    CGPathAddLineToPoint(polygonPath, nil, bottomLeft.x, bottomLeft.y)
    CGPathCloseSubpath(polygonPath)

    var n = SKShapeNode(path: polygonPath)
    n.strokeColor = UIColor.grayColor()
    n.physicsBody = SKPhysicsBody(edgeLoopFromPath: polygonPath)

这设置了适当的物理碰撞边界。虽然这解决了我的问题,但任何关于为什么原始代码不能使用贝塞尔路径的见解都会很棒。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-01
    • 2013-01-21
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多