Skip to content

Instantly share code, notes, and snippets.

@Danappelxx
Created December 18, 2015 04:48
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 Danappelxx/61fc8bd89ebc6fca2733 to your computer and use it in GitHub Desktop.
Save Danappelxx/61fc8bd89ebc6fca2733 to your computer and use it in GitHub Desktop.
//
// GameViewController.swift
// WordMatch
//
// Created by Dan Appel on 12/17/15.
// Copyright (c) 2015 Dan Appel. All rights reserved.
//
import UIKit
import QuartzCore
import SceneKit
class GameViewController: UIViewController {
var boxNode = SCNNode()
typealias Vector2 = (x: Float, y: Float)
var rotationAngle = Vector2(x: 0, y: 0)
var distance: Double = 1
var scnView: SCNView {
return self.view as! SCNView
}
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
setupBox()
lights()
camera()
action()
}
func setupScene() {
let scene = SCNScene()
scnView.scene = scene
scnView.showsStatistics = true
scnView.allowsCameraControl = true
}
func setupBox() {
let box1 = SCNBox(width: 10, height: 10, length: 10, chamferRadius: 1.0)
let box2 = SCNBox(width: 10, height: 10, length: 10, chamferRadius: 1.0)
let subBoxNode1 = SCNNode(geometry: box1)
subBoxNode1.transform = SCNMatrix4MakeTranslation(7, 0, 0)
let subBoxNode2 = SCNNode(geometry: box2)
subBoxNode2.transform = SCNMatrix4MakeTranslation(-7, 0, 0)
boxNode.addChildNode(subBoxNode1)
boxNode.addChildNode(subBoxNode2)
scnView.scene!.rootNode.addChildNode(boxNode)
}
func lights() {
let ambientLightNode = SCNNode()
let ambientLight = SCNLight()
ambientLight.type = SCNLightTypeAmbient
ambientLight.color = UIColor(white: 0.67, alpha: 1)
ambientLightNode.light = ambientLight
scnView.scene!.rootNode.addChildNode(ambientLightNode)
let omniLightNode = SCNNode()
let omniLight = SCNLight()
omniLight.type = SCNLightTypeOmni
omniLight.color = UIColor(white: 0.3, alpha: 1)
omniLightNode.light = omniLight
omniLightNode.position = SCNVector3Make(0, 50, 50)
scnView.scene!.rootNode.addChildNode(omniLightNode)
}
func camera() {
let cameraNode = SCNNode()
let camera = SCNCamera()
cameraNode.camera = camera
cameraNode.position = SCNVector3Make(0, 0, 50)
scnView.scene!.rootNode.addChildNode(cameraNode)
}
func action() {
let panGesture = UIPanGestureRecognizer(target: self, action: "handlePan:")
scnView.addGestureRecognizer(panGesture)
}
func handlePan(gestureRecognizer: UIPanGestureRecognizer) {
let translation = gestureRecognizer.translationInView(gestureRecognizer.view!)
// for some reason these are flipped
let xAngle = translation.y.toRadians() + rotationAngle.x
let yAngle = translation.x.toRadians() + rotationAngle.y
let xRotation = SCNMatrix4MakeRotation(xAngle, 1, 0, 0)
let yRotation = SCNMatrix4MakeRotation(yAngle, 0, 1, 0)
let rotation = SCNMatrix4Mult(xRotation, yRotation)
boxNode.transform = rotation
if gestureRecognizer.state == .Ended {
rotationAngle.x += xAngle
rotationAngle.y += yAngle
// 2 radians per circle, therefore radians % 2 gives us actual rotation
rotationAngle.x = rotationAngle.x % 2
rotationAngle.y = rotationAngle.y % 2
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}
extension CGFloat {
func toRadians() -> Float {
// 1 pixel = 1 degree
// degrees -> radians:
//
// π radians
// degree x ----------- = radians
// 180 degrees
//
// ex: 180 degrees -> pi radians
return Float(self) * Float(M_PI)/180.0
}
}
extension CGVector {
var x: Float {
return Float(self.dx)
}
var y: Float {
return Float(self.dy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment