参考 https://www.jianshu.com/p/348678918783 隐私权限 https://www.cnblogs.com/shisishao/p/5872178.html import UIKitimport ARKit struct Masks: OptionSet { let rawValue: Int static let ship = Masks(rawValue: 1 0 ) static let bullet = Mas
参考
https://www.jianshu.com/p/348678918783
隐私权限
https://www.cnblogs.com/shisishao/p/5872178.html
import UIKit import ARKit struct Masks: OptionSet { let rawValue: Int static let ship = Masks(rawValue: 1 << 0) static let bullet = Masks(rawValue: 1 << 1) } class ViewController: UIViewController, ARSCNViewDelegate { var sceneView: ARSCNView! override func viewDidLoad() { super.viewDidLoad() let showHeight : CGFloat = UIScreen.main.bounds.size.height - 100 sceneView = ARSCNView.init(frame: CGRect.init(x: 0, y: 50, width: UIScreen.main.bounds.size.width, height:showHeight)) // 整个场景的大小 self.view.addSubview(sceneView) sceneView.automaticallyUpdatesLighting = true // 启动完成后添加飞船 self.addShip() } func addShip() { let ship = Ship() let x : Double = 0 let y : Double = 0.3 let z : Double = -0.5 ship.position = SCNVector3(x,y,z) self.sceneView.scene.rootNode.addChildNode(ship) //球形 let bullet = Bullet() let x1 : Double = 0.1 let y1 : Double = 0 let z1 : Double = -0.2 bullet.position = SCNVector3(x1,y1,z1) self.sceneView.scene.rootNode.addChildNode(bullet) // 有.scn 类型的资源就更方便了。 // Create a new scene // let scene = SCNScene(named: "art.scnassets/ship.scn")! } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let configuration = ARWorldTrackingConfiguration() // Run the view‘s session sceneView.session.run(configuration) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // Pause the view‘s session sceneView.session.pause() } } // 飞船类 class Ship: SCNNode { override init() { super.init() //正方体 let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0) self.geometry = box let shape = SCNPhysicsShape(geometry: box) self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape) self.physicsBody?.isAffectedByGravity = false self.physicsBody?.categoryBitMask = Masks.ship.rawValue self.physicsBody?.contactTestBitMask = Masks.bullet.rawValue let material = SCNMaterial() material.diffuse.contents = UIImage.init(named: "battle") self.geometry?.materials = [material,material,material,material,material,material] } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } // 子弹类: class Bullet: SCNNode { override init() { super.init() // 形状:球体 let sphere = SCNSphere(radius: 0.025) self.geometry = sphere // 物理实体 let shape = SCNPhysicsShape(geometry: sphere) self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape) self.physicsBody?.isAffectedByGravity = false // 不受引力影响 self.physicsBody?.categoryBitMask = Masks.bullet.rawValue // 自身是识别码 self.physicsBody?.contactTestBitMask = Masks.ship.rawValue // 碰撞的掩码 // 添加节点的图片 let material = SCNMaterial() material.diffuse.contents = UIImage.init(named: "sanlian") self.geometry?.materials = [material] } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }