arWars

AR Tutorial with Star Wars Theme

Stars
7

ArWars

A small example project I've live coded at the Google Developer Group conference DevFest 2017 in Karlsruhe Germany.

http://www.devfestka.de/programm

It is based on ARKit and demonstrates general 3D development basics, SceneKit, loading 3D Models, the PhysicsEngine and the AVAudioPlayer.

Step by Step Guide

Checkout commit "Initial version"

Step 1: Create laser

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
 let node = SCNNode()

 let box = SCNBox(width: 0.1, height: 0.1, length: 2, chamferRadius: 0)
 box.firstMaterial?.diffuse.contents = UIColor.red
 box.firstMaterial?.lightingModel = .constant

 node.geometry = box
 node.opacity = 0.5

 if let pov = sceneView.pointOfView {
     node.position = pov.position
     node.position.y -= 0.3
     node.eulerAngles = pov.eulerAngles
 }

 sceneView.scene.rootNode.addChildNode(node)
}

See:

Step 2: Add physics and animate laser

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  ...
  addPhysics(laser)
  animateLaser(laser)
}

Step 3: Play laser sound

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  ...
  assets.playSoundEffect(ofType: .laser)
}

Step 4: Make laser disappear after 1 second

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  ...
  DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      laser.removeFromParentNode()
  }
}

Step 5: Add new TIE Fighter

override func viewDidLoad() {
  ...
  addNewTieFighter()
}

Step 6: Animate TIE fighter

private func addNewTieFighter() {
  ...
  animateFighter(fighter)
}

Step 7: Remove 3D objects on contact

func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
  ...
  contact.nodeA.removeFromParentNode()
  contact.nodeB.removeFromParentNode()
}

Step 8: Play explosion animation and sound

func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
  ...
  assets.playSoundEffect(ofType: .explosion)
  createExplosion(contact.nodeA.position)
}

Step 9: Spawn new TIE Fighter

func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
  ...
  DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      self.addNewTieFigher()
  }
}