swift - SpriteKit PhysicsBody 没有应用我的贝塞尔曲线

标签 swift sprite-kit collision-detection skphysicsbody

测试代码如下:

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)

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

Physic issue: Physics is not applied fully to middle rectangle in gray

最佳答案

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

    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)

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

关于swift - SpriteKit PhysicsBody 没有应用我的贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26112588/

相关文章:

ios - 比较字符串的问题

swift - 当 2 个手指在屏幕上时应用程序崩溃,触摸开始,触摸移动

ios - Sprite Kit 仅对屏幕左右边缘进行碰撞检测

swift - vDSP_conv 有时会返回 NAN

swift - 如何在没有任何 UI 元素的情况下录制 ARSCNView?

没有 cocoapods 的 iOS firebase Mach-O 链接器错误

swift - Sprite 互相跟随

ios - SKPhysicsJointPin 引脚位置

python - Pygame Sprites 未检测到碰撞

C#/XNA - 二维碰撞引擎故障?