スタートゲートから一斉に飛び出すiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, strong) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createGround];
[self createGate];
[self createHorse];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [self color:0];
sv.scene = [SCNScene scene];
sv.autoenablesDefaultLighting = YES;
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createGround {
SCNBox *box = [SCNBox boxWithWidth:10 height:0.2 length:20 chamferRadius:0];
box.firstMaterial.diffuse.contents = [self color:4];
SCNNode *ground = [SCNNode nodeWithGeometry:box];
[self.sceneView.scene.rootNode addChildNode:ground];
ground.physicsBody = [SCNPhysicsBody staticBody];
}
– (void)createGate {
for (int i=0; i<5; i++) {
SCNCylinder *c = [SCNCylinder cylinderWithRadius:0.2 height:4];
c.firstMaterial.diffuse.contents = [self color:1];
SCNNode *pole = [SCNNode nodeWithGeometry:c];
pole.position = SCNVector3Make(i * 2.3 – 4.6, 2, –3);
[self.sceneView.scene.rootNode addChildNode:pole];
pole.physicsBody = [SCNPhysicsBody staticBody];
if (i != 0) {
SCNBox *b = [SCNBox boxWithWidth:2 height:2.6 length:0.2 chamferRadius:0.2];
b.firstMaterial.diffuse.contents = [self color:1];
SCNNode *door = [SCNNode nodeWithGeometry:b];
door.name = @”door”;
door.pivot = SCNMatrix4MakeTranslation(1, 0, 0);
door.position = SCNVector3Make(pole.position.x, 2, –3);
[self.sceneView.scene.rootNode addChildNode:door];
}
}
}
– (void)createHorse {
for (int i=0; i<4; i++) {
UIColor *color = (i%2) ? [self color:2] : [self color:3];
SCNNode *horse = [SCNNode node];
horse.name = @”horse”;
horse.position = SCNVector3Make(i * 2.3 – 3.4, 1, –7);
[self.sceneView.scene.rootNode addChildNode:horse];
SCNBox *head = [SCNBox boxWithWidth:1 height:1 length:2 chamferRadius:0];
head.firstMaterial.diffuse.contents = color;
SCNNode *headNode = [SCNNode nodeWithGeometry:head];
headNode.position = SCNVector3Make(0, 2, 1.5);
[horse addChildNode:headNode];
SCNBox *body = [SCNBox boxWithWidth:1 height:1 length:3 chamferRadius:0];
body.firstMaterial.diffuse.contents = color;
SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];
bodyNode.position = SCNVector3Make(0, 1, 0);
[horse addChildNode:bodyNode];
for (int j=0; j<2; j++ ) {
SCNBox *f = [SCNBox boxWithWidth:1 height:1.5 length:1 chamferRadius:0];
f.firstMaterial.diffuse.contents = color;
SCNNode *foot = [SCNNode nodeWithGeometry:f];
foot.position = SCNVector3Make(0, 0, j ? –1 : 1);
[horse addChildNode:foot];
}
horse.physicsBody = [SCNPhysicsBody dynamicBody];
}
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(-10, 10, 10);
camera.rotation = SCNVector4Make(0.4, 0.5, 0, –0.8);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
if ([child.name isEqual:@”door”]) {
[child runAction:[SCNAction rotateToX:0 y:M_PI/2.0 z:0 duration:0.1]];
}}];
[self.sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
if ([child.name isEqual:@”horse”]) {
float randnum = (arc4random() % 10) * 0.3 + 0.1;
[child.physicsBody applyForce:SCNVector3Make(0, 1, randnum) atPosition:SCNVector3Make(0, 0, 1) impulse:YES];
}
}];
}
#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000) >> 16) / 255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]
– (UIColor *)color:(int)i {
if (i > 4) {
return nil;
}
int colorCode[] = {0xACF0F2, 0xF3FFE2, 0xEB7F00, 0x225378, 0x595241};
return ColorHex(colorCode[i]);
}
@end