
トントン相撲をするiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createDohyo()
createRikishi()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor(hue: 0.25, saturation: 0.5, brightness: 1, alpha: 1)
sv.scene = SCNScene()
sv.allowsCameraControl = true
sv.autoenablesDefaultLighting = true
view.addSubview(sv)
sceneView = sv
}
func createDohyo() {
let base = SCNCone(topRadius: 5, bottomRadius: 6, height: 1)
base.firstMaterial?.diffuse.contents = UIColor(hue: 0.14, saturation: 0.4, brightness: 0.9, alpha: 1)
let baseNode = SCNNode(geometry: base)
baseNode.physicsBody = SCNPhysicsBody.staticBody()
sceneView?.scene?.rootNode.addChildNode(baseNode)
let nawa = SCNTorus(ringRadius: 4.5, pipeRadius: 0.2)
nawa.firstMaterial?.diffuse.contents = UIColor(hue: 0.15, saturation: 0.6, brightness: 0.3, alpha: 1)
let nawaNode = SCNNode(geometry: nawa)
nawaNode.position = SCNVector3(x: 0, y: 0.5, z: 0)
nawaNode.physicsBody = SCNPhysicsBody.staticBody()
sceneView?.scene?.rootNode.addChildNode(nawaNode)
}
func createRikishi() {
for j in 0…1 {
let rikishi = SCNBox(width: 1, height: 1.6, length: 1, chamferRadius: 0)
rikishi.firstMaterial?.diffuse.contents = UIColor(hue: 0.12, saturation: 0.3, brightness: 1, alpha: 1)
let rikishiNode = SCNNode(geometry: rikishi)
rikishiNode.name = “rikishi \(j)”
if j == 0 {
rikishiNode.position = SCNVector3(x: –2.5, y: 2, z: 0)
rikishiNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI * 0.25))
} else {
rikishiNode.position = SCNVector3(x: 2.5, y: 2, z: 0)
rikishiNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI * 1.25))
}
let hear = SCNBox(width: 1, height: 0.2, length: 0.2, chamferRadius: 0.1)
hear.firstMaterial?.diffuse.contents = UIColor.blackColor()
let hearNode = SCNNode(geometry: hear)
hearNode.position = SCNVector3(x: 0, y: 1, z: 0)
hearNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(-M_PI * 0.25))
rikishiNode.addChildNode(hearNode)
for i in 0…1 {
let eye = SCNBox(width: 0.5, height: 0.5, length: 0.1, chamferRadius: 0)
eye.firstMaterial?.diffuse.contents = UIColor.whiteColor()
let eyeNode = SCNNode(geometry: eye)
eyeNode.position = SCNVector3(x: 0.1, y: 0.5, z: 0.5)
rikishiNode.addChildNode(eyeNode)
let black = SCNBox(width: 0.1, height: 0.1, length: 0.05, chamferRadius: 0)
black.firstMaterial?.diffuse.contents = UIColor.blackColor()
let blackNode = SCNNode(geometry: black)
blackNode.position = SCNVector3(x: 0, y: 0, z: 0.1)
eyeNode.addChildNode(blackNode)
if i == 1{
eyeNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI * 0.5))
eyeNode.position = SCNVector3(x: 0.5, y: 0.5, z: 0.1)
}
let arm = SCNBox(width: 1, height: 0.4, length: 0.4, chamferRadius: 0)
arm.firstMaterial?.diffuse.contents = UIColor(hue: 0.12, saturation: 0.3, brightness: 1, alpha: 1)
let armNode = SCNNode(geometry: arm)
if i==0 {
armNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: –Float(M_PI * 0.25))
armNode.transform = SCNMatrix4Translate(armNode.transform, 0, –0.1, 0.8)
} else {
armNode.transform = SCNMatrix4Translate(armNode.transform, 0.5, –0.1, –0.6)
armNode.transform = SCNMatrix4Rotate(armNode.transform, –Float(M_PI * 0.25), 0, 1, 0)
}
rikishiNode.addChildNode(armNode)
rikishiNode.physicsBody = SCNPhysicsBody.dynamicBody()
sceneView?.scene?.rootNode.addChildNode(rikishiNode)
}
}
}
func nokotta() {
let rikishi0 = sceneView?.scene?.rootNode.childNodeWithName(“rikishi 0”, recursively: false)
let rikishi1 = sceneView?.scene?.rootNode.childNodeWithName(“rikishi 1”, recursively: false)
let dx = rikishi0!.presentationNode().position.x – rikishi1!.presentationNode().position.x
let dz = rikishi0!.presentationNode().position.z – rikishi1!.presentationNode().position.z
rikishi0?.physicsBody?.applyForce(SCNVector3(x: -dx * 2.5, y: 1.5, z: -dz * 2.0), impulse: true)
rikishi1?.physicsBody?.applyForce(SCNVector3(x: dx * 2.5, y: 1.5, z: dz * 2.0), impulse: true)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
nokotta()
}
}