SceneKit物理联系

时间:2016-11-06 15:38:48

标签: ios swift physics scenekit

我正在尝试为iPhone创建一个测试3D游戏,但我坚持使用物理学。当汽车和救护车相互接触时,汽车不会消失?之前我和SpriteKit合作过,我知道我必须将PhysicsWorld ContactDelegate设置为self,但我不知道如何在SceneKit中执行此操作,也许这就是问题?

代码:

ambulance.physicsBody?.categoryBitMask = PhysicsCategory.ambulance
ambulance.physicsBody?.contactTestBitMask = PhysicsCategory.car

car.physicsBody?.categoryBitMask = PhysicsCategory.car


func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
    let firstNode = contact.nodeA
    let secondNode = contact.nodeB

    if(firstNode.physicsBody?.categoryBitMask == PhysicsCategory.ambulance && secondNode.physicsBody?.categoryBitMask == PhysicsCategory.car ||
        firstNode.physicsBody?.categoryBitMask == PhysicsCategory.car && secondNode.physicsBody?.categoryBitMask == PhysicsCategory.ambulance){

        firstNode.physicsBody?.categoryBitMask = PhysicsCategory.ambulance
        secondNode.physicsBody?.categoryBitMask = PhysicsCategory.car

        secondNode.removeFromParentNode()
    }
}

我希望有人可以帮助我!谢谢!

编辑:我已经找到了如何将场景的contactDelegate设置为self:

scene.physicsWorld.contactDelegate = self

1 个答案:

答案 0 :(得分:1)

对于遇到同样问题的每个人: 我忘了设置我的节点的PhysicsBody。

我必须添加的代码:

ambulance.physicsBody = SCNPhysicsBody.dynamic()
ambulance.physicsBody?.isAffectedByGravity = false

car.physicsBody = SCNPhysicsBody.static()