ランダムにボールを移動させる土管を表示するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createGround];
[self createTube];
[self createCamera];
[self createBall];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:2];
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createGround {
SCNBox *box = [SCNBox boxWithWidth:40 height:10 length:40 chamferRadius:0];
box.firstMaterial.diffuse.contents = [self color:0];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
– (void)createTube {
for (int i=0; i<4; i++) {
float x = (i % 2) == 0 ? –10 : 10;
float z = (i / 2) == 0 ? –10 : 10;
SCNTube *t = [SCNTube tubeWithInnerRadius:4 outerRadius:4.5 height:5];
t.firstMaterial.diffuse.contents =[self color:4];
SCNNode *tNode = [SCNNode nodeWithGeometry:t];
tNode.name = [NSString stringWithFormat:@”tube %d”, i];
tNode.position = SCNVector3Make(x, 8, z);
[self.sceneView.scene.rootNode addChildNode:tNode];
}
}
– (void)createBall {
SCNSphere *ball = [SCNSphere sphereWithRadius:3];
ball.firstMaterial.diffuse.contents = [self color:1];
SCNNode *ballNode = [SCNNode nodeWithGeometry:ball];
ballNode.name = @”ball”;
ballNode.position = SCNVector3Make(0, 30, 0);
[self.sceneView.scene.rootNode addChildNode:ballNode];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 50, 70);
camera.rotation = SCNVector4Make(1, 0, 0, – 0.5);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint p = [[touches anyObject] locationInView:self.sceneView];
SCNNode *hit = [[self.sceneView hitTest:p options:@{SCNHitTestSortResultsKey:@YES}].firstObject node];
if ([hit.name hasPrefix:@”tube”]) {
SCNNode *ball = [self.sceneView.scene.rootNode childNodeWithName:@”ball” recursively:NO];
SCNNode *nextTube = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”tube %d”, arc4random_uniform(4)] recursively:NO];
[ball runAction:[SCNAction sequence:@[
[SCNAction moveTo:SCNVector3Make(hit.position.x, 30, hit.position.z) duration:0.5],
[SCNAction moveTo:SCNVector3Make(hit.position.x, –10, hit.position.z) duration:0.5],
[SCNAction moveTo:SCNVector3Make(nextTube.position.x, –10, nextTube.position.z) duration:0.2],
[SCNAction moveTo:SCNVector3Make(nextTube.position.x, 30, nextTube.position.z) duration:0.2],
]]];
}
}
#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[] = {0x493F0B, 0xA7C520, 0xF5F6D4, 0xCDE855, 0x85DB18};
return ColorHex(colorCode[i]);
}
@end