2015-09-27 40 views
1

我已經使用委託SCNPhysicsContactDelegate構建了一個包含衝突檢測的應用程序。現在我正在使用Swift 2.0和Xcode 7,並嘗試檢測與SCNPhysicsContactDelegate委託的衝突,但它不起作用。 我做錯了什麼?代碼如下:SceneKit Swift 2.0碰撞檢測(SCNPhysicsContactDelegate)

let CubeType = 1 
let PlayerType = 2 
class GameViewController: UIViewController, SCNSceneRendererDelegate,SCNPhysicsContactDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let scnView = self.view as! SCNView 
    scnView.scene = SCNScene() 
    scnView.scene?.physicsWorld.contactDelegate = self 
    let cameraNode = SCNNode() 
    cameraNode.camera = SCNCamera() 
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 5) 
    scnView.scene?.rootNode.addChildNode(cameraNode) 
    scnView.autoenablesDefaultLighting = true 
    scnView.allowsCameraControl = true 
    scnView.showsStatistics = true 
    scnView.backgroundColor = UIColor.blackColor() 

    let g = SCNBox(width: 0.5, height: 0.5, length: 0.5, chamferRadius: 0) 
    let n = SCNNode(geometry: g) 
    n.physicsBody = SCNPhysicsBody(type: .Kinematic, shape: SCNPhysicsShape(geometry: g, options: nil)) 
    n.physicsBody?.categoryBitMask = CubeType 
    n.physicsBody?.collisionBitMask = PlayerType 
    n.position = SCNVector3Make(5, 0, 0) 
    scnView.scene?.rootNode.addChildNode(n) 

    let playerGeometry = SCNSphere(radius: 1) 
    let playerNode = SCNNode(geometry: playerGeometry) 
    playerNode.position = SCNVector3Zero 
    playerNode.physicsBody = SCNPhysicsBody(type: .Kinematic, shape: SCNPhysicsShape(geometry: playerGeometry, options:nil)) 

    playerNode.physicsBody?.categoryBitMask = PlayerType 
    playerNode.physicsBody?.collisionBitMask = CubeType 
    scnView.scene?.rootNode.addChildNode(playerNode) 
    let moveAction = SCNAction.moveTo(n.position, duration: 3) 
    playerNode.runAction(moveAction) 
} 

func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) { 
    print("-> didBeginContact") 
} 

func physicsWorld(world: SCNPhysicsWorld, didEndContact contact: SCNPhysicsContact) { 
    print("-> didEndContact") 
} 

func physicsWorld(world: SCNPhysicsWorld, didUpdateContact contact: SCNPhysicsContact) { 
    print("-> didupdatecontact") 
} 
+0

可能是因爲現場沒有一個物理的身體嗎?像邊界一樣? – EDUsta

+0

場景沒有物理主體 –

回答

1

.contactTestBitMask財產缺少兩個節點:

n.physicsBody?.contactTestBitMask = PlayerType 
playerNode.physicsBody?.contactTestBitMask = CubeType 
+0

用於定義哪些類別的主體會導致與該物理主體交叉通知的蒙版。默認爲0.在iOS 8和OS X 10.10及更低版本中,交集通知總是在發生衝突時發送。 – Apps