
箱が自動ゲートを通るiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController () <SCNSceneRendererDelegate>
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int count;
@property (nonatomic) BOOL start;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createGate];
[self createBox];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor colorWithHue:0.28 saturation:0.1 brightness:1 alpha:1];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
sv.autoenablesDefaultLighting = YES;
sv.delegate = self;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createGate {
SCNBox *road = [SCNBox boxWithWidth:20 height:0.1 length:3 chamferRadius:0];
road.firstMaterial.diffuse.contents = [UIColor lightGrayColor];
SCNNode *roadNode = [SCNNode nodeWithGeometry:road];
roadNode.position = SCNVector3Make(0, –1, 0);
roadNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:roadNode];
SCNCylinder *pole = [SCNCylinder cylinderWithRadius:0.5 height:2];
pole.firstMaterial.diffuse.contents = [UIColor grayColor];
SCNNode *poleNode = [SCNNode nodeWithGeometry:pole];
poleNode.position = SCNVector3Make(0, 0, –1.5);
[self.sceneView.scene.rootNode addChildNode:poleNode];
SCNBox *bar = [SCNBox boxWithWidth:0.2 height:1 length:3 chamferRadius:0.1];
bar.firstMaterial.diffuse.contents = [UIColor redColor];
SCNNode *barNode = [SCNNode nodeWithGeometry:bar];
barNode.name = @”bar”;
barNode.pivot = SCNMatrix4MakeTranslation(0, 0, –1.5);
barNode.position = SCNVector3Make(0, 0.6, –1.5);
[self.sceneView.scene.rootNode addChildNode:barNode];
SCNBox *panel = [SCNBox boxWithWidth:4 height:0.01 length:3 chamferRadius:1];
panel.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode *panelNode =[SCNNode nodeWithGeometry:panel];
panelNode.position = SCNVector3Make(0, –0.95, 0);
[self.sceneView.scene.rootNode addChildNode:panelNode];
}
– (void)createBox {
for (int i=0; i<5; i++) {
SCNBox *box = [SCNBox boxWithWidth:1.5 height:1.5 length:1.5 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.15 * i saturation:0.5 brightness:1 alpha:1];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.name = [NSString stringWithFormat:@”box %d”, i];
boxNode.position = SCNVector3Make(-9, 2 * i, 0);
[self.sceneView.scene.rootNode addChildNode:boxNode];
boxNode.physicsBody = [SCNPhysicsBody dynamicBody];
boxNode.physicsBody.angularVelocityFactor = SCNVector3Make(0, 0, 0);
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.start = YES;
self.sceneView.playing = YES;
}
– (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time {
if (!self.start) {
return;
}
SCNNode *box = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”box %d”, self.count] recursively:NO];
if (box.presentationNode.position.x < 10.0) {
box.physicsBody.velocity = SCNVector3Make(5, 0, 0);
} else {
self.count++;
}
SCNNode *bar = [self.sceneView.scene.rootNode childNodeWithName:@”bar” recursively:NO];
if (box.presentationNode.position.x > –2.5 && box.presentationNode.position.x < 1.0) {
[bar runAction:[SCNAction rotateToX:-M_PI * 0.5 y:0 z:0 duration:0.2]];
} else {
[bar runAction:[SCNAction rotateToX:0 y:0 z:0 duration:0.2]];
}
}
@end