iPhone うずドミノ

くるっと渦巻き状に配置したドミノを倒すiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var scene : SCNScene?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.lightGrayColor()

        

        var sceneView = SCNView(frame: CGRectMake(0, 0, 320, 320))

        sceneView.backgroundColor = UIColor.greenColor()

        self.view.addSubview(sceneView)

        sceneView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))

        sceneView.scene = SCNScene()

        self.scene = sceneView.scene

        

        self.createPlate()

        self.createCamera()

        self.createDomino()

        

        var light = SCNLight()

        light.type = SCNLightTypeDirectional

        light.color = UIColor.whiteColor()

        var lightNode = SCNNode()

        lightNode.light = light

        lightNode.position = SCNVector3(x: 0, y: 50, z: 100)

        if let target = self.scene?.rootNode.childNodeWithName(“plate”, recursively: false) {

            lightNode.constraints = [SCNLookAtConstraint(target: target)]

        }

        self.scene?.rootNode.addChildNode(lightNode)

    }

    

    func createPlate()

    {

        var s = 30.0 as CGFloat

        var box = SCNBox(width: s, height: s * 0.05, length: s, chamferRadius: s*0.2)

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

        var plate = SCNNode(geometry: box)

        plate.name = “plate”

        plate.position = SCNVector3(x: 0, y: –10, z: 0)

        self.scene?.rootNode.addChildNode(plate)

        

        plate.physicsBody = SCNPhysicsBody.staticBody()

    }

    func createCamera()

    {

        var cameraNode = SCNNode()

        cameraNode.camera = SCNCamera()

        cameraNode.position = SCNVector3(x: 0, y: 10, z: 30)

        if let target = self.scene?.rootNode.childNodeWithName(“plate”, recursively: false) {

            cameraNode.constraints = [SCNLookAtConstraint(target: target)]

        }

        self.scene?.rootNode.addChildNode(cameraNode)

    }

    

    func createDomino()

    {

        for i in 032 {

            var angle = Float(M_PI/12.0) * Float(i)

            var r = 4.0 + 0.3 * Float(i)

            var b = SCNBox(width: 2, height: 4, length: 0.4, chamferRadius: 0)

            b.firstMaterial?.diffuse.contents = UIColor.redColor()

            var node = SCNNode(geometry: b)

            node.name = “domino\(i)

            node.rotation = SCNVector4(x: 0, y: –1, z: 0, w: angle)

            node.position = SCNVector3(x: r * cos(angle), y: –8, z: r * sin(angle))

            self.scene?.rootNode.addChildNode(node)

            node.physicsBody = SCNPhysicsBody.dynamicBody()

        }

    }

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        var domino = self.scene?.rootNode.childNodeWithName(“domino0”, recursively: false)

        domino?.physicsBody?.applyForce(SCNVector3(x: 0, y: 0, z: 5), atPosition: SCNVector3(x: 0, y: 1, z: 0), impulse: true)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}