iPhoneワッフル

ワッフルをフライパンからトスするiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createFrypan()

        createDish()

        createWaffle()

        createCamera()

    }

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.backgroundColor = UIColor(hue: 0.25, saturation: 0.2, brightness: 0.4, alpha:1)

        sv.scene = SCNScene()

        sv.autoenablesDefaultLighting = true

        sv.allowsCameraControl = true

        view.addSubview(sv)

        sceneView = sv

    }

    

    func createFrypan() {

        let color = UIColor(white: 0.3, alpha: 1)

        let pan = SCNBox(width: 3, height: 0.1, length: 3, chamferRadius: 0.1)

        pan.firstMaterial?.diffuse.contents = color

        let panNode = SCNNode(geometry: pan)

        panNode.name = “frypan”

        panNode.position = SCNVector3(x: 4, y: 0, z: 0)

        

        let handle = SCNBox(width: 0.7, height: 0.1, length: 1, chamferRadius: 0)

        handle.firstMaterial?.diffuse.contents = color

        let handleNode = SCNNode(geometry: handle)

        handleNode.position = SCNVector3(x: 0, y: 0, z: 2)

        panNode.addChildNode(handleNode)

        

        sceneView?.scene?.rootNode.addChildNode(panNode)

        panNode.physicsBody = SCNPhysicsBody.staticBody()

    }

    

    func createDish() {

        let dish = SCNCylinder(radius: 2, height: 0.2)

        dish.firstMaterial?.diffuse.contents = UIColor.whiteColor()

        dish.firstMaterial?.lightingModelName = SCNLightingModelConstant

        let dishNode = SCNNode(geometry: dish)

        dishNode.position = SCNVector3(x: –2, y: 0, z: 0)

        sceneView?.scene?.rootNode.addChildNode(dishNode)

        

        dishNode.physicsBody = SCNPhysicsBody.staticBody()

    }

    

    func createWaffle() {

        let base = SCNBox(width: 2.9, height: 0.3, length: 2.9, chamferRadius: 0.2)

        base.firstMaterial?.diffuse.contents = UIColor.brownColor()

        let waffle = SCNNode(geometry: base)

        waffle.name = “waffle”

        

        for i in 04 {

            let x = (i < 2) ? Float(i % 2) – 0.5 : 0

            let z = (i < 2) ? 0 : Float(i % 2) – 0.5

            

            let bar = SCNBox(width: 3, height: 0.5, length: 0.2, chamferRadius: 0.1)

            bar.firstMaterial?.diffuse.contents = UIColor.brownColor()

            let barNode = SCNNode(geometry: bar)

            barNode.position = SCNVector3(x: x, y: 0, z: z)

            if i < 2 {

                barNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI) * 0.5)

            }

            waffle.addChildNode(barNode)

        }

        waffle.position = SCNVector3(x: 4, y: 2, z: 0)

        sceneView?.scene?.rootNode.addChildNode(waffle)

        

        waffle.physicsBody = SCNPhysicsBody.dynamicBody()

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.position = SCNVector3(x: 1, y: 4, z: 8)

        camera.rotation = SCNVector4(x: 1, y: 0, z: 0, w: –0.3)

        sceneView?.scene?.rootNode.addChildNode(camera)

    }

    

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        let waffle = sceneView?.scene?.rootNode.childNodeWithName(“waffle”, recursively: false)

        waffle?.physicsBody?.applyForce(SCNVector3(x: –4.1, y: 5.7, z: 0), atPosition: SCNVector3(x: 1.5, y: 0, z: 0), impulse: true)

        waffle?.name = “”

        

        delay(0.5) {

            self.createWaffle()

        }

    }

    

    final func delay(delay:Double, doit:() -> Void) {

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), doit)

    }

    

}