iPhone玉入れ

カップに向かって玉をころがすiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var scene : SCNScene?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.setupScene()

        self.createPlateWithHole()

        self.createCamera()

        self.createLight()

    }

    

    func setupScene() {

        var w = CGRectGetMaxX(self.view.bounds)

        var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: w, height: w))

        sceneView.center = CGPoint(x: CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds))

        sceneView.backgroundColor = UIColor(hue: 0.3, saturation: 0.5, brightness: 0.8, alpha: 1)

        sceneView.scene = SCNScene()

        self.view.addSubview(sceneView)

        

        self.scene = sceneView.scene

        self.scene?.physicsWorld.speed = 2.0

    }

    

    func createPlateWithHole() {

        

        var plate = SCNBox(width: 20, height: 0.5, length: 30, chamferRadius: 1)

        plate.firstMaterial?.diffuse.contents = UIColor(hue: 0.9, saturation: 0.5, brightness: 1, alpha: 1)

        var plateNode = SCNNode(geometry: plate)

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

        plateNode.physicsBody = SCNPhysicsBody.staticBody()

        self.scene?.rootNode.addChildNode(plateNode)

        

        var partsA = SCNBox(width: 7, height: 0.5, length: 10, chamferRadius: 1)

        var partsANode = SCNNode(geometry: partsA)

        partsANode.position = SCNVector3(x: 6.5, y: 0, z: –30)

        partsANode.physicsBody = SCNPhysicsBody.staticBody()

        self.scene?.rootNode.addChildNode(partsANode)

        

        var partsB = SCNBox(width: 7, height: 0.5, length: 10, chamferRadius: 1)

        var partsBNode = SCNNode(geometry: partsB)

        partsBNode.position = SCNVector3(x: –6.5, y: 0, z: –30)

        partsBNode.physicsBody = SCNPhysicsBody.staticBody()

        self.scene?.rootNode.addChildNode(partsBNode)

        

        var hole = SCNTube(innerRadius: 3, outerRadius: 8, height: 0.5)

        var holeNode = SCNNode(geometry: hole)

        holeNode.position = SCNVector3(x: 0, y: 0, z: –30)

        holeNode.name = “hole”

        holeNode.physicsBody = SCNPhysicsBody.staticBody()

        holeNode.physicsBody?.physicsShape = SCNPhysicsShape(geometry: hole, options: [SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeConcavePolyhedron])

        self.scene?.rootNode.addChildNode(holeNode)

    }

    

    func createCamera() {

        var camera = SCNNode()

        camera.camera = SCNCamera()

        camera.position = SCNVector3(x: 0, y: 20, z: 30)

        self.scene?.rootNode.addChildNode(camera)

        

        camera.constraints = [SCNLookAtConstraint(target: self.scene!.rootNode.childNodeWithName(“hole”, recursively: false)!)]

    }

    

    func createLight() {

        var light = SCNLight()

        light.type = SCNLightTypeOmni

        var lightNode = SCNNode()

        lightNode.light = light;

        lightNode.position = SCNVector3(x: 0, y: 20, z: 10)

        self.scene?.rootNode.addChildNode(lightNode)

    }

    

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

        var ball = SCNSphere(radius: 1)

        ball.firstMaterial?.diffuse.contents = UIColor(hue: 0.7, saturation: 0.7, brightness: 1, alpha: 1)

        var ballNode = SCNNode(geometry: ball)

        self.scene?.rootNode.addChildNode(ballNode)

        

        ballNode.physicsBody = SCNPhysicsBody.dynamicBody()

        ballNode.physicsBody?.velocity = SCNVector3(x: 0, y: 10, z: –10)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}