ios – SpriteKit碰撞检测中SKSpriteNodes之间的差距

我一直试图想出这个问题已经有一段时间了 – 我有一个简单的平台物理游戏,玩家会掉到一个街区,这会阻止他摔倒.这是有效的,但是在玩家停止的位置和实际的对象/ spritenode之间存在明显的差距.这是截图,应该是不言自明的:

《ios – SpriteKit碰撞检测中SKSpriteNodes之间的差距》

class GameScene: SKScene {

override init(){
    super.init(size: UIScreen.mainScreen().bounds.size)
    self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
func genBlock(iteration: CGFloat){
    let block = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100,50))
    block.position = CGPointMake((iteration*50)-block.size.width/2,0)
    block.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(0,0,block.size.width,block.size.height))
    self.addChild(block)
}
func genPlayer(){
    let char = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100,100))
    char.position = CGPointMake(0,500)
    char.physicsBody = SKPhysicsBody(rectangleOfSize: char.size)
    self.addChild(char)
}
override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    view.backgroundColor = UIColor.whiteColor()
    self.view?.backgroundColor = UIColor.whiteColor()
    genBlock(0)
    genBlock(1)
    genBlock(2)
    genBlock(3)
    genBlock(4)
    genPlayer()
}
}

我觉得这不是SpriteKit的问题,而是我的代码问题.关于如何消除节点之间的这种差距的任何建议将不胜感激.

编辑:我已将此提交给Apple的bug报告,但我不希望收到任何回复.如果有人在这个帖子发布后很久就会来这里,如果你能在这里提交你的建议会很棒.

最佳答案 我相信它与从CGPath构建的physicsBodies有关.例如,使用下面的代码创建6个方块:

override func didMoveToView(view: SKView) {
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
    self.backgroundColor = UIColor.whiteColor()

    let square1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 50, height: 50))
    square1.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: -25, y: -25, width: square1.size.width, height: square1.size.height))
    square1.position = CGPoint(x: self.size.width/2 - 75, y: 25)
    self.addChild(square1)

    let square2 = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 50, height: 50))
    square2.physicsBody = SKPhysicsBody(rectangleOfSize: square2.size)
    square2.position = CGPoint(x: self.size.width/2 - 75, y: 200)
    self.addChild(square2)

    let square3 = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 50, height: 50))
    square3.physicsBody = SKPhysicsBody(rectangleOfSize: square3.size)
    square3.position = CGPoint(x: self.size.width/2 + 75, y: 200)
    self.addChild(square3)

    let squarePath = getSquarePath()
    let square4 = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
    square4.physicsBody = SKPhysicsBody(polygonFromPath: squarePath)
    square4.position = CGPoint(x: self.size.width/2 + 75, y: 400)
    self.addChild(square4)

    let square5 = SKSpriteNode(color: UIColor.orangeColor(), size: CGSize(width: 50, height: 50))
    square5.physicsBody = SKPhysicsBody(rectangleOfSize: square5.size)
    square5.position = CGPoint(x: self.size.width/2, y: 200)
    self.addChild(square5)

    let square6 = SKSpriteNode(color: UIColor.purpleColor(), size: CGSize(width: 50, height: 50))
    square6.physicsBody = SKPhysicsBody(rectangleOfSize: square6.size)
    square6.position = CGPoint(x: self.size.width/2, y: 400)
    self.addChild(square6)
}

func getSquarePath() -> CGPath {
    let path = CGPathCreateMutable()
    CGPathMoveToPoint(path, nil, -25, -25)
    CGPathAddLineToPoint(path, nil, -25, 25)
    CGPathAddLineToPoint(path, nil, 25, 25)
    CGPathAddLineToPoint(path, nil, 25, -25)
    CGPathCloseSubpath(path)

    return path
}

您可以看到使用CGPath创建的physicsBodies的正方形(包括edgeLoopF​​romRect)存在间隙问题.即使是场景边缘循环的physicsBody也存在这个问题.当我使用纹理构造函数构建physicsBody时,这也显示出来,这似乎构建了引擎盖下的CGPath.

除了不使用那些类型的物理实体进行长期(可见)碰撞之外,我还没有找到解决方法.

点赞