
サッカーのPKをやってみるiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createGoal()
createKeeper()
createBall()
createCamera()
createLight()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.scene = SCNScene()
sv.backgroundColor = UIColor(hue: 0.5, saturation: 0.1, brightness: 1, alpha: 1)
view.addSubview(sv)
sceneView = sv
}
func createGoal() {
let ground = SCNBox(width: 40, height: 0.1, length: 40, chamferRadius: 0)
ground.firstMaterial?.diffuse.contents = UIColor(hue: 0.4, saturation: 0.3, brightness: 1, alpha: 1)
let groundNode = SCNNode(geometry: ground)
groundNode.position = SCNVector3(x: 0, y: –10, z: 0)
sceneView?.scene?.rootNode.addChildNode(groundNode)
groundNode.physicsBody = SCNPhysicsBody.staticBody();
let goal = SCNNode()
goal.position = SCNVector3(x: 0, y: –2, z: –5)
sceneView?.scene?.rootNode.addChildNode(goal)
let post0 = SCNBox(width: 20, height: 1, length: 1, chamferRadius: 0)
post0.firstMaterial?.diffuse.contents = UIColor(white: 0.9, alpha: 1)
let post0Node = SCNNode(geometry: post0)
post0Node.position = SCNVector3(x: 0, y: 0, z: 0)
goal.addChildNode(post0Node)
let post1 = SCNBox(width: 1, height: 8, length: 1, chamferRadius: 0)
post1.firstMaterial?.diffuse.contents = UIColor(white: 0.9, alpha: 1)
let post1Node = SCNNode(geometry: post1)
post1Node.position = SCNVector3(x: 9.5, y: –4, z: 0)
goal.addChildNode(post1Node)
let post2Node = SCNNode(geometry: post1)
post2Node.position = SCNVector3(x: –9.5, y: –4, z: 0)
goal.addChildNode(post2Node)
post0Node.physicsBody = SCNPhysicsBody.staticBody()
post1Node.physicsBody = SCNPhysicsBody.staticBody()
post2Node.physicsBody = SCNPhysicsBody.staticBody()
}
func createKeeper() {
let keeper = SCNNode()
keeper.name = “keeper”
keeper.position = SCNVector3(x: 0, y: –5.5, z: –3.5)
sceneView?.scene?.rootNode.addChildNode(keeper)
let head = SCNBox(width: 3, height: 2, length: 1, chamferRadius: 0)
head.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let headNode = SCNNode(geometry: head)
headNode.position = SCNVector3(x: 0, y: 0.5, z: 0)
keeper.addChildNode(headNode)
let body = SCNBox(width: 1.5, height: 3, length: 1, chamferRadius: 0)
body.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let bodyNode = SCNNode(geometry: body)
bodyNode.position = SCNVector3(x: 0, y: –2, z: 0)
keeper.addChildNode(bodyNode)
let hand = SCNBox(width: 2, height: 1, length: 1, chamferRadius: 0)
hand.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let handNode = SCNNode(geometry: hand)
handNode.position = SCNVector3(x: 1.5, y: –0.8, z: 0)
handNode.rotation = SCNVector4(x: 0, y: 0, z: 1, w: 0.4)
keeper.addChildNode(handNode)
let handNode2 = SCNNode(geometry: hand)
handNode2.position = SCNVector3(x: –1.5, y: –0.8, z: 0)
handNode2.rotation = SCNVector4(x: 0, y: 0, z: 1, w: –0.4)
keeper.addChildNode(handNode2)
keeper.physicsBody = SCNPhysicsBody.dynamicBody()
keeper.physicsBody?.mass = 2
keeper.physicsBody?.angularDamping = 0.9
keeper.physicsBody?.restitution = 1
keeper.physicsBody?.friction = 1.0
}
func createBall() {
let ball = SCNSphere(radius: 1)
ball.firstMaterial?.diffuse.contents = UIColor(hue: 0.125, saturation: 0.8, brightness: 1, alpha: 1)
let ballNode = SCNNode(geometry: ball)
ballNode.name = “ball”
ballNode.position = SCNVector3(x: 0, y: –9, z: 10)
sceneView?.scene?.rootNode.addChildNode(ballNode)
ballNode.physicsBody = SCNPhysicsBody.dynamicBody()
ballNode.physicsBody?.mass = 0.2
}
func createCamera() {
let camera = SCNNode()
camera.camera = SCNCamera()
camera.position = SCNVector3(x: 0, y: –5, z: 20)
sceneView?.scene?.rootNode.addChildNode(camera)
}
func createLight() {
let light = SCNLight()
light.type = SCNLightTypeAmbient
light.color = UIColor(white: 0.5, alpha: 1)
let lightNode = SCNNode()
lightNode.light = light
sceneView?.scene?.rootNode.addChildNode(lightNode)
let spot = SCNLight()
spot.type = SCNLightTypeSpot
spot.color = UIColor(white: 0.8, alpha: 1)
let spotNode = SCNNode()
spotNode.position = SCNVector3(x: 0, y: 50, z: 100)
spotNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: –0.4)
spotNode.light = spot
sceneView?.scene?.rootNode.addChildNode(spotNode)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let jumpRight = touch.locationInView(self.view).x > CGRectGetMidX(self.view.bounds)
if let ball = sceneView?.scene?.rootNode.childNodeWithName(“ball”, recursively: false) {
let kickRight = arc4random() % 2 == 0
ball.physicsBody?.applyForce(SCNVector3(x: kickRight ? 2 : –2, y: 2, z: –5), impulse: true)
if let keeper = self.sceneView?.scene?.rootNode.childNodeWithName(“keeper”, recursively: false) {
self.delay(0.1, doit: { () -> Void in
keeper.physicsBody?.applyForce(SCNVector3(x: jumpRight ? 16 : –16, y: 12, z: 3), atPosition: SCNVector3(x: 0, y: 1, z: 0), impulse: true)
})
self.delay(4.0, doit: { () -> Void in
keeper.removeFromParentNode()
ball.removeFromParentNode()
self.createBall()
self.createKeeper()
})
}
}
}
}
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)
}
}