Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 10, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/e592a2c56a4dbf86151b to your computer and use it in GitHub Desktop.
Save mzsima/e592a2c56a4dbf86151b to your computer and use it in GitHub Desktop.
karakuri ring
//
// ViewController.m
// karakuri01
//
// Created by Mizushima Yusuke on 8/10/15.
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved.
//
#import "ViewController.h"
@import SceneKit;
@interface ViewController () <SCNSceneRendererDelegate>
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBox];
[self createDisk];
[self createWall];
[self createCamera];
}
- (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [self color:2];
sv.scene = [SCNScene scene];
sv.delegate = self;
sv.autoenablesDefaultLighting = YES;
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
- (void)createBox {
SCNBox *box = [SCNBox boxWithWidth:0.8 height:0.8 length:0.8 chamferRadius:0.1];
box.firstMaterial.diffuse.contents = [self color:4];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.position = SCNVector3Make(-4, 1, 0);
boxNode.physicsBody = [SCNPhysicsBody dynamicBody];
boxNode.physicsBody.friction = 0.9;
boxNode.physicsBody.angularVelocityFactor = SCNVector3Make(0, 1, 0);
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
- (void)createDisk {
float n = 8;
float dw = 2.0 * M_PI / n;
for (int j=0; j<4; j++) {
SCNNode *disk = [SCNNode node];
disk.name = @"disk";
for (int i=0; i<n; i++) {
float x = 4 * cos(dw * i);
float z = 4 * sin(dw * i);
SCNBox *b = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0];
b.firstMaterial.diffuse.contents = [self color:(i + j) % 2 ? 1 : 3];
SCNNode *bNode = [SCNNode nodeWithGeometry:b];
bNode.position = SCNVector3Make(x, -j * 4, z);
[disk addChildNode:bNode];
}
disk.physicsBody = [SCNPhysicsBody dynamicBody];
disk.physicsBody.friction = 1;
SCNPhysicsHingeJoint *j = [SCNPhysicsHingeJoint jointWithBody:disk.physicsBody axis:SCNVector3Make(0, 1, 0) anchor:disk.position];
[self.sceneView.scene.physicsWorld addBehavior:j];
[self.sceneView.scene.rootNode addChildNode:disk];
}
}
- (void)createWall {
for (int i=0; i<4; i++) {
SCNNode *wallWorld = [SCNNode node];
wallWorld.rotation = SCNVector4Make(0, 1, 0, i * M_PI * 0.5);
[self.sceneView.scene.rootNode addChildNode:wallWorld];
SCNBox *w = [SCNBox boxWithWidth:2 height:1.5 length:0.2 chamferRadius:0];
w.firstMaterial.diffuse.contents = [self color:0];
SCNNode *wnode = [SCNNode nodeWithGeometry:w];
wnode.position = SCNVector3Make(4, -i * 4 + 1.4, 0);
wnode.physicsBody = [SCNPhysicsBody staticBody];
[wallWorld addChildNode:wnode];
}
}
- (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, -4, 20);
camera.rotation = SCNVector4Make(1, 0, 0, -0.1);
[self.sceneView.scene.rootNode addChildNode:camera];
}
- (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time {
[self.sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
if ([child.name isEqual:@"disk"]) {
child.physicsBody.angularVelocity = SCNVector4Make(0, 1, 0, 0.3);
}
}];
}
#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 {
int colorCode[] = {0x595241, 0xB8A9C, 0xF0F0F0, 0xACCFCC, 0x8A0917};
if (i > 4) return nil;
return ColorHex(colorCode[i]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment