Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 15, 2015 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/3d824b919c17afc2371f to your computer and use it in GitHub Desktop.
Save mzsima/3d824b919c17afc2371f to your computer and use it in GitHub Desktop.
radial ball
//
// ViewController.swift
// radialBall
//
// Created by Mizushima Yusuke on 8/15/15.
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved.
//
import UIKit
import SceneKit
class ViewController: UIViewController, SCNSceneRendererDelegate {
weak var sceneView : SCNView?
var radialNode = SCNNode()
var ballArr = Array<SCNNode>()
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createGround()
createCamera()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor(hue: 0.3, saturation: 0.1, brightness: 0.1, alpha: 1)
sv.scene = SCNScene()
sv.autoenablesDefaultLighting = true
sv.allowsCameraControl = true
sv.delegate = self
view.addSubview(sv)
self.sceneView = sv
}
func createGround() {
let ground = SCNBox(width: 15, height: 0.1, length: 15, chamferRadius: 0)
ground.firstMaterial?.diffuse.contents = UIColor(hue: 0.6, saturation: 0.5, brightness: 0.7, alpha: 1)
let groundNode = SCNNode(geometry: ground)
groundNode.position = SCNVector3(x: 0, y: -1, z: 0)
groundNode.physicsBody = SCNPhysicsBody.staticBody()
groundNode.physicsBody?.restitution = 0.8
groundNode.physicsBody?.categoryBitMask = 1 << 2
groundNode.physicsBody?.collisionBitMask = 1 << 1
sceneView?.scene?.rootNode.addChildNode(groundNode)
}
func createBall() -> SCNNode {
let ball = SCNSphere(radius: 0.2)
ball.firstMaterial?.diffuse.contents = UIColor.whiteColor()
let ballNode = SCNNode(geometry: ball)
return ballNode
}
func createCamera() {
let camera = SCNNode()
camera.camera = SCNCamera()
camera.position = SCNVector3(x: 0, y: 4, z: 10)
camera.rotation = SCNVector4(x: 1, y: 0, z: 0, w: -0.4)
sceneView?.scene?.rootNode.addChildNode(camera)
}
func runRadialAction() {
let dw = Float(M_PI) / 5.0
for i in 0...9 {
let ball = createBall()
ball.name = "\(i)"
ball.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(i) * dw)
ball.physicsBody = SCNPhysicsBody.dynamicBody()
ball.physicsBody?.categoryBitMask = 1 << 1
ball.physicsBody?.collisionBitMask = 1 << 2
ball.physicsBody?.restitution = 0.95
sceneView?.scene?.rootNode.addChildNode(ball)
let dx = 3 * cos(Float(i) * dw)
let dz = 3 * sin(Float(i) * dw)
ball.physicsBody?.applyForce(SCNVector3(x: dx, y: 8, z: dz), impulse: true)
self.ballArr.append(ball)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.sceneView?.scene?.rootNode.addChildNode(radialNode)
runRadialAction()
}
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
self.ballArr.foreach { i in
let dot = SCNBox(width: 0.04, height: 0.04, length: 0.04, chamferRadius: 0)
dot.firstMaterial?.diffuse.contents = UIColor(hue: CGFloat(i.name!.toInt()!) * 0.1, saturation: 0.4, brightness: 1, alpha: 1)
let dotNode = SCNNode(geometry: dot)
dotNode.position = i.presentationNode().position
self.radialNode.addChildNode(dotNode)
if i.position.y < 0 { i.removeFromParentNode() }
}
}
}
extension Array {
func foreach(doit:T -> Void) {
for i in self {doit(i)}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment