SpriteKit SKShapeNode因重力碰撞而过早停止

时间:2015-03-04 16:58:53

标签: ios swift sprite-kit

这是设置

    // Create a scene
    scene = SKScene(size: self.view.frame.size)

    let sceneHeight = scene.frame.height
    let sceneWidth = scene.frame.width

    // Create nodes
    // Rectangle
    let shapeWidth: CGFloat = 100
    let shapeHeight = shapeWidth
    let shapeNode = SKShapeNode(rect: CGRectMake(0, 0, shapeWidth, 100))
    shapeNode.position = CGPoint(x: sceneWidth/2 - shapeWidth/2, y: 300)
    shapeNode.fillColor = UIColor.redColor()
    shapeNode.strokeColor = UIColor.redColor()

    // Floor
    let floorWidth: CGFloat = sceneWidth
    let floorHeight: CGFloat = 10
    let floorNode = SKShapeNode(rect: CGRectMake(0, 0, floorWidth, floorHeight))
    floorNode.position = CGPoint(x: 0, y: 100)
    floorNode.fillColor = UIColor.greenColor()
    floorNode.strokeColor = UIColor.greenColor()

    // Create the node tree
    scene.addChild(floorNode)
    scene.addChild(shapeNode)

然后我定义并向我的两个节点添加两个物理实体。

    // Create physics
    let shapePhysicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: shapeWidth, height: shapeHeight))
    shapePhysicsBody.dynamic = true
    shapePhysicsBody.categoryBitMask = PhysicsCategory.Shape
    shapePhysicsBody.contactTestBitMask = PhysicsCategory.Floor
    shapePhysicsBody.collisionBitMask = PhysicsCategory.None
    shapePhysicsBody.usesPreciseCollisionDetection = true

    let floorPhysicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: floorWidth, height: floorHeight))
    floorPhysicsBody.dynamic = true
    floorPhysicsBody.categoryBitMask = PhysicsCategory.Floor
    floorPhysicsBody.contactTestBitMask = PhysicsCategory.Shape
    floorPhysicsBody.collisionBitMask = PhysicsCategory.None
    floorPhysicsBody.usesPreciseCollisionDetection = true
    floorPhysicsBody.affectedByGravity = false

    // Add physics
    shapeNode.physicsBody = shapePhysicsBody
    floorNode.physicsBody = floorPhysicsBody
    scene.physicsWorld.gravity = CGVector(dx: 0, dy: -1)
    scene.physicsWorld.contactDelegate = self

这种工作,即矩形形状确实从它的初始位置落下,碰撞发生时调用碰撞委托。

我的didBeginContact:方法的内容。

func didBeginContact(contact: SKPhysicsContact) {
    var firstBody   : SKPhysicsBody
    var secondBody  : SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody   = contact.bodyA
        secondBody  = contact.bodyB
    } else {
        firstBody   = contact.bodyB
        secondBody  = contact.bodyA
    }

    if ((firstBody.categoryBitMask & PhysicsCategory.Floor != 0) && (secondBody.categoryBitMask & PhysicsCategory.Shape != 0)) {
        secondBody.dynamic = false
        println("collision")
    }
}

但形状节点在视觉接触之前就停止了。我希望这些图片给人一种看起来如何的印象。

从这里开始 Initial state

这是联系发生的地方 Final state

为什么它们之间存在这样的差距?我错过了什么?在矩形停止移动之前,我希望它们在视觉上相互接触。

1 个答案:

答案 0 :(得分:1)

SKShapeNode(rect:)SKShapeNode绘制为node.position origin。对于SKPhysicsBody position,对应center的{​​{1}}。这会在物理主体和实际节点之间创建一个偏移量。你可以看到,如果你启用

rectangle

您可以使用以下代码创建scene.view?.showsPhysics = true来更正偏移量。

SKShapeNode