2016-08-31 124 views
0

我找不出爲什麼我的碰撞未被檢測到。我希望你能弄清楚爲什麼,因爲這導致了很多問題,我需要學習如何正確地做到這一點。 Swift,SceneKit。沒有檢測到SceneKit碰撞檢測

好吧,以便始終我開始了這種說法:

override func viewDidLoad() { 
    super.viewDidLoad() 


    createScene() 



    scene.physicsWorld.contactDelegate = self 

    // This Statement.^ 

    motionManager = CMMotionManager() 
    motionManager.startAccelerometerUpdates() 


} 

我想一個球,箱節點之間發生衝突。我的盒子節點是mainBox,我的球是......球。

ball.position = SCNVector3Make(0, 1.75, 3) 
    ball.geometry = ballGeometry 
    ballMaterial.diffuse.contents = UIColor.greenColor() 
    ballGeometry.materials = [ballMaterial] 
    scene.rootNode.addChildNode(ball) 
    ball.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: SCNPhysicsShape(geometry: ballGeometry, options: nil)) 
    ball.physicsBody?.angularVelocityFactor = SCNVector3Make(0, 0, 0) 
    ball.physicsBody?.angularVelocity = SCNVector4Make(0, 0, 0, 0) 
    ball.name = "sphere" 
    ball.physicsBody?.categoryBitMask = bodyNames.Person 
    ball.physicsBody?.contactTestBitMask = bodyNames.Floor 
    ball.physicsBody?.collisionBitMask = bodyNames.Floor 
    ball.physicsBody?.affectedByGravity = true 
    ball.addChildNode(cameraNode) 


    mainBox.position = SCNVector3Make(0, -0.75, 2) 
    mainBox.geometry = mainBoxGeometry 
    mainBoxMaterial.diffuse.contents = UIColor.whiteColor() 
    mainBox.physicsBody?.categoryBitMask = bodyNames.Floor 
    mainBox.physicsBody?.contactTestBitMask = bodyNames.Person 
    mainBox.physicsBody?.collisionBitMask = bodyNames.Person 
    mainBox.physicsBody = SCNPhysicsBody.staticBody() 
    mainBoxGeometry.materials = [mainBoxMaterial] 
    mainBox.name = "floor" 
    scene.rootNode.addChildNode(mainBox) 

現在神奇在哪裏發生,但也神奇在哪裏是沒有發生......

func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) { 

    let nodeA = contact.nodeA 
    let nodeB = contact.nodeB 

    if nodeA.physicsBody?.categoryBitMask == bodyNames.Person && nodeB.physicsBody?.categoryBitMask == bodyNames.Floor || nodeA.physicsBody?.categoryBitMask == bodyNames.Floor && nodeB.physicsBody?.categoryBitMask == bodyNames.Person { 


     print("I collided with the damn box.") 

    } 

} 

哦,這件事情是在bodyNames從何而來。

struct bodyNames { 

static let Person = 0x1 << 1 
static let Floor = 0x1 << 2 

} 

讓我知道如果我需要添加更多的代碼來回答,我很樂意這樣做。

+2

physicsBody你設置'上所有的位掩碼在設置physicsBody本身之前的mainBox' physicsBody。 –

+0

非常簡單的修復,但它現在正常工作。謝謝。 @JamesP –

回答

0

你有沒有檢查過didBeginContact是否訪問過?

如果參觀,檢查,如果條件

nodeA.physicsBody?.categoryBitMask == bodyNames.Person && nodeB.physicsBody?.categoryBitMask == bodyNames.Floor || nodeA.physicsBody?.categoryBitMask == bodyNames.Floor && nodeB.physicsBody?.categoryBitMask == bodyNames.Person 

如果沒有訪問所有,u必須檢查是否世界,盒子和球配置正確

+0

didBeginContact由於某種原因未被調用。 –