
四角がすべり台をすべる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 createSlide];
[self createCamera];
[self createBall];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:0];
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createGround {
SCNCylinder *ground = [SCNCylinder cylinderWithRadius:30 height:1];
ground.firstMaterial.diffuse.contents = [self color:1];
SCNNode *groundNode = [SCNNode nodeWithGeometry:ground];
groundNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:groundNode];
}
– (void)createSlide {
for (int i=0; i<4; i++) {
SCNBox *stair = [SCNBox boxWithWidth:4 height:4 + i * 2.5 length:10 chamferRadius:0];
stair.firstMaterial.diffuse.contents = [self color:2];
SCNNode *stairNode = [SCNNode nodeWithGeometry:stair];
stairNode.position = SCNVector3Make(-15 + i * 4, i * 2, 0);
stairNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:stairNode];
}
SCNBox *plate = [SCNBox boxWithWidth:25 height:1 length:10 chamferRadius:0];
plate.firstMaterial.diffuse.contents = [self color:3];
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
plateNode.position = SCNVector3Make(11, 5.5, 0);
plateNode.rotation = SCNVector4Make(0, 0, 1, –0.4);
plateNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:plateNode];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.zFar = 300;
camera.position = SCNVector3Make(0, 50, 100);
camera.rotation = SCNVector4Make(1, 0, 0, –0.3);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)createBall {
SCNBox *box = [SCNBox boxWithWidth:3 height:3 length:3 chamferRadius:0.1];
box.firstMaterial.diffuse.contents = [self color:4];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.name = @”box”;
boxNode.position = SCNVector3Make(-20, 5, 0);
boxNode.physicsBody = [SCNPhysicsBody dynamicBody];
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SCNNode *node = [self.sceneView.scene.rootNode childNodeWithName:@”box” recursively:NO];
[node.physicsBody applyForce:SCNVector3Make(1, 10, 0) impulse:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[node.physicsBody applyForce:SCNVector3Make(4, 0, 0) 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]
– (UIColor *)color:(int)i {
if (i > 4) return nil;
int colorCode[] = {0xCFCA4C, 0xFCF5BF, 0x9FE5C2, 0x5EB299, 0x745A33};
return ColorHex(colorCode[i]);
}
@end