Reference
https://www.jianshu.com/p/348678918783
Privacy permission
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))
// the size of the entire scene
self.view.addSubview(sceneView)
sceneView.automaticallyUpdatesLighting = true
// Add spaceship after startup
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)
//spherical
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)
// It is more convenient to have .scn type resources.
// 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()
}
}
// Spaceship class
class Ship: SCNNode {
override init() {
super.init()
//Cube
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 span>")
}
}
// Bullets:
class Bullet: SCNNode {
override init() {
super.init()
// Shape: Sphere
let sphere = SCNSphere(radius: 0.025)
self.geometry = sphere
// physical entities
let shape = SCNPhysicsShape(geometry: sphere)
self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape )
self.physicsBody?.isAffectedByGravity = false // Not affected by gravity
self.physicsBody?.categoryBitMask = Masks.bullet.rawValue // itself is the identification code
self.physicsBody?.contactTestBitMask = Masks.ship.rawValue // collision mask
// Add a picture of the node
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 span>")
}
}
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))
// the size of the entire scene
self.view.addSubview(sceneView)
sceneView.automaticallyUpdatesLighting = true
// Add spaceship after startup
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)
//spherical
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)
// It is more convenient to have .scn type resources.
// 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()
}
}
// Spaceship class
class Ship: SCNNode {
override init() {
super.init()
//Cube
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 span>")
}
}
// Bullets:
class Bullet: SCNNode {
override init() {
super.init()
// Shape: Sphere
let sphere = SCNSphere(radius: 0.025)
self.geometry = sphere
// physical entities
let shape = SCNPhysicsShape(geometry: sphere)
self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape )
self.physicsBody?.isAffectedByGravity = false // Not affected by gravity
self.physicsBody?.categoryBitMask = Masks.bullet.rawValue // itself is the identification code
self.physicsBody?.contactTestBitMask = Masks.ship.rawValue // collision mask
// Add a picture of the node
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 span>")
}
}