I build an application with collision detection using delegate SCNPhysicsContactDelegate. I try to detect collision with SCNPhysicsContactDelegate delegate but it doesn't work.
what is worng !?
this is my code
let CollisionCategorySpaceMan = 1
let CollisionCategoryEnemy = 2
func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) {
if contact.nodeB.physicsBody!.categoryBitMask == CollisionCategoryEnemy {
print("Enemy HIT!--------")
}
}
override func viewDidLoad() {
super.viewDidLoad()
let mainScene = createMainScene()
mainScene.physicsWorld.contactDelegate = self
mainScene.physicsWorld.gravity = SCNVector3Make(0, 0, 0)
sceneView = self.view as! GameView
sceneView.scene = mainScene
sceneView.delegate = self
}
this is the enemy
func setupEnemy(){
space = space - 300
let football2 = SCNScene(named: "art.scnassets/k.scn")
let football21 = football2!.rootNode.childNodeWithName("football2", recursively: false)
football21!.name = "Enemy"
football21!.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
football21!.physicsBody!.categoryBitMask = CollisionCategoryEnemy
football21!.physicsBody!.collisionBitMask = CollisionCategorySpaceMan
football21!.position = SCNVector3(x: 0, y: 0, z: Float(space))
sceneView.scene!.rootNode.addChildNode(football21!)
}
and this is the hero
func createMainScene() -> SCNScene {
let scene = SCNScene(named: "art.scnassets/football.scn")
spaceManNode = scene!.rootNode.childNodeWithName("football", recursively: false)
spaceManNode!.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
spaceManNode!.physicsBody!.categoryBitMask = CollisionCategorySpaceMan
spaceManNode!.physicsBody!.contactTestBitMask = CollisionCategoryEnemy
spaceManNode.name = "SpaceMan"
setupLighting(scene!)
setupCameras(scene!)
return scene!
}
didBeginContact
gets at least called? Who guarantees you thatnodeB
is ofCollisionCategoryEnemy
, notnodeA
? – luk2302collisionBitMask
on yourspaceManNode
. – James P