Created
August 21, 2015 11:46
-
-
Save mzsima/0d63c890bc6b975c3555 to your computer and use it in GitHub Desktop.
ball disk
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 | |
// BallDisk | |
// | |
// Created by MizushimaYusuke on 8/21/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
import UIKit | |
import SceneKit | |
class ViewController: UIViewController, SCNSceneRendererDelegate { | |
var state = 0 | |
weak var sceneView : SCNView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupScene() | |
createDisk() | |
createBall() | |
createCamera() | |
} | |
func setupScene() { | |
let sv = SCNView(frame: view.bounds) | |
sv.backgroundColor = UIColor.darkGrayColor() | |
sv.scene = SCNScene() | |
sv.autoenablesDefaultLighting = true | |
sv.allowsCameraControl = true | |
sv.delegate = self | |
view.addSubview(sv) | |
sceneView = sv | |
} | |
func createDisk() { | |
let disk = SCNNode() | |
disk.name = "disk" | |
disk.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(M_PI) * 0.5) | |
let backboard = SCNCylinder(radius: 5, height: 0.1) | |
backboard.firstMaterial?.diffuse.contents = UIColor(hue: 0, saturation: 0.6, brightness: 0.7, alpha: 1) | |
let backboardNode = SCNNode(geometry: backboard) | |
disk.addChildNode(backboardNode) | |
let edge = SCNTube(innerRadius: 4.8, outerRadius: 5, height: 1) | |
edge.firstMaterial?.diffuse.contents = UIColor(hue: 0.25, saturation: 1, brightness: 0.7, alpha: 1) | |
let edgeNode = SCNNode(geometry: edge) | |
edgeNode.position = SCNVector3(x: 0, y: 0.5, z: 0) | |
disk.addChildNode(edgeNode) | |
for i in [SCNVector3(x: -2.5, y: 0.5, z: -2), | |
SCNVector3(x: 2, y: 0.5, z: 0), | |
SCNVector3(x: -1.7, y: 0.5, z: 1.5), | |
SCNVector3(x: 1.5, y: 0.5, z: 3)] { | |
let b = SCNBox(width: 6, height: 1, length: 0.25, chamferRadius: 0) | |
b.firstMaterial?.diffuse.contents = UIColor(hue: 0.25, saturation: 1, brightness: 0.7, alpha: 1) | |
let bNode = SCNNode(geometry: b) | |
bNode.position = i | |
disk.addChildNode(bNode) | |
} | |
disk.physicsBody = SCNPhysicsBody.dynamicBody() | |
disk.physicsBody?.physicsShape = SCNPhysicsShape(node: disk, options: [SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron]) | |
disk.physicsBody?.angularVelocityFactor = SCNVector3(x: 0, y: 0, z: 1) | |
disk.physicsBody?.angularDamping = 0.9 | |
let pin = SCNPhysicsHingeJoint(body: disk.physicsBody!, axis: SCNVector3(x: 0, y: 1, z: 0), anchor: disk.position) | |
self.sceneView?.scene?.physicsWorld.addBehavior(pin) | |
sceneView?.scene?.rootNode.addChildNode(disk) | |
} | |
func createBall() { | |
let ball = SCNSphere(radius: 0.4) | |
ball.firstMaterial?.diffuse.contents = UIColor.lightGrayColor() | |
let ballNode = SCNNode(geometry: ball) | |
ballNode.position = SCNVector3(x: 0, y: 4, z: 0.6) | |
sceneView?.scene?.rootNode.addChildNode(ballNode) | |
ballNode.physicsBody = SCNPhysicsBody.dynamicBody() | |
ballNode.physicsBody?.velocityFactor = SCNVector3(x: 1, y: 1, z: 0) | |
} | |
func createCamera() { | |
let camera = SCNNode() | |
camera.camera = SCNCamera() | |
camera.position = SCNVector3(x: 0, y: 0, z: 18) | |
sceneView?.scene?.rootNode.addChildNode(camera) | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
sceneView?.playing = true | |
if let touch = touches.first as? UITouch { | |
let p = touch.locationInView(self.sceneView) | |
self.state = p.x < CGRectGetMidX(self.view.bounds) ? 1 : 2 | |
} | |
} | |
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { | |
self.state = 0 | |
} | |
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) { | |
if let disk = self.sceneView?.scene?.rootNode.childNodeWithName("disk", recursively: false) { | |
switch (self.state) { | |
case 1: | |
disk.physicsBody?.applyTorque(SCNVector4(x: 0, y: 0, z: 1, w: 50), impulse: false) | |
break | |
case 2: | |
disk.physicsBody?.applyTorque(SCNVector4(x: 0, y: 0, z: 1, w: -50), impulse: false) | |
break | |
default: | |
disk.physicsBody?.angularVelocity = SCNVector4Zero | |
break | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment