collision detection

I am trying to get collision detection to work. But can't get it to call the delegate method. Thanks for help in getting this to work.

Note I am using TVOS, but tried it also on ios. Also used latest beta and newest release with the same results


1) Added delegates SCNSceneRendererDelegate, SCNPhysicsContactDelegate to class definition

2) scnView.scene?.physicsWorld.contactDelegate = self


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

print("collision")

}


Thanks

Steve


import UIKit
import QuartzCore
import SceneKit
import SpriteKit
class GameViewController: UIViewController, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{
    override func viewDidLoad() {
        super.viewDidLoad()
     
        let scene = SCNScene()
    
       
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)
    
        cameraNode.position = SCNVector3(x: 0, y: 7, z: 15)
    
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light!.type = SCNLightTypeOmni
        lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
        scene.rootNode.addChildNode(lightNode)
    
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light!.type = SCNLightTypeAmbient
        ambientLightNode.light!.color = UIColor.darkGrayColor()
        scene.rootNode.addChildNode(ambientLightNode)
    
        let scnView = self.view as! SCNView
    
        scnView.scene = scene
    
        scnView.allowsCameraControl = true
    
        scnView.showsStatistics = true
    
        scnView.backgroundColor = UIColor.blackColor()
    
        scnView.scene?.physicsWorld.contactDelegate = self
    
        let plane1 = SCNBox(width: 10, height: 1, length: 10, chamferRadius: 0)
        let planeNode1 = SCNNode(geometry: plane1)
        planeNode1.position = SCNVector3(0,-5,-5)
        planeNode1.eulerAngles = SCNVector3(Float(-M_2_PI), 0, 0 )
        planeNode1.physicsBody = SCNPhysicsBody.staticBody()
        scene.rootNode.addChildNode(planeNode1)
    
        let sphere1 = SCNSphere(radius: 1)
        let sphereNode1 = SCNNode(geometry: sphere1)
        sphereNode1.position = SCNVector3(0,5,-5)
        sphereNode1.physicsBody = SCNPhysicsBody.dynamicBody()
        sphereNode1.geometry?.firstMaterial?.diffuse.contents = UIColor.yellowColor()
        scene.rootNode.addChildNode(sphereNode1)
    
    }

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


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
}
Accepted Answer

It appears they made contactTestBitMask a requirement now. It used to work without setting it. Try setting


physicsBody.contactTestBitMask to the same integer. Worked for me.

Thanks!

collision detection
 
 
Q