Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 9, 2015 13:02
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/defb9f74920227533ef1 to your computer and use it in GitHub Desktop.
Save mzsima/defb9f74920227533ef1 to your computer and use it in GitHub Desktop.
mesh board
import UIKit
import SceneKit;
class ViewController: UIViewController, SCNSceneRendererDelegate {
weak var sceneView : SCNView?
weak var meshboard : SCNNode?
var demostart = false
var lastupdate : NSTimeInterval = 0
var boardMoveTo = SCNVector3(x: 1, y: 0, z: 0)
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createMeshBoard()
createBucket()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.scene = SCNScene()
sv.backgroundColor = UIColor.brownColor()
sv.allowsCameraControl = true
sv.autoenablesDefaultLighting = true
sv.delegate = self
view.addSubview(sv)
sceneView = sv
}
func createMeshBoard() {
let board = SCNNode()
board.name = "board"
sceneView?.scene?.rootNode.addChildNode(board)
for i in 0...10 {
let barA = SCNBox(width: 9.5, height: 0.1, length: 0.1, chamferRadius: 0)
barA.firstMaterial?.diffuse.contents = UIColor.lightGrayColor()
let barANode = SCNNode(geometry: barA)
barANode.position = SCNVector3(x: 0, y: 0, z: Float(i)-5)
board.addChildNode(barANode)
let barB = SCNBox(width: 0.1, height: 0.1, length: 9.5, chamferRadius: 0)
barB.firstMaterial?.diffuse.contents = UIColor.lightGrayColor()
let barBNode = SCNNode(geometry: barB)
barBNode.position = SCNVector3(x: Float(i)-5, y: 0, z: 0)
board.addChildNode(barBNode)
}
board.physicsBody = SCNPhysicsBody.dynamicBody()
board.physicsBody?.velocityFactor = SCNVector3(x: 1, y: 0, z: 0)
board.physicsBody?.angularVelocityFactor = SCNVector3Zero
meshboard = board
}
func createBucket() {
let color = UIColor(hue: 0.6, saturation: 1, brightness: 0.6, alpha: 1)
let tube = SCNTube(innerRadius: 6, outerRadius: 6.2, height: 3)
tube.firstMaterial?.diffuse.contents = color
let tubeNode = SCNNode(geometry: tube)
tubeNode.position = SCNVector3(x: 0, y: -4, z: 0)
sceneView?.scene?.rootNode.addChildNode(tubeNode)
tubeNode.physicsBody = SCNPhysicsBody.staticBody()
tubeNode.physicsBody?.physicsShape = SCNPhysicsShape(node: tubeNode, options: [SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron])
let bottom = SCNCylinder(radius: 6.2, height: 0.2)
bottom.firstMaterial?.diffuse.contents = color
let bottomNode = SCNNode(geometry: bottom)
bottomNode.position = SCNVector3(x: 0, y: -5, z: 0)
sceneView?.scene?.rootNode.addChildNode(bottomNode)
bottomNode.physicsBody = SCNPhysicsBody.staticBody()
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
demostart = true
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
demostart = false
}
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
let interval = time - lastupdate
if demostart && interval > 0.5 {
self.lastupdate = time
let r = CGFloat(arc4random() % 5) * 0.08 + 0.2
let ball = SCNSphere(radius: r)
let color = UIColor(hue: CGFloat(arc4random() % 5) * 0.1, saturation: 0.3, brightness: 1, alpha: 1)
ball.firstMaterial?.diffuse.contents = color
let ballNode = SCNNode(geometry: ball)
ballNode.position = SCNVector3(x: 0, y: 4, z: 0.2)
sceneView?.scene?.rootNode.addChildNode(ballNode)
ballNode.physicsBody = SCNPhysicsBody.dynamicBody()
ballNode.physicsBody?.mass = 0.01
ballNode.physicsBody?.restitution = 0
}
if let board = meshboard {
if self.boardMoveTo.x > 0 {
board.physicsBody?.velocity = SCNVector3(x: 3, y: 0, z: 0)
} else {
board.physicsBody?.velocity = SCNVector3(x: -3, y: 0, z: 0)
}
let x = board.presentationNode().position.x
if fabs(x) > 0.5 {
board.position = SCNVector3(x: x > 0 ? 0.49 : -0.49 , y: 0, z: 0)
board.physicsBody?.velocity = SCNVector3Zero
self.boardMoveTo = SCNVector3(x: -self.boardMoveTo.x, y: 0, z: 0)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment