1
votes

I have some issues with detection of collisions. I want to see in the console that collision is detected.

When nodes are generated I see in console what kind of categoryBitMask they have, so there is must be no problem. CollisionMask and contactBitMask are set as well.

So objects interact with each other with no problem.

If I change masks they stop interacting with each other so they go through each other. So I can say that mask set properly. Masks I set with the helper struct:

struct BitMaskCategory: OptionSet {
    let rawValue: Int

    static let none    = BitMaskCategory(rawValue: 0 << 0) // 0 
    static let box     = BitMaskCategory(rawValue: 1 << 0) // 1
    static let plane   = BitMaskCategory(rawValue: 1 << 1) // 2
}

One body is .dynamic physicsBody other is .static.

My ViewController class implement SCNPhysicsContactDelegate protocol and in viewDidLoad I write:

sceneView.scene.physicsWorld.contactDelegate = self

After that I try to use method but it is not even called:

func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
    if contact.nodeB.physicsBody?.contactTestBitMask == 1 {
        print("NodeB has mask = 1")
    } else {
        print("NodeB has mask != 1")
    }
}

After that I press on screen and see cubes are coming and hit plane surface but there is no contact detection at all. Delegate method physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) even is not called at all.

What I miss?

Thank you!

1

1 Answers

4
votes

After multiple hour debug challenge I found that I rewrite scene after I set delegate.

Was:

   sceneView.scene.physicsWorld.contactDelegate = self
    let scene = SCNScene()
    sceneView.scene = scene

Should be:

    let scene = SCNScene()
    sceneView.scene = scene

    sceneView.scene.physicsWorld.contactDelegate = self

For my case this fixes the problem. Hope someone will save hours with this :)