Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 12, 2015 13:40
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/8c75fa42677219defe3e to your computer and use it in GitHub Desktop.
Save mzsima/8c75fa42677219defe3e to your computer and use it in GitHub Desktop.
Pull Block
//
// ViewController.swift
// PullBlock
//
// Created by MizushimaYusuke on 8/12/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
import UIKit
import SceneKit
class ViewController: UIViewController, SCNSceneRendererDelegate {
weak var sceneView : SCNView?
weak var selectedNode : SCNNode?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createGround()
createJumpBlock()
createBall()
createCamera()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.scene = SCNScene()
sv.backgroundColor = UIColor(hue: 0.6, saturation: 0.2, brightness: 1, alpha: 1)
sv.autoenablesDefaultLighting = true
sv.delegate = self
view.addSubview(sv)
sceneView = sv
}
func createGround() {
let n = 20
var node = SCNNode()
for i in 0...n {
let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.1)
box.firstMaterial?.diffuse.contents = UIColor(hue: 0.12, saturation: 1, brightness: 0.6, alpha: 1)
let boxNode = SCNNode(geometry: box)
boxNode.position = SCNVector3(x: Float(i) - Float(n) / 2.0, y: 0, z: 0)
node.addChildNode(boxNode)
}
node = node.flattenedClone()
node.physicsBody = SCNPhysicsBody.staticBody()
node.physicsBody?.categoryBitMask = 1 << 1
node.physicsBody?.collisionBitMask = 1 << 1
sceneView?.scene?.rootNode.addChildNode(node)
}
func createJumpBlock() {
let box = SCNBox(width: 1.1, height: 1, length: 1.1, chamferRadius: 0.1)
box.firstMaterial?.diffuse.contents = UIColor.whiteColor()
let boxNode = SCNNode(geometry: box)
boxNode.position = SCNVector3(x: -5, y: 0, z: 0)
boxNode.name = "jump block"
boxNode.physicsBody = SCNPhysicsBody.dynamicBody()
boxNode.physicsBody?.categoryBitMask = 1 << 2
boxNode.physicsBody?.collisionBitMask = 1 << 2
boxNode.physicsBody?.damping = 0.95
boxNode.physicsBody?.angularVelocityFactor = SCNVector3Zero
boxNode.physicsBody?.velocityFactor = SCNVector3(x: 0, y: 1, z: 0)
sceneView?.scene?.rootNode.addChildNode(boxNode)
}
func createBall() {
let ball = SCNSphere(radius: 0.8)
ball.firstMaterial?.diffuse.contents = UIColor(hue: 0, saturation: 0.9, brightness: 0.7, alpha: 1)
let ballNode = SCNNode(geometry: ball)
ballNode.position = SCNVector3(x: -10, y: 2, z: 0)
ballNode.physicsBody = SCNPhysicsBody.dynamicBody()
ballNode.physicsBody?.categoryBitMask = 1 << 2 | 1 << 1
ballNode.physicsBody?.collisionBitMask = 1 << 2 | 1 << 1
ballNode.physicsBody?.velocityFactor = SCNVector3(x: 1, y: 1, z: 0)
sceneView?.scene?.rootNode.addChildNode(ballNode)
ballNode.physicsBody?.applyForce(SCNVector3(x: 3, y: 0, z: 0), impulse: true)
}
func createCamera() {
let camera = SCNNode()
camera.camera = SCNCamera()
camera.position = SCNVector3(x: 0, y: 3, z: 12)
sceneView?.scene?.rootNode.addChildNode(camera)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let p = touch.locationInView(self.sceneView)
if let hit = self.sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey : true])?.first as? SCNHitTestResult {
if hit.node.name == "jump block" {
hit.node.physicsBody?.type = .Kinematic
self.selectedNode = hit.node
}
} else {
createBall()
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let p = touch.locationInView(self.sceneView)
let y = self.sceneView!.unprojectPoint(SCNVector3(x: Float(p.x), y: Float(p.y), z: 0.94)).y
self.selectedNode?.position = SCNVector3(x: self.selectedNode!.position.x, y: y, z: 0)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
self.selectedNode?.physicsBody?.type = .Dynamic
self.selectedNode = nil
}
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
if let jumpblock = sceneView?.scene?.rootNode.childNodeWithName("jump block", recursively: false) {
let d = jumpblock.presentationNode().position.y
if d != 0 {
let antiG : Float = 0.175
let fy = -(d / fabs(d)) * fabs(pow(d, 3)) + antiG
jumpblock.physicsBody?.applyForce(SCNVector3(x: 0, y: Float(fy), z: 0), impulse: true)
}
}
}
}
@mzsima
Copy link
Author

mzsima commented Aug 12, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment