Created
September 13, 2015 12:39
-
-
Save mzsima/7ae9a27dc4d638943f76 to your computer and use it in GitHub Desktop.
backflip bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// BackflipBot | |
// | |
// Created by Mizushima Yusuke on 9/13/15. | |
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved. | |
// | |
import UIKit | |
import SceneKit | |
class ViewController: UIViewController { | |
weak var sceneView : SCNView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupScene() | |
createGround() | |
createBot() | |
} | |
func setupScene() { | |
let sv = SCNView(frame: view.bounds) | |
sv.scene = SCNScene() | |
sv.backgroundColor = UIColor(hue: 0.6, saturation: 0.1, brightness: 1, alpha: 1) | |
sv.allowsCameraControl = true | |
sv.autoenablesDefaultLighting = true | |
view.addSubview(sv) | |
sceneView = sv | |
} | |
func createGround() { | |
let ground = SCNBox(width: 10, height: 1, length: 10, chamferRadius: 0) | |
ground.firstMaterial?.diffuse.contents = UIColor.brownColor() | |
let gNode = SCNNode(geometry: ground) | |
gNode.position = SCNVector3(x: 0, y: -3, z: 0) | |
sceneView?.scene?.rootNode.addChildNode(gNode) | |
} | |
func createBot() { | |
let bot = SCNNode() | |
bot.name = "bot" | |
sceneView?.scene?.rootNode.addChildNode(bot) | |
for i in 0...1 { | |
let leg = SCNBox(width: 0.5, height: 5, length: 1, chamferRadius: 0) | |
leg.firstMaterial?.diffuse.contents = UIColor.lightGrayColor() | |
let lNode = SCNNode(geometry: leg) | |
lNode.position = SCNVector3(x: (i==0) ? -2.4 : 2.4, y: 0, z: 0) | |
bot.addChildNode(lNode) | |
let foot = SCNBox(width: 1, height: 0.5, length: 3, chamferRadius: 0) | |
foot.firstMaterial?.diffuse.contents = UIColor.lightGrayColor() | |
let fNode = SCNNode(geometry: foot) | |
fNode.position = SCNVector3(x: (i==0) ? -2.5 : 2.5, y: -2.5, z: 0) | |
bot.addChildNode(fNode) | |
} | |
let body = SCNCylinder(radius: 2.1, height: 4) | |
body.firstMaterial?.diffuse.contents = UIColor(white: 0.99, alpha: 1) | |
body.firstMaterial?.specular.contents = UIColor.whiteColor() | |
let bNode = SCNNode(geometry: body) | |
bNode.name = "body" | |
bNode.pivot = SCNMatrix4MakeTranslation(0, 2, 0) | |
bNode.position = SCNVector3(x: 0, y: 2, z: 0) | |
bot.addChildNode(bNode) | |
let head = SCNSphere(radius: 2.1) | |
head.firstMaterial?.diffuse.contents = UIColor.lightGrayColor() | |
head.firstMaterial?.diffuse.contents = UIColor.whiteColor() | |
let hNode = SCNNode(geometry: head) | |
hNode.position = SCNVector3(x: 0, y: 2, z: 0) | |
bNode.addChildNode(hNode) | |
let ring = SCNCylinder(radius: 2.15, height: 0.3) | |
ring.firstMaterial?.diffuse.contents = UIColor(hue: 0.6, saturation: 1, brightness: 0.5, alpha: 1) | |
let rNode = SCNNode(geometry: ring) | |
rNode.position = SCNVector3(x: 0, y: 2, z: 0) | |
bNode.addChildNode(rNode) | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
if let bot = sceneView?.scene?.rootNode.childNodeWithName("body", recursively: true) { | |
bot.runAction(SCNAction.rotateByX(-2.0 * CGFloat(M_PI), y: 0, z: 0, duration: 3.0)) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4347