Created
September 16, 2015 15:10
-
-
Save mzsima/04c956c4382ba19a809c to your computer and use it in GitHub Desktop.
horizontal bar
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 | |
// horizontalBar | |
// | |
// Created by MizushimaYusuke on 9/16/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
import UIKit | |
import SceneKit | |
class ViewController: UIViewController { | |
weak var sceneView : SCNView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupScene() | |
createBar() | |
createDoll(); | |
createCamera() | |
} | |
func setupScene() { | |
let sv = SCNView(frame: view.bounds) | |
sv.scene = SCNScene() | |
sv.backgroundColor = UIColor(hue: 0.34, saturation: 0.1, brightness: 1, alpha: 1) | |
sv.autoenablesDefaultLighting = true | |
sv.allowsCameraControl = true | |
view.addSubview(sv) | |
sceneView = sv | |
} | |
func createBar() { | |
let pillar = SCNCylinder(radius: 0.2, height: 5) | |
pillar.firstMaterial?.diffuse.contents = UIColor.lightGrayColor() | |
pillar.firstMaterial?.specular.contents = UIColor.whiteColor() | |
for i in 0...1 { | |
let pillarNode = SCNNode(geometry: pillar) | |
pillarNode.position = SCNVector3(x: i == 0 ? -2 : 2, y: 0, z: 0) | |
pillarNode.physicsBody = SCNPhysicsBody.staticBody() | |
self.sceneView?.scene?.rootNode.addChildNode(pillarNode) | |
} | |
let bar = SCNCylinder(radius: 0.1, height: 4) | |
bar.name = "bar" | |
bar.materials = pillar.materials | |
let barNode = SCNNode(geometry: bar) | |
barNode.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(M_PI * 0.5)) | |
barNode.position = SCNVector3(x: 0, y: 2.3, z: 0) | |
sceneView?.scene?.rootNode.addChildNode(barNode) | |
} | |
func createDoll() { | |
let doll = SCNNode() | |
doll.name = "doll" | |
doll.position = SCNVector3(x: 0, y: 2, z: 0.3) | |
let mt = SCNMaterial() | |
mt.diffuse.contents = UIColor.grayColor() | |
mt.specular.contents = UIColor.blueColor() | |
let head = SCNSphere(radius: 0.4) | |
head.materials = [mt] | |
let headNode = SCNNode(geometry: head) | |
headNode.position = SCNVector3(x: 0, y: 1, z: 0) | |
doll.addChildNode(headNode) | |
let body = SCNBox(width: 0.8, height: 1.2, length: 0.5, chamferRadius: 0.1) | |
body.materials = [mt] | |
let bodyNode = SCNNode(geometry: body) | |
doll.addChildNode(bodyNode) | |
let bar = SCNCylinder(radius: 0.05, height: 3) | |
bar.materials = [mt] | |
let barNode = SCNNode(geometry: bar) | |
barNode.position = SCNVector3(x: 0, y: -1.5, z: 0) | |
doll.addChildNode(barNode) | |
let weight = SCNBox(width: 0.6, height: 0.4, length: 0.4, chamferRadius: 0) | |
weight.materials = [mt] | |
let weightNode = SCNNode(geometry: weight) | |
weightNode.position = SCNVector3(x: 0, y: -2.5, z: 0) | |
doll.addChildNode(weightNode) | |
doll.physicsBody = SCNPhysicsBody.dynamicBody() | |
let joint = SCNPhysicsHingeJoint(body: doll.physicsBody!, axis: SCNVector3(x: 1, y: 0, z: 0), anchor: SCNVector3(x: 0, y: 0.3, z: -0.4)) | |
sceneView?.scene?.physicsWorld.addBehavior(joint) | |
sceneView?.scene?.rootNode.addChildNode(doll) | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
let doll = sceneView?.scene?.rootNode.childNodeWithName("doll", recursively: false) | |
doll?.physicsBody?.applyTorque(SCNVector4(x: 1, y: 0, z: 0, w: 2), impulse: true) | |
} | |
func createCamera() { | |
let camera = SCNNode() | |
camera.camera = SCNCamera() | |
camera.position = SCNVector3(x: 0, y: 0, z: 14) | |
sceneView?.scene?.rootNode.addChildNode(camera) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4360