
ワイヤーに引っかかっているリン
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createWire];
[self createRing];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:0];
sv.autoenablesDefaultLighting = YES;
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createWire {
SCNNode *wire = [SCNNode node];
wire.name = @”wire”;
for (int i=0; i<5; i++) {
SCNCylinder *c = [SCNCylinder cylinderWithRadius:0.1 height:(i + 1) * 2];
c.firstMaterial.diffuse.contents = [self color:1];
SCNNode *cNode = [SCNNode nodeWithGeometry:c];
cNode.position = SCNVector3Make(0, (4 – i * 2), 0);
cNode.rotation = SCNVector4Make(0, 0, 1, M_PI * 0.5);
[wire addChildNode:cNode];
SCNCylinder *cv = [SCNCylinder cylinderWithRadius:0.1 height:2.3];
cv.firstMaterial.diffuse.contents = [self color:1];
SCNNode *cvNode = [SCNNode nodeWithGeometry:cv];
cvNode.position = SCNVector3Make(((i%2)?1:-1) * (i + 1.5), (3 – i * 2), 0);
cvNode.rotation = SCNVector4Make(0, 0, 1, (i%2)?0.45:-0.45);
[wire addChildNode:cvNode];
}
wire.physicsBody = [SCNPhysicsBody dynamicBody];
[self.sceneView.scene.rootNode addChildNode:wire];
SCNPhysicsHingeJoint *joint = [SCNPhysicsHingeJoint jointWithBody:wire.physicsBody axis:SCNVector3Make(0, 0, 1) anchor:SCNVector3Make(0, 4, 0)];
[self.sceneView.scene.physicsWorld addBehavior:joint];
}
– (void)createRing {
SCNTorus *t = [SCNTorus torusWithRingRadius:1 pipeRadius:0.2];
t.firstMaterial.diffuse.contents = [self color:2];
SCNNode *tNode = [SCNNode nodeWithGeometry:t];
tNode.position = SCNVector3Make(0, 2, 0);
tNode.rotation = SCNVector4Make(0, 0, 1, M_PI * 0.5);
[self.sceneView.scene.rootNode addChildNode:tNode];
tNode.physicsBody = [SCNPhysicsBody dynamicBody];
tNode.physicsBody.physicsShape = [SCNPhysicsShape shapeWithNode:tNode options:@{SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron}];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 20);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
SCNNode *wire = [self.sceneView.scene.rootNode childNodeWithName:@”wire” recursively:NO];
if (p.x > CGRectGetMidX(self.view.bounds)) {
[wire.physicsBody applyForce:SCNVector3Make(2, 0, 0) atPosition:SCNVector3Make(0, –10, 0) impulse:YES];
} else {
[wire.physicsBody applyForce:SCNVector3Make(-2, 0, 0) atPosition:SCNVector3Make(0, –10, 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.0]
– (UIColor *)color:(int)i {
if (i>2) return nil;
int colorCode[] = {0x3B313D, 0x8B929C, 0xFF2479};
return ColorHex(colorCode[i]);
}
@end