Я хочу, чтобы пуля столкнулась с кораблем, но
func physicsWorld(_ world: SCNPhysicsWorld, didEnd contact: SCNPhysicsContact)
никогда не вызывается, хотя я установил свойства столкновения между делегатом и физическим телом.
Вот мой код:
class Bullet: SCNNode {
override init () {
super.init()
let sphere = SCNSphere(radius: 0.020)
sphere.materials.first?.diffuse.contents = UIColor.red
self.geometry = sphere
let shape = SCNPhysicsShape(geometry: sphere, options: nil)
self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape)
self.physicsBody?.isAffectedByGravity = false
self.physicsBody?.categoryBitMask = CollisionCategory.bullets.rawValue
self.physicsBody?.contactTestBitMask = CollisionCategory.enemy.rawValue
self.physicsBody?.collisionBitMask = CollisionCategory.enemy.rawValue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
struct CollisionCategory: OptionSet {
let rawValue: Int
static let bullets = CollisionCategory(rawValue: 1 << 1) // 00...01
static let enemy = CollisionCategory(rawValue: 1 << 0) // 00..10
}
class ARViewController: UIViewController, ARSCNViewDelegate, SCNPhysicsContactDelegate {
@IBOutlet var sceneView: ARSCNView!
var enemyNodes = [SCNNode]()
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
sceneView.scene.physicsWorld.contactDelegate = self
setupEnemies()
}
func setupEnemies() {
guard let enemyNode = SCNScene(named: "art.scnassets/ship.scn")?.rootNode.childNodes.first else { return }
enemyNode.position = getRandomVector()
enemyNode.name = "enemy"
enemyNode.physicsBody?.categoryBitMask = CollisionCategory.enemy.rawValue
enemyNode.physicsBody?.contactTestBitMask = CollisionCategory.bullets.rawValue
enemyNode.physicsBody?.collisionBitMask = CollisionCategory.bullets.rawValue
enemyNodes.append(enemyNode)
sceneView.scene.rootNode.addChildNode(enemyNode)
}
func physicsWorld(_ world: SCNPhysicsWorld, didEnd contact: SCNPhysicsContact) {
print("did begin contact", contact.nodeA.physicsBody!.categoryBitMask, contact.nodeB.physicsBody!.categoryBitMask)
}
}
Я не знаю, куда я иду не так.