Last active
          April 8, 2016 11:23 
        
      - 
      
- 
        Save aqubi/d5a71db1b734d9b7baa94fb3a221cb75 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import UIKit | |
| import SceneKit | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var sceneView: SCNView! | |
| override func loadView() { | |
| super.loadView() | |
| let scene = SCNScene() | |
| self.sceneView.scene = scene | |
| sceneView.autoenablesDefaultLighting = true | |
| setupScene(scene) | |
| let y:Float = 8 | |
| //反発係数を変えた3つ球体を追加 | |
| scene.rootNode.addChildNode(createSphere(SCNVector3Make(-1.5, y, 0), restitution: 0.5)) | |
| scene.rootNode.addChildNode(createSphere(SCNVector3Make(0, y, 0), restitution: 1.0)) | |
| scene.rootNode.addChildNode(createSphere(SCNVector3Make(1.5, y, 0), restitution: 1.5)) | |
| //4秒後にもう一つ球体を追加 | |
| dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { | |
| scene.rootNode.addChildNode(self.createSphere(SCNVector3Make(-1, y, 0), restitution: 1.0)) | |
| }) | |
| } | |
| private func setupScene(scene:SCNScene) { | |
| //カメラ追加 | |
| let cameraNode = SCNNode() | |
| cameraNode.camera = SCNCamera() | |
| cameraNode.position = SCNVector3(x: 0, y: 1, z: 15) | |
| scene.rootNode.addChildNode(cameraNode) | |
| //床を作成 | |
| let floor = SCNFloor() | |
| floor.reflectivity = 0.25 | |
| let floorNode = SCNNode(geometry: floor) | |
| let floorShape = SCNPhysicsShape(geometry: floor, options: nil) | |
| let floorBody = SCNPhysicsBody(type: .Static, shape: floorShape) | |
| floorNode.physicsBody = floorBody | |
| scene.rootNode.addChildNode(floorNode) | |
| } | |
| //球体を作成 | |
| private func createSphere(position:SCNVector3, restitution:CGFloat) -> SCNNode { | |
| let sphere = SCNSphere(radius: 0.5) | |
| let node = SCNNode(geometry: sphere) | |
| node.position = position | |
| if let material = node.geometry?.firstMaterial { | |
| material.diffuse.contents = UIColor.redColor() | |
| material.specular.contents = UIColor.whiteColor() | |
| } | |
| let physicsShape = SCNPhysicsShape(geometry: sphere, options: nil) | |
| let physicsBody = SCNPhysicsBody(type: .Dynamic, shape: physicsShape) | |
| physicsBody.restitution = restitution | |
| node.physicsBody = physicsBody | |
| return node | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment