
ドミノを倒して、ボールを落とすiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNScene *scene;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [self color:0];
[self setupScene];
[self createSlope];
[self createDomino];
[self createBall];
[self createCamera];
[self createButton];
}
– (void)setupScene {
float w = CGRectGetMaxX(self.view.bounds);
SCNView *sv = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, w, w)];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:3];
[self.view addSubview:sv];
self.scene = sv.scene;
}
– (void)createSlope {
SCNBox *slope = [SCNBox boxWithWidth:40 height:1 length:10 chamferRadius:0];
slope.firstMaterial.diffuse.contents = [self color:2];
SCNNode *slopeNodeA = [SCNNode nodeWithGeometry:slope];
slopeNodeA.position = SCNVector3Make(0, 10, –5);
slopeNodeA.physicsBody = [SCNPhysicsBody staticBody];
[self.scene.rootNode addChildNode:slopeNodeA];
SCNNode *slopeNodeB = [SCNNode nodeWithGeometry:slope];
slopeNodeB.position = SCNVector3Make(0, 0, 5);
slopeNodeB.transform = SCNMatrix4Rotate(slopeNodeB.transform, –M_PI/8.0, 0.1, 0, 1);
slopeNodeB.physicsBody = [SCNPhysicsBody staticBody];
[self.scene.rootNode addChildNode:slopeNodeB];
}
-(void)createDomino {
SCNBox *box = [SCNBox boxWithWidth:0.7 height:4 length:2 chamferRadius:0.2];
box.firstMaterial.diffuse.contents = [self color:4];
for (int i=0; i<10; i++) {
SCNNode *domino = [SCNNode nodeWithGeometry:box];
domino.name = [NSString stringWithFormat:@”domino%d”, i];
if (i==0) domino.transform = SCNMatrix4Rotate(domino.transform, M_PI/6.0, 0, 1, 0);
domino.position = SCNVector3Make(i * 3 – 8, 12, –5);
domino.physicsBody = [SCNPhysicsBody dynamicBody];
[self.scene.rootNode addChildNode:domino];
}
}
– (void)createBall {
SCNSphere *ball = [SCNSphere sphereWithRadius:3];
ball.firstMaterial.diffuse.contents = [self color:1];
SCNNode *ballNode = [SCNNode nodeWithGeometry:ball];
ballNode.position = SCNVector3Make(-12, 14, –2);
ballNode.physicsBody = [SCNPhysicsBody dynamicBody];
ballNode.physicsBody.physicsShape = [SCNPhysicsShape shapeWithGeometry:ball options:nil];
[self.scene.rootNode addChildNode:ballNode];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 40, 40);
camera.rotation = SCNVector4Make(1, 0, 0, – 40.0 * (M_PI/180.0));
[self.scene.rootNode addChildNode:camera];
}
– (void)createButton {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@”PUSH” forState:UIControlStateNormal];
[btn setTitleColor:[self color:3] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:30];
[btn sizeToFit];
btn.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)+70);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
}
– (void)push {
SCNNode *domino = [self.scene.rootNode childNodeWithName:@”domino9″ recursively:NO];
[domino.physicsBody applyForce:SCNVector3Make(-3, 0, 0) atPosition:SCNVector3Make(0, 2, 0) impulse:YES];
}
#define ColorHex(rgbValue) [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0]
– (UIColor *)color:(int)i {
int hex[] = {0xB9121B, 0x4C1B1B, 0xF6E497, 0xFCFAE1, 0xBD8D46};
return ColorHex(hex[i]);
}
@end